Spring Cloud Consul

项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,具有分布式、高可用、高扩展性

Consul

Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul的方案更“一站式” ,内置了服务注册与发现框 架、具有以下性质:

● 分布一致性协议实现

● 健康检查

● Key/Value存储

● 多数据中心方案

不再需要依赖其他工具(比如ZooKeeper等)

添加依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

consul-all依赖提供了哪些功能?

<!--消息总线,提供配置实时刷新,不再依赖中间件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-bus</artifactId>
</dependency>
<!--consul的配置中心功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<!--服务注册和发现功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

bootstarp.yml

这里要注意是要配置在 bootstarp.yml

spring:
application:
name: pig-consul
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
format: KEY_VALUE
watch:
enabled: true
prefix: pig-config

安装consul

下载: https://www.consul.io/downloads.html

使用:(dev模式,生成建议cluster模式)

-dev表示开发模式运行,使用-client 参数可指定允许客户端使用什么ip去访问,例如-client 127.0.0.1 表示可以使用。  

consul agent -dev -client 127.0.0.1

生产配置参考:

https://www.consul.io/intro/getting-started/join.html

http://127.0.0.1:8500/ui/ 去访问

image

配置config

image

demo

@RestController
public class DemoController {
@Value("${author}")
private String author;
<span class="token annotation builtin">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/demo"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> String <span class="token function">demo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> author<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

关于实时刷新配置

spring:
cloud:
consul:
config:
watch:
enabled: true

然后应用要开启定时任务

@EnableScheduling

总结

  1. 相较于spring cloud config 的配置中心,使用起来较为麻烦,但是对于实时刷新,这块要优于spring cloud config 的,不依赖于中间件的消息通知,也不会出现服务下线的问题。
  2. 这篇文章主要是入门,更高级的使用Consul Config 结合 Git 来做版本控制,请参考我后边的文章
  3. 关于pig:基于Spring Cloud、oAuth2.0开发基于Vue前后分离的开发平台,支持账号、短信、SSO等多种登录,提供配套视频开发教程

原文地址:https://www.jianshu.com/p/b1463c1df5a2

Consul微服务的配置中心体验篇的更多相关文章

  1. 使用 Consul 作为 Python 微服务的配置中心

    使用 Consul 作为 Python 微服务的配置中心 Consul 作为数据中心,提供了 k/v 存储的功能,我们可以利用这个功能为 Python 微服务提供配置中心. Consul 提供了 HT ...

  2. 阿里巴巴微服务与配置中心技术实践之道 配置推送 ConfigurationManagement ConfigDrivenAnyting

    阿里巴巴微服务与配置中心技术实践之道 原创: 坤宇 InfoQ 2018-02-08 在面向分布式的微服务系统中,如何通过更高效的配置管理方式,帮助微服务系统架构持续"无痛"的演进 ...

  3. 阿里巴巴微服务与配置中心技术实践之道 原创: 坤宇 InfoQ 2018-02-08

    阿里巴巴微服务与配置中心技术实践之道 原创: 坤宇 InfoQ 2018-02-08

  4. 【微服务】- 配置中心 - Nacos

    微服务 - 配置中心 - Nacos 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 今天的学习任务就是学习使用Nacos作为配置中心. 努力克制自己,拒绝摆烂! 什么是配 ...

  5. Spring Cloud微服务集成配置中心

    1. 搭建Spring Cloud Config配置中心(见上一篇博客) 2. 创建微服务项目bounter-simon-app,pom文件如下: <?xml version="1.0 ...

  6. 微服务之配置中心ConfigKeeper

    在微服务架构中,配置中心是必不可少的基础服务.ConfigKeeper已开源,本文将深度分析配置中心的核心内容,错过「Spring Cloud中国社区北京沙龙-2018.10.28 」的同学将从本篇文 ...

  7. .Net Core微服务系列--配置中心

    什么是配置中心 简单来说配置中心就是对配置进行管理的一个中心.对于配置这个司空见惯的东西,我们想想为什么对于应用程序需要各种各样的配置来支撑? 我们人类没有办法掌控和预知一切,所以映射到软件系统这个领 ...

  8. 【微服务】之二:从零开始,轻松搞定SpringCloud微服务系列--注册中心(一)

    微服务体系,有效解决项目庞大.互相依赖的问题.目前SpringCloud体系有强大的一整套针对微服务的解决方案.本文中,重点对微服务体系中的服务发现注册中心进行详细说明.本篇中的注册中心,采用Netf ...

  9. 基于Apollo实现.NET Core微服务统一配置(测试环境-单机)

    一.前言 注:此篇只是为测试环境下的快速入门.后续会给大家带来生产环境下得实战开发. 具体的大家可以去看官方推荐.非常的简单明了.以下介绍引用官方内容: Apollo(阿波罗)是携程框架部门研发的分布 ...

随机推荐

  1. org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

    编程中遇到:org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot ...

  2. JVM synchronized关键字所生成的字节码

    一.创建测试类 package com.example.jvm.bytecode; public class MyTest2 { String str = "Welcome"; p ...

  3. locust性能测试脚本模板

    locust性能测试脚本模板 #!/usr/bin/env python # -*- coding: utf-8 -*- import time from locust import HttpLocu ...

  4. Web Application Framework

    ASP.NET Boilerplate https://github.com/aspnetboilerplate ASP.NET Boilerplate - Web Application Frame ...

  5. Kali Linux软件更新日报20190623

    Kali Linux软件更新日报20190623   (1)payloadsallthethings更新到2.0-0kali4,此次更新增加帮助脚本. (2)tftpd32更新到4.50-0kali2 ...

  6. [转][osg]探究osg中的程序设计模式【目录】

    作者:3wwang 原文接连:http://www.3wwang.cn/html/article_104.html 前序 探究osg中的程序设计模式---开篇 探究osg中的程序设计模式---创造性模 ...

  7. 基于spark logicplan的表血缘关系解析实现

    随着公司平台用户数量与表数量的不断增多,各种表之间的数据流向也变得更加复杂,特别是某个任务中会对源表读取并进行一系列复杂的变换后又生成新的数据表,因此需要一套表血缘关系解析机制能清晰地解析出每个任务所 ...

  8. visual studio code 调试ROS的插件

    ctrl+p搜索: ext install ros https://marketplace.visualstudio.com/items?itemName=ajshort.ros 进行安装 其他可以调 ...

  9. ES6深入浅出-12 ES6新增的API(下)-1.录屏

    String.includes es5里面判断字符串是否存在的方法 search searcg的厉害之处是可以使用正则 match正则的方式 repeat -1遍,就不合法 startsWith 判断 ...

  10. 【问题解决】Flasgger mapping values are not allowed here?

    参考来源:https://stackoverflow.com/questions/9055371/python-and-pyaml-yaml-scanner-scannererror-mapping- ...