推荐:微服务汇总
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新
首先创建一个Spring Boot项目作为注册中心。
pom.xml
如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kaven</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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>
application.yml
如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false
server:
enable-self-preservation: true
spring:
application:
name: eureka
server:
port: 8761
启动类:
package com.kaven.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
注解@EnableEurekaServer
不要忘记加上。
再来创建一个Spring Boot项目作为Config Server配置中心。
pom.xml
如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kaven</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</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>
这个依赖很重要:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
之前遇到过问题:Spring Cloud Config使用Github的Webhooks功能出现400状态码
要实现配置动态更新,还需要使用Bus组件,这个组件使用起来非常简单,只需要引入依赖,配置相关消息队列的信息即可,其他的就不用管了,因为Config组件内部已经封装好了,当配置文件发生更改后,Config Server会收到消息(我这里是使用Github的Webhooks功能),然后Config Server通过Bus组件将配置更改消息告诉Config Client。这里就先用一下Bus组件,不过多介绍,并且消息队列是RabbitMQ。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
application.yml
如下:
server:
port: 8083
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://github.com/ITKaven/config # 配置文件仓库,之后会进行创建
username: username
password: password
basedir: E:\workspace\IDEA\spring cloud\config\basedir
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: "*"
GitHub会通过你设置的Webhooks来请求你Config Server的monitor
接口,所以这里暴露全部端点(接口):
management:
endpoints:
web:
exposure:
include: "*"
启动类:
package com.kaven.config;
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
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
注解@EnableDiscoveryClient
、@EnableConfigServer
不要忘记加上。
先启动注册中心和Config Server配置中心。
GitHub会通过你设置的Webhooks来请求你Config Server的monitor
接口,而我们是在本地进行学习开发的,GitHub是不能请求到我们本地的接口,所以这里需要使用到内网穿透工具。
我使用的是这个(不是广告):内网穿透
创建免费隧道,代理到本地8083
端口,也就是我Config Server的运行端口。
再根据natapp
提供的教程来实现内网穿透,其实很简单,下载一个exe
文件,然后输入命令即可。
这样就弄好了。
再来创建一个Github仓库,用来存放配置文件。
设置该GitHub仓库的Webhooks。
可以发现此时Github已经请求了一次,并且请求成功。
再来创建一个Spring Boot项目作为获取配置的Config Client。
pom.xml
如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kaven</groupId>
<artifactId>getconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>getconfig</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
bootstrap.yml
如下:
spring:
application:
name: getconfig
cloud:
config:
discovery:
enabled: true
service-id: config
profile: dev
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
注意这里配置文件是bootstrap.yml
,而不是application.yml
,因为它需要先去获取配置文件才能运行,而不能运行后再去获取配置文件(端口都没有配置,需要去Config Server获取)。
启动类:
package com.kaven.getconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GetconfigApplication {
public static void main(String[] args) {
SpringApplication.run(GetconfigApplication.class, args);
}
}
注解@EnableDiscoveryClient
不要忘记加上。
再在Github的配置中心仓库创建文件getconfig.yml
(文件命名要按要求定义):
创建一个接口:
package com.kaven.getconfig.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 GetConfig {
@Value("${kaven}")
private String kaven;
@GetMapping("/getkaven")
public String getKaven(){
return kaven;
}
}
注解@RefreshScope
不能少,这是实现配置动态更新的关键一步。
启动项目。
可以看到项目会自动去配置中心拉取配置文件。
项目成功运行在8000
端口。
访问http://localhost:8000/getkaven
,可以得到如下结果:
我们来改变一下配置参数kaven
。
配置更新后,Github会请求Config Server配置中心的monitor
接口,通过内网穿透工具可以看见Github的请求成功了。
再来访问http://localhost:8000/getkaven
,可以得到如下结果:
这就实现了配置动态更新。
写博客是博主记录自己的学习过程,如果有错误,请指正,谢谢!
标签:spring,Spring,boot,springframework,Bus,org,import,Config,cloud From: https://blog.51cto.com/u_15870611/5838866