9Cloud Config改造(Kafka) · SpringCloud微服务实战 · 看云
导航
本节代码地址
前面我们使用了RabbitMQ 来改造Spring Cloud Config,这节我们使用Kafka 来改造Spring Cloud Config。在RabbitMQ 那一节使用的是spring-cloud-starter-bus-amqp包,和RabbitMQ 那一节集成 Spring Cloud Bus 集成的区别是包和连接配置有区别,下面我们看下Kafka的依赖包。
1.新建项目
我们新建一个客户端项目用来演示Spring Cloud Bus、Kafka 的示例
2.maven 配置
需要引入spring-cloud-starter-bus-kafka包,这个Spring Cloud Bus 和Kafka 集成的工具包,未开发节省了很多操作。
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
</dependencies>
3.新建启动类
这里和RabbitMQ 集成Sring Cloud Bus 是一样的
@EnableDiscoveryClient
@SpringBootApplication
public class FwConfigKafkaClientApplication {
public static void main(String[] args) {
SpringApplication.run(FwConfigKafkaClientApplication.class, args);
}
}
4. 应用配置
配置中和RabbitMQ 集成的区别主要是讲RabbitMQ 的连接信息换成Kafka的连接信息,确保Kafka 是启动的。同时我们将应用的健康信息和bus-refresh接口暴露出去
server:
port: 8779
spring:
application:
name: fw-register-eureka-client
cloud:
config: #自己指定的和服务发现的2选1
# uri: http://localhost:8778/ 自己指定的
profile: dev
label: master
discovery: #基于服务发现的
enabled: true
service-id: fw-config-server
# kafka
stream:
kafka:
binder:
brokers: localhost:9092
bus:
trace:
enabled: true
management:
endpoints:
web:
exposure:
include: refresh,health,info,bus-refresh
5. 启动项目
先将fw-cloud-config-native-server启动起来,然后启动本项目,否则会报错,获取不到配置信息
浏览器或者Postman 测试localhost:8779/api/version
如果修改了服务的配置,例如git 仓库修改的配置,通过localhost:8779/actuator/bus-refresh即可给全部客户端刷新配置,这里和RabbitMQ 集成Spring Cloud Bus那一节一样的,可以回头再看一下。

