作者:jiangzz
电话:15652034180
微信:jiangzz_wx
微信公众账号:jiangzz_wy
In the micro-service architecture systems, we often use lightweight message broker to build a common theme to make micro-news service instance all systems are connected up, because the message that topic will be generated in all instances of listening and consumption so we call it the message bus. Each instance on the bus can easily broadcast a number of other instances need to make the connection on the subject are aware of the news, for example, or some other configuration change management and other operational information. Since the message bus service is widely used in micro-architecture system, so it is the same configuration as the center, almost micro-services architecture prerequisites. Spring Cloud as a micro-service architecture integrated solutions, which naturally has its own implementation, this is the chapter Spring Cloud Bus we are going to introduce specific. By using the Spring Cloud Bus, it can be easily erected message bus, while achieving a number of common features in the message bus, for example, with dynamic updates Spring Cloud Config achieve micro service application configuration information and the like.
Configure server integration Kafka bus
Add the following dependence for the configuration server
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
In application.proeprties add the following
server.port = 8080
# 连接远程git读取配置信息
spring.cloud.config.server.git.uri = https://github.com/jiangzz/applicationconfig.git
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.refresh-rate=5
spring.cloud.config.server.git.repos.aa.search-paths=app1
spring.cloud.config.server.git.repos.aa.clone-on-start=true
spring.cloud.config.server.git.repos.aa.pattern=app*
spring.cloud.config.server.git.repos.aa.uri=https://github.com/jiangzz/applicationconfig.git
spring.cloud.config.server.git.repos.aa.refresh-rate=5
management.endpoints.web.exposure.include=*
## 配置eureka
spring.application.name=CONFIG-SERVICE
eureka.instance.instance-id=001
eureka.instance.prefer-ip-address=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.client.register-with-eureka=true
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/
# 配置bus总线
spring.kafka.bootstrap-servers=CentOS:9092,CentOS:9093,CentOS:9094
The machine must configure the mapping between the host name and ip CentOS, start the following SpringBootConfigServer
@SpringBootApplication
@EnableConfigServer
public class SpringBootConfigServer {
public static void main(String[] args) {
SpringApplication.run(SpringBootConfigServer.class,args);
}
}
Start the service, you can see Kafka message server
[root@CentOS kafka_2.11-2.2.0]# ./bin/kafka-topics.sh --zookeeper CentOS:2181 --list
springCloudBus
Configure the client integration Kafka bus
Add the following dependence file in pom
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties follows
server.servlet.context-path= /SpringCloudConfigClient
bootstrap.properties follows
# 指定连接的配置服务名字
spring.application.name=app1
spring.cloud.config.profile=test
spring.cloud.config.label=master
spring.cloud.config.name= SpringCloudConfigServer
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=CONFIG-SERVICE
# 开启所有的健康检查
management.endpoints.web.exposure.include=*
## 配置Eureka
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/
# 配置bus总线
spring.kafka.bootstrap-servers=CentOS:9092,CentOS:9093,CentOS:9094
TestController follows
@RefreshScope
@RestController
public class TestController {
@Value("${server.port}")
private int port;
@RequestMapping
public int test(){
return port;
}
}
ConfigClientApplication code is as follows
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}
At this point, the user attempts to update the configuration information git repository, and then executed on the server configuration, you can see all servers will refresh their configuration information.
curl.exe -X POST http://localhost:8080//actuator/bus-refresh
If you want to update some server specified can specify the server name and port
curl.exe -X POST http://localhost:8080/actuator/bus-refresh/app1:7077