定义 :
功能:
动态刷新全部通知
curl -X POST "http://localhost:3344/actuator/bus-refresh"
定点通知
curl -X POST "http://localhost:3344/actuator/bus-refresh/springcloud-config-client:3355"
spring boot集成spring cloud config 和spring cloud bus
利用消息总线触发服务端,从而刷新所有的客户端配置
大前提是在服务器上 已经开启了rabbitmq
(1)服务者
pom.xml
<dependencies>
<!--添加消息总线rabbitmq支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- 引入eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入spring cloud config 服务端 的依赖包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
=========================================================
配置文件
#配置端口号
server:
port: 3344
#配置服务名
spring:
application:
name: springcloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/liuyanan920921/springcloud-config.git #github上面的git仓库名字
search-paths:
- springcloud-config #搜索目录
#读取分支
label: master
#rabbitmq相关的配置
rabbitmq:
host: 152.136.27.48
port: 5672
username: tina
password: 123456
#rabbitq 相关配置 暴露bus刷新配置的端点
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
#注册到eureka
eureka:
client:
service-url:
defaultZone: http://springcloud-eureka-server7001:7001/eureka,http://springcloud-eureka-server7002:7002/eureka
instance:
instance-id: springcloud-config-center
prefer-ip-address: true #访问路径可以显示IP地址
lease-renewal-interval-in-seconds: 1 #向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-expiration-duration-in-seconds: 2 #收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除
#开启eureka负载均衡策略
ribbon:
eureka:
enabled: true
=========================================================
启动类
@SpringBootApplication
@EnableEurekaClient//表明是eureka客户端
@EnableConfigServer //开启spring cloud config
public class ConfigCenterApplication3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication3344.class,args);
}
}
=========================================================
(2) 客户端
pom.xml
<dependencies>
<!--添加消息总线rabbitmq支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--引入spring cloud config 客户端 的依赖包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 引入eureka客户端的依赖包 -->
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--添加common 通用包-->
<dependency>
<artifactId>springcloud-api-common</artifactId>
<groupId>com.tina.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
=========================================================
配置文件
#配置端口号
server:
port: 3355
#配置服务名
spring:
application:
name: springcloud-config-client
cloud:
#config客户端配置
config:
label: master #分支名称
name: config #配置文件的名称
profile: dev #读取后缀名出 ,上述配置综合:master分支上config-dev.yml
uri: http://localhost:3344 #配置中心的地址
#注册到eureka
eureka:
client:
service-url:
defaultZone: http://springcloud-eureka-server7001:7001/eureka
instance:
instance-id: springcloud-config-client1
prefer-ip-address: true #访问路径可以显示IP地址
lease-renewal-interval-in-seconds: 1 #向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-expiration-duration-in-seconds: 2 #收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除
#开启eureka负载均衡策略
ribbon:
eureka:
enabled: true
#暴露监控的端点
management:
endpoints:
web:
exposure:
include: "*"
#rabbitmq相关的配置
rabbitmq:
host: 152.136.27.48
port: 5672
username: tina
password: 123456
=========================================================
启动类
@SpringBootApplication
@EnableEurekaClient
@RefreshScope //开启实时监控
public class ConfigClientApplication3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication3355.class,args);
}
}
=========================================================
控制类
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@Value("${server.port}")
private String port;
@GetMapping(value = "/configInfo")
public String getConfigInfor()
{
return configInfo+":"+port;
}
}
=========================================================
(3)客户端
pom.xml
<dependencies>
<!--添加消息总线rabbitmq支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--引入spring cloud config 客户端 的依赖包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 引入eureka客户端的依赖包 -->
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--添加common 通用包-->
<dependency>
<artifactId>springcloud-api-common</artifactId>
<groupId>com.tina.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
=========================================================
配置文件
#配置端口号
server:
port: 3366
#配置服务名
spring:
application:
name: springcloud-config-client
cloud:
#config客户端配置
config:
label: master #分支名称
name: config #配置文件的名称
profile: dev #读取后缀名出 ,上述配置综合:master分支上config-dev.yml
uri: http://localhost:3344 #配置中心的地址
#注册到eureka
eureka:
client:
service-url:
defaultZone: http://springcloud-eureka-server7001:7001/eureka
instance:
instance-id: springcloud-config-client2
prefer-ip-address: true #访问路径可以显示IP地址
lease-renewal-interval-in-seconds: 1 #向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-expiration-duration-in-seconds: 2 #收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除
#开启eureka负载均衡策略
ribbon:
eureka:
enabled: true
#暴露监控的端点
management:
endpoints:
web:
exposure:
include: "*"
#rabbitmq相关的配置
rabbitmq:
host: 152.136.27.48
port: 5672
username: tina
password: 123456
=========================================================
启动类
@SpringBootApplication
@EnableEurekaClient
@RefreshScope //开启实时监控
public class Client3366Application3366 {
public static void main(String[] args) {
SpringApplication.run(Client3366Application3366.class,args);
}
}
=========================================================
控制类
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@Value("${server.port}")
private String port;
@GetMapping(value = "/configInfo")
public String getConfigInfor()
{
return configInfo+":"+port;
}
}
=========================================================
网友评论