博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springCloud config 实现高可用的分布式配置并可以自动刷新配置
阅读量:7024 次
发布时间:2019-06-28

本文共 11758 字,大约阅读时间需要 39 分钟。

hot3.png

        为了方便服务配置文件统一管理,更易于部署、维护,所以就需要分布式配置中心组件了,在spring cloud中,有分布式配置中心组件spring cloud config,它支持配置文件放在在配置服务的内存中,也支持放在远程Git仓库里。本项目采用的是springBoot2.0.2版本,版本不一致,配置以及访问会有所变化

       在github上面创建一个项目springCloud-config存放配置文件:

创建了一个用于访问,内容: name=ningcs。

为了实现config的高可用,引入eureka,上篇博文已经介绍,

我们在这里只需引入即可

eureka.client.serviceUrl.defaultZone=http://localhost:18170/eureka/

详细eureka代码详见地址:

  创建config服务端:config-server

配置文件

spring.application.name=config-serverserver.port=18173spring.profiles.active=dev#eureka配置eureka.client.serviceUrl.defaultZone=http://localhost:18170/eureka/# 配置git仓库地址spring.cloud.config.server.git.uri=https://github.com/ningcs/springCloud-config#配置文件所在的目录spring.cloud.config.server.git.search-paths=/**# 配置仓库的分支spring.cloud.config.label=master# 访问git仓库的用户名spring.cloud.config.server.git.username=ningcs# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写spring.cloud.config.server.git.password=xxx

启动类添加@EnableConfigServer 

package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient@EnableConfigServer@SpringBootApplicationpublic class ConfigServerApplication {   public static void main(String[] args) {      SpringApplication.run(ConfigServerApplication.class, args);   }}

pom文件

4.0.0
com.example
demo
1.0.0
jar
config-server
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR1
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

服务端配置完成,下面验证一下服务端的配置有没有什么问题,依次启动eureka-server、config-server,在浏览器里输入

http://localhost:18173/config-client-dev.properties

name: ningcs

说明访问到了 

证明配置服务中心可以从远程程序获取配置信息,http请求地址和资源文件映射如下:,可参考

·        /{application}/{profile}[/{label}]

·        /{application}-{profile}.yml

·        /{label}/{application}-{profile}.yml

·        /{application}-{profile}.properties

·        /{label}/{application}-{profile}.properties

    创建config客户端:config-client

在resources目录下新建文件bootstrap.properties,内容:

#eureka配置eureka.client.serviceUrl.defaultZone=http://localhost:18170/eureka/#开启配置服务发现spring.cloud.config.discovery.enabled=true#配置服务实例名称spring.cloud.config.discovery.service-id=config-server# 指明配置服务中心的网址spring.cloud.config.uri=http://localhost:18173#配置文件所在分支spring.cloud.config.label=master#配置所使用文件类别 dev-开发,test测试,pro生产spring.cloud.config.profile=dev#spring.cloud.config.fail-fast:true##自动刷新配置 默认 health,infomanagement.endpoints.web.exposure.include=refresh,health,info##是否需要权限拉去,默认是true,如果不false就不允许你去拉取配置中心Server更新的内容management.security.enabled=false

pom:

4.0.0
com.example
demo
0.0.1-SNAPSHOT
jar
config-client
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR1
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-context
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

 

引入springcloud上下文依赖,使bootstrap配置文件生效

org.springframework.cloud
spring-cloud-context

这里说明一下为什么 要放在bootstrap里面,因为项目启动的时候是先加载bootstrap.properties

在client的bootstrap.properties中可以添加自己的属性配置,但如果有和server端相同的属性配置会以server端的会准。

application.properties配置如下

#spring.profiles.active=devspring.application.name=config-clientserver.port=18174#开启健康检查#eureka.client.healthcheck.enabled=trueinfo.app.name=@project.name@info.app.description=@project.description@info.app.version=@project.version@

启动方法:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@EnableEurekaClient@SpringBootApplicationpublic class ConfigClientApplication {   public static void main(String[] args) {      SpringApplication.run(ConfigClientApplication.class, args);   }}

测试方法:

@RefreshScope@RestControllerpublic class MyController {    @Value("${name}")    String foo;    @RequestMapping(value = "/name",method = RequestMethod.GET)    public String hi(){        return foo;    }    @RequestMapping(value = "/hi",method = RequestMethod.GET)    public String his(){        return "ok";    }}

    启动后在日志里面可以看到/actuator/health、/actuator/info、/actuator/refresh,如没有·说明没有配置成功。

2018-09-03 14:21:38.303  INFO 6000 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

2018-09-03 14:21:38.304  INFO 6000 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-09-03 14:21:38.304  INFO 6000 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/refresh],methods=[POST],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

 看到以下日志说明访问到了config-server

2018-09-03 11:50:54.272  INFO 7492 --- [io-18174-exec-4] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://172.21.122.120:18173/

 

注: @RefreshScope 开启 SpringCloudConfig 客户端的 refresh 刷新范围,来获取服务端的最新配置,@RefreshScope要加在声明声明的类上,否则refresh之后Conroller拿不到最新的值,会默认调用缓存。

操作步骤:

①首先在  修改配置文件

我把ningcs修改为 name=ningcs123

②然后访问 http://localhost:18174/actuator/refresh 会出现你修改的字段名 以及版本号

[

    "config.client.version",
    "name"
]

③然后调用 http://localhost:18174/name 如果不调用  http://localhost:18174/actuator/refresh则会显示ningcs

 每次修改配置文件更新都需要手动刷新配置,这样岂不是很麻烦,当然springcloud也解决了这个问题,

接下来我将引入springcloud-bus实现自动刷新。

新建config-bus-client项目(复制config-client即可)

在bootstarp.properites引入mq配置文件

#配置mqspring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=ningcsspring.rabbitmq.password=ningcs123456

pom添加mq jar包:

org.springframework.cloud
spring-cloud-starter-bus-amqp

总bootstarp.properites配置文件如下:

#eureka配置eureka.client.serviceUrl.defaultZone=http://localhost:18170/eureka/#开启配置服务发现spring.cloud.config.discovery.enabled=true#配置服务实例名称spring.cloud.config.discovery.service-id=config-server# 指明配置服务中心的网址spring.cloud.config.uri=http://localhost:18175#配置文件所在分支spring.cloud.config.label=master#配置所使用文件类别 dev-开发,test测试,pro生产spring.cloud.config.profile=dev#spring.cloud.config.fail-fast:true##自动刷新配置 默认 health,infomanagement.endpoints.web.exposure.include=refresh,health,info##是否需要权限拉去,默认是true,如果不false就不允许你去拉取配置中心Server更新的内容management.security.enabled=false#配置mqspring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=ningcsspring.rabbitmq.password=ningcs123456

application.properties配置如下:

#spring.profiles.active=devspring.application.name=config-bus-clientserver.port=18176#开启健康检查#eureka.client.healthcheck.enabled=trueinfo.app.name=@project.name@info.app.description=@project.description@info.app.version=@project.version@

①依次启动eureka-server,config-server,config-bus-client ,在  修改配置文件

②手动刷新配置http://172.21.122.120:18175/actuator/refresh

③访问 http://localhost:18176/name  如果显示成功说明已配置完成。

④在  配置webhooks

进入项目 /setting-->webhooks -->add webhook

然后配置url,这样你必须需要一个公网可以访问的地址即可,我这里只是示例。

然后修改 自动刷新配置 。

webhooks理解:

Webhook是一个API概念,并且变得越来越流行。我们能用事件描述的事物越多,webhook的作用范围也就越大。Webhook作为一个轻量的事件处理应用,正变得越来越有用。

准确的说webhoo是一种web回调或者http的push API,是向APP或者其他应用提供实时信息的一种方式。Webhook在数据产生时立即发送数据,也就是你能实时收到数据。这一种不同于典型的API,需要用了实时性需要足够快的轮询。这无论是对生产还是对消费者都是高效的,唯一的缺点是初始建立困难。

Webhook有时也被称为反向API,因为他提供了API规则,你需要设计要使用的API。Webhook将向你的应用发起http请求,典型的是post请求,应用程序由请求驱动。

 

注:写此博客的时候查阅文献,很多博主springboot2.0版本config-client都配置的

management.endpoints.web.exposure.include=bus-refresh

或者 config-server再加配置 management.endpoints.web.exposure.include=‘*’

我测试多次,均访问报404错误。

后来参考文档:

就将配置改为 

management.endpoints.web.exposure.include=refresh,health,info

即访问成功,如果有springboot2.0版本使用bus-refresh访问成功的可以留下评论探讨一下。

项目源码地址:

转载于:https://my.oschina.net/u/2851681/blog/1941461

你可能感兴趣的文章
HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
查看>>
八进制
查看>>
python网络爬虫笔记(五)
查看>>
java面试题——中间件&&数据库&&redis(三)
查看>>
batch normalization在测试时的问题
查看>>
PhoneGap 简单使用
查看>>
Python时间和日期
查看>>
二叉树节点的删除
查看>>
计算机网络面试总结
查看>>
在线看电视(清晰方便)
查看>>
AngularJs中,如何在render完成之后,执行Js脚本
查看>>
【BZOJ4298】[ONTAK2015]Bajtocja
查看>>
为什么C语言中int的表示范围是-32768~32767
查看>>
有关位运算的基础知识和应用
查看>>
框架dubbox的简单使用
查看>>
codevs1163访问艺术馆 树形dp
查看>>
java获取登陆用户的IP地址
查看>>
JAVA线程控制
查看>>
Java关键字final、static使用总结
查看>>
转载-Objective-C内存管理详解(含示例代码)
查看>>