前面我们已经创建了一个系统结构,包括Eureka配置中心,slueth(zipkin)服务追踪,ribbon及feign负载均衡,zuul路由网关,hystrix断路器、dashboard监控、turbine聚合监控等。下面来学习了解一下config配置中心,如何将配置中心注册到eureka进行集群化实现高可用,bus总线更新config配置。
一、部署一个简单的config配置中心configServer及客户端configclient
config配置中心的部署包括server和client两部分。使用spring-cloud-config-server和spring-cloud-starter-config两个依赖包
首先创建一个配置中心configserver,修改pom文件(为了快速搭建,我只是在工程中新增了module,又开始使用前面博文中的父pom文件了,我还是都粘出来吧)
父pom文件内容(其实只是固定了springboot2.0版本和springcloud F版本,没有什么其他配置):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud</groupId>
<artifactId>testspringcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springcloud</name>
<description>Demo project for Spring Cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
configserver的pom文件(添加了web依赖包和configserver依赖包):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.configserver</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>configserver</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>testspringcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
修改application.properties配置文件:
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
用了别人的github地址,自己可以依据需要修改,后续我也会调整博文使用自己本地配置文件信息
修改**application.java入口类,添加@EnableConfigServer依赖
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
启动configserver服务,浏览器访问http://localhost:8888/foo/dev查看到下面的信息就表示配置中心已经启动成功了
然后创建一个configclient工程,修改pom文件,引入spring-cloud-starter-config依赖包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.configclient</groupId>
<artifactId>configclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>configclient</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>testspringcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
修改配置文件,将来configserver的服务地址告诉configclient
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881
修改**application.java入口类,创建一个hi接口来反馈读入的配置信息,测试configclient是否能够通过configserver得到配置信息
@SpringBootApplication
@RestController
public class ConfigclientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigclientApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/hi")
public String hi(){
return foo;
}
}
启动configclient工程(当然这时候configserver要处于启动状态)
浏览器访问刚刚配置好的hi接口 http://localhost:8881/hi 得到如下截图信息就表示configclient已成功从configserver得到配置信息
二、将配置中心configserver注册到eurekaserver注册中心,通过集群实现高可用。
启动一个eurekaserver在我前面的博文里有,就不赘述了,还是使用8761端口开通注册中心
修改configserver的pom文件,添加eureka client 的依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
修改application.properties配置文件,添加eureka server的服务接口
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
启动eureka server,浏览器访问http://localhost:8761就可以看到eureka server 的可视化页面。
重新启动刚刚修改好的configserver,然后刷新eureka server的页面看看是否注册成功。
实现高可用无非是多台设备启动一样的configserver服务注册到eureka server中形成服务集群,本地测试没有意义,我就开一个好了。
下面调整configclient,也配置成eureka client注册到注册中心,修改pom文件添加eureka client的依赖包,上面有我就不粘贴了。
修改configclient的application.properties配置文件,添加eureka server的服务地址,同时将config server的服务地址识别方式从指定的ip+port改成注册中心的serviceID(application name)。
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=8881
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
然后启动config client(这时eureka server 和 config server应处于启动状态),浏览器再次调用hi接口看看效果
至此,高可用的配置中心部署完成。我们已经可以通过configserver为所有的configclient系统提供统一的配置信息。下面来学习一下如何在配置中心修改配置信息,然后通知configclient去及时更新配置信息。
三、调整配置中心读取本地配置
前面为了快速搭建学习config组件,使用了别人教程里的github配置,企业部署系统的时候想必没多少人愿意把自己的配置放到别人的服务器上,尤其是一些敏感信息。那么下面我们调整configserver的配置文件,将配置信息的读取从github上迁回本地。
spring.application.name=config-server
server.port=8888
#从github上读取配置信息
#spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
#spring.cloud.config.server.git.searchPaths=respo
#spring.cloud.config.label=master
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
#从本地文件读取配置信息
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=D:/conf
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
我只是注释掉了原来github的地址,新增了本地地址D:/conf。然后在该地址上放一个配置文件config-client-dev.properties
文件名是用来识别给哪个serviceID用的配置文件,弄懂了再修改,不然config client启动的时候可就读不到配置信息了。
config-client-div.properties文件中写入 foo=test-foo-info
重新看一下config client的配置文件
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#指定配置中心uri
#spring.cloud.config.uri= http://localhost:8888/
#指定配置中心serviceID
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=8881
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
没有修改config client的配置,只是确认一下资源文件映射
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
-
/{label}/{application}-{profile}.properties
确保在配置中心的config-client-dev.properties会被识别到。
重新启动config server和config client。浏览器调用hi接口 http://localhost:8881/hi
读取配置中心配置信息成功。
四、bus总线实现配置信息的更新通知。
前面第三节我们修改配置中心的配置到本地之后,config server和config client都要重启才能读入我新的配置文件信息,那么当微服务众多时候,想统一修改配置信息我们当然不希望干掉进程重新启动所有的微服务。下面通过bus消息总线实现配置信息变化的通知。
由于bus消息总线使用rabbitMQ消息队列进行消息推送,因此要先下载安装并启用rabbitMQ server。可以参考我的教程:
https://blog.csdn.net/qq_15903671/article/details/82754025 下载安装之后需要启动rabbitMQ server,然后进行下面的调整。
其实只需要在config client开发的时候引入spring-cloud-starter-bus-amqp和spring-boot-starter-actuator依赖包,修改配置文件引用rabbitMQ,入口类加上相应注解就可以了。OK,调整config client程序,pom文件添加依赖包。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改配置文件,添加rabbitmq和bus消息总线的配置
#配置rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#配置bus消息总线
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
修改入口类,添加注解
@SpringBootApplication
@RestController
@EnableEurekaClient
@EnableDiscoveryClient
@RefreshScope
maven下将configclient项目打包,运行8881和8882两个端口进行测试
然后修改D:/conf下保存的config-client-dev.properties配置文件中的信息,修改成foo=test-new-info
启动postmen,使用post方式调用http://localhost:8881/actuator/bus-refresh通知8881这个服务,配置信息已更改。
body里什么也不要写,不然会报错。再次调用8881和8882的hi接口看看效果
测试显示,配置文件的修改已经通过bus消息总线通知到所有config-client服务进群中的每个服务,配置更新成功。