1、Config
5. 测试结果
3. 改YML
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
2.2.4、测试结果
- 利用消息总线触发一个客户端
/bus/refresh
,而刷新所有客户端的配置
<dependencies>
<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>com.xiao</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
4. 主启动
- 在发送该请求
curl -X POST "http://localhost:3344/actuator/bus-refresh"
之后的测试结果如下
(1)、访问3344端口
-
Spring Cloud Config
分为服务端和客户端两部分。 - 服务端也成为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
- 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用
git
来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git
客户端工具来方便的管理和访问配置内容。
1.3、Config客户端配置与测试
<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>com.xiao</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
4. 主启动
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
/{application}/{profile}[/{label}]
/{application}-{profile}.yml ## 推荐使用这个
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
-
Module
的名称为cloud-config-center-3344
。
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
1.2、Config服务端配置与测试
(1)、POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
5. Controller类
2.3、定点通知形式的动态刷新
2.1、概述简介
官网地址
- 如图所示,我们成功的从相应的配置文件中读取到内容。
2. Config是什么
(1)、其他的访问路径格式
(2)、新添加的YML内容
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3366.class,args);
}
}
2. 改POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(1)、POM
4. 需要发送一个请求刷新客户端
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
3. 改YML
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
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 ConfigClientController {
@Value("${spring.application.name}")
private String serverPort;
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return "serverPort:"+serverPort+"\t\n\n configInfo: "+configInfo;
}
}
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
- 打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新职责
- 破坏了微服务各节点的对等性
- 有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改。
(2)、访问3355端口
1. 建Module
server:
port: 3344
spring:
application:
name: cloud-config-center
profiles:
active: dev
cloud:
config:
server:
git:
uri: https://gitee.com/henryxjh/springcloud-config.git # url地址
search-paths: # 搜索路径
- springcloud-config
label: master # 配置的是主分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
- 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
management:
endpoints:
web:
exposure:
include: "*"
- 利用消息总线触发一个服务端
ConfigServer
的/bus/refresh
端点,而刷新所有客户端的配置(更加推荐)
2. 给cloud-config-center-3355客户端添加消息总线支持
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
- 一次修改,广播通知,处处生效。
-
Linux
运维修改GitHub
上的配置文件内容做调整 -
刷新
3344
,发现ConfigServer
配置中心立刻响应 -
刷新
3355
,发现ConfigServer
客户端没有任何响应 -
3355
没有变化除非自己重启或者重新加载
文章目录
- 1、Config
-
- 1.1、概述简介
- 1.2、Config服务端配置与测试
- 1.3、Config客户端配置与测试
- 1.4、Config客户端之动态刷新(手动版)
-
- 1.4.1、面临的问题
- 1.4.2、动态刷新配置与测试
- 2、Bus
-
- 2.1、概述简介
- 2.2、全局广播形式的动态刷新
-
- 2.2.1、新添加一个客户端
- 2.2.2、两种导致全局动态刷新的方式
- 2.2.3、添加消息总线支持的配置
- 2.2.4、测试结果
- 2.3、定点通知形式的动态刷新
1. 建Module
2.2.1、新添加一个客户端
2.2.2、两种导致全局动态刷新的方式
2. 改POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
1.4.2、动态刷新配置与测试
1.4、Config客户端之动态刷新(手动版)
1. 是什么
(2)、新添加的YML内容
<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>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
curl -X POST "http://localhost:3355/actuator/refresh"
3. 给cloud-config-center-3366客户端添加消息总线支持
(1)、第二种架构方式更合适的原因
- 要将
application.yaml
文件名称改为bootstrap.yaml
是很关键的,bootstrap.yaml
是比application.yaml
优先加载的。且它是系统级的,优先级更高。 -
SpringCloud
会创建一个Bootstrap Context
,作为Spring
应用的Application Context
的父上下文。初始化的时候,Bootstrap Context
负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment
。
3. 能干嘛
2、Bus
5. 测试结果
- 在
2.2
小节上述环境,只通知3355
不通知3366
的请求如下:
1.4.1、面临的问题
2. 在yml文件中添加以下代码
server:
port: 3366
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
6. 测试结果
-
Spring Cloud Bus
能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、时间推送等,也可以当作微服务间的通信通道。
1. 建Module
-
Module
的名称为cloud-config-client-3355
。
1.1、概述简介
3. 改YML
2. 能干嘛
(1)、POM
-
Spring Cloud Bus
是用来将分布式系统的节点与轻量级消息系统连接起来的框架,它整合了Java
的事件处理机制和消息中间件的功能。Spring Cloud Bus
目前支持RabbitMQ
和Kafka
。 - 在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个公用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。
- 其原理是
ConfigClient
实例都监听MQ
中同一个topic
默认是SpringCloudBus
。当一个服务刷新数据的时候,他会把这个消息放入到Topic
中,这样其它监听到同一Topic
的服务就能得到通知,然后去更新自身的配置。
2.2.3、添加消息总线支持的配置
5. controller类
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署比如
dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露
3. 修改controller类
(2)、新添加的YML内容
-
Module
的名称为cloud-config-client-3366
。
1. POM引入actuator监控
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${spring.application.name}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
2.2、全局广播形式的动态刷新
1. 分布式面临的问题
1. 给cloud-config-center-3344配置中心服务端添加消息总线支持
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 ConfigClientController {
@Value("${spring.application.name}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
2. 改POM
4. 主启动
本文转自 https://blog.csdn.net/weixin_56727438/article/details/122447009
评论