spring-cloud-config
spring-cloud-zookeeper
spring-cloud-bus(kafka)
实现自动化更新服务项目总结备注:整个过程基于IntelliJ IDEA
,整个工程基于父项目版本如下:
org.springframework.boot
spring-boot-starter-parent
2.1.13.RELEASE
spring-cloud-config
,实现配置中心服务zookeeper
,kafka
config-center-server
作为配置中心服务config-center-client
配置中心服务引用方pom.xm
如下
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.13.RELEASEversion>
<relativePath/>
parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-sampleartifactId>
<version>${project.version}version>
<name>config-center-samplename>
<packaging>pompackaging>
<modules>
<module>config-center-servermodule>
<module>config-center-clientmodule>
modules>
<properties>
<project.version>0.0.1-SNAPSHOTproject.version>
<java.version>1.8java.version>
<spring-cloud.version>Greenwich.SR5spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
project>
config-center-server
配置在pom.xml添加依赖
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-sampleartifactId>
<version>0.0.1-SNAPSHOTversion>
<relativePath/>
parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-serverartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>config-center-servername>
<description>Demo project for config centerdescription>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
在Resources文件夹下增加文件bootstrap.yml
其中增加配置,其中Spring cloud中先加载bootstrap.yml
再加载application.yml
中的配置,其配置文件如下:
#application.yml
server:
port: 10010
management:
endpoints:
web:
exposure:
include: '*' #通过web地址访问,暴露的path
spring:
application:
name: config-center-server #服务名称
##以下配置是spring-cloud-config 配置中心服务的配置文件存放的位置
cloud:
config:
server:
git:
uri: https://[email protected]/zqlgit/JavaProjects/_git/configCenter #git远程Repository
username: Zhou_Qilin #用户名称
password: nbehijwmihyxjzo7xcw5v**jee4y5dup77ematt4kb2oq6jle6trqioq #用户密码
basedir: basedir #git中文件下载到本地的目录
修改启动文件,在文件中增加相应的注解类@EnableConfigServer
package com.hitgen.configcenterserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterServerApplication.class, args);
}
}
通过以上步骤,配置中心的配置已经完成,可以启动项目正常运行。
备注:git中有文件config-center-client-dev.yml
内容为:
#config-center-client-dev.yml
server:
port: 9001
label: test-label-hitgen
添加相关依赖,其pom.xml
文件内容如下
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-sampleartifactId>
<version>0.0.1-SNAPSHOTversion>
<relativePath/>
parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-clientartifactId>
<version>${project.version}version>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<version>RELEASEversion>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
在Resources文件夹下增加文件bootstrap.yml
其中增加配置,其中Spring cloud中先加载bootstrap.yml
再加载application.yml
中的配置,其配置文件如下:
#bootstrap.yml
#应用名称,自动加载配置中心的配置,根据规则{uri}/[lable]/{application-name}-[profile].yml
#生成 http://localhost:10010/master/config-center-client-dev.yml获取配置中心的配置,使用其启动项目
spring:
cloud:
config:
uri: http://localhost:10010/ #spring-cloud-config 引用配置中心的url
label: master #label名称
profile: dev #profile
application:
name: config-center-client #application-name
增加ClientController
类,作为获取配置中心服务的配置,其内容如下:
package com.hitgen.configcenterclient.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ClientController {
@Value("${label:default}")
private String label;
@GetMapping("label")
public String getLabel() {
return label;
}
}
启动项目,浏览器中输入http://localhost:9001/label
可以看到显示值为:test-label-hitgen
spring-cloud-discovery
,以zookeeper为服务注册中心config-center-server
增加spring-cloud-discovery相关依赖在pom.xml
文件中增加spring-cloud-starter-zookeeper-discovery
,其完整配置如下
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-sampleartifactId>
<version>0.0.1-SNAPSHOTversion>
<relativePath/>
parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-serverartifactId>
<version>${project.version}version>
<name>config-center-servername>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
引入设置zookeeper
为注册中的配置,并且设置启用服务发现注册,服务修改配置文件内容 bootstrap.yml
如下:
spring:
application:
name: config-center-server #服务名称
##以下配置是spring-cloud-config 配置中心服务的配置文件存放的位置
cloud:
config:
server:
git:
uri: https://[email protected]/zqlgit/JavaProjects/_git/configCenter #git远程Repository
username: Zhou_Qilin #用户名称
password: nbehijwmihyxjzo7xcw5vj4y5dup77ematt4kb2oq6jle6trqioq #用户密码
basedir: basedir #git中文件下载到本地的目录
##zookeeper作为注册中心的配置,注册时的配置信息及说
zookeeper: #zookeeper配种信息 此配置信息必须在bootstarp.yml文件中
connect-string: 192.168.40.105:2181
enabled: true
discovery: #注册服务
enabled: true #true-发现(默认值)
root: /services #zookeeper的根目录,默认值(/services)
instance-id: config-center-server #zookeeper叶子节点(实例)名称
uri-spec: http://127.0.0.1:10010 #注册到zookeeper中的配置中心往外提供服务的协议及地址,默认使用环境变量 hots-name,port组合
为启动入口类ConfigCenterServerApplication
增加注解@EnableDiscoveryClient
,其完整代码如下:
package com.hitgen.configcenterserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigCenterServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterServerApplication.class, args);
}
}
config-center-client
以zookeeper
为注册中心,增加spring-cloud-discovery
的相关依赖在pom.xml
文件中增加spring-cloud-starter-zookeeper-discovery
,其完整配置如下
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-sampleartifactId>
<version>0.0.1-SNAPSHOTversion>
<relativePath/>
parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-clientartifactId>
<version>${project.ver}version>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
在bootstrap.yml
文件中引入自动发现服务配置,其完整配置如下:
#bootstrap.yml
#应用名称,自动加载配置中心的配置,根据规则{uri}/[lable]/{application-name}-[profile].yml
#生成 http://localhost:10010/master/config-center-client-dev.yml获取配置中心的配置,使用其启动项目
spring:
application:
name: config-center-client #application-name
main:
allow-bean-definition-overriding: true #配置zookeeper为注册中心发现服务时,必须设置该值为true,表示后注册bean覆盖前面相同的bean
cloud:
config:
# uri: http://localhost:10010/ #spring-cloud-config 引用配置中心的url
label: master #label名称
profile: dev #profile 多个逗号分隔
discovery:
service-id: config-center-server #配置中心服务名称
enabled: true #配置自动发现服务
zookeeper:
connect-string: 192.168.40.105:2181 #zookeeper注册中心的地址
启动项目,自动加载git
中配置,设置服务端口为9001。浏览器中输入http://localhost:9001/label
可以看到显示值为:test-label-hitgen
spring-cloud-bus
,以kafka为消息中间件,实现服务配置自动化刷新config-center-server
增加spring-cloud-starter-bus-kafka依赖包作为消息中间件在pom.xml
文件中引入spring-cloud-starter-bus-kafka
依赖包,完整文件如下:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-sampleartifactId>
<version>0.0.1-SNAPSHOTversion>
<relativePath/>
parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-serverartifactId>
<version>${project.ver}version>
<name>config-center-servername>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-kafkaartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
在bootstrap.yml
文件中引入kafka相关配置,其完整配置如下:
spring:
application:
name: config-center-server #服务名称
##以下配置是spring-cloud-config 配置中心服务的配置文件存放的位置
cloud:
config:
server:
git:
uri: https://gitee.com/hql2019/config-center.git
username: [email protected] #用户名称
password: gitee-zhou765219 #密码
basedir: basedir #git中文件下载到本地的目录
##zookeeper作为注册中心的配置,注册时的配置信息及说
zookeeper: #zookeeper配种信息 此配置信息必须在bootstarp.yml文件中
connect-string: 192.168.40.105:2181
enabled: true
discovery: #注册服务
enabled: true #true-发现(默认值)
root: /services #zookeeper的根目录,默认值(/services)
instance-id: config-center-server #zookeeper叶子节点(实例)名称
uri-spec: http://127.0.0.1:10010 #注册到zookeeper中的配置中心往外提供服务的协议及地址,默认使用环境变量 hots-name,port组合
bus: #以下为消息总线配置
trace:
enabled: true #打开acks消息跟踪
kafka:
bootstrap-servers: 192.168.40.105:9092
启动项目
config-center-client
增加spring-cloud-starter-bus-kafka
依赖包作为消息中间件,自动更新配置信息在pom.xml
文件中引入spring-cloud-starter-bus-kafka
依赖包,完整文件如下:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-sampleartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.hitgengroupId>
<artifactId>config-center-clientartifactId>
<version>${project.ver}version>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-kafkaartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
在bootstrap.yml
文件中引入自动发现服务配置,其完整配置如下:
#bootstrap.yml
#应用名称,自动加载配置中心的配置,根据规则{uri}/[lable]/{application-name}-[profile].yml
#生成 http://localhost:10010/master/config-center-client-dev.yml获取配置中心的配置,使用其启动项目
spring:
application:
name: config-center-client #application-name
main:
allow-bean-definition-overriding: true #配置zookeeper为注册中心发现服务时,必须设置该值为true,表示后注册bean覆盖前面相同的bean
cloud:
config:
# uri: http://localhost:10010/ #spring-cloud-config 引用配置中心的url
label: master #label名称
profile: dev #profile 多个逗号分隔
discovery:
service-id: config-center-server #配置中心服务名称
enabled: true #配置自动发现服务
zookeeper:
connect-string: 192.168.40.105:2181 #zookeeper注册中心的地址
kafka: #以下为kafka配置信息
bootstrap-servers: 192.168.40.105:9092 #连接信息
在所需更新的类中增加@RefreshScope()
,如下:
package com.hitgen.configcenterclient.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ClientController {
@Value("${label:default}")
private String label;
@GetMapping("label")
public String getLabel() {
return label;
}
}
启动项目,更改git Repository 上文件label对应的值,并且提交。通过PostMan 请求(POST)http://localhost:10010/actuator/bus-refresh
再通过访问http://localhost:9001/label
得到最新的值
url/actuator/bus-refresh
,设置每次更新时,自动访问设置的地址按照以上的步骤,一步一步进行,能准确实现,配置中心服务,并且自动更新使用端的配置。
源码下载