1. 分布式面临的问题
2. Config是什么
Spring Cloud Config
分为服务端和客户端两部分。git
来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git
客户端工具来方便的管理和访问配置内容。3. 能干嘛
dev/test/prod/beta/release
1. 建Module
Module
的名称为cloud-config-center-3344
。2. 改POM
<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>
3. 改YML
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
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);
}
}
5. 测试结果
(1)、其他的访问路径格式
/{application}/{profile}[/{label}]
/{application}-{profile}.yml ## 推荐使用这个
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
1. 建Module
Module
的名称为cloud-config-client-3355
。2. 改POM
<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>
3. 改YML
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
application.yaml
文件名称改为bootstrap.yaml
是很关键的,bootstrap.yaml
是比application.yaml
优先加载的。且它是系统级的,优先级更高。SpringCloud
会创建一个Bootstrap Context
,作为Spring
应用的Application Context
的父上下文。初始化的时候,Bootstrap Context
负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment
。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);
}
}
5. Controller类
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;
}
}
6. 测试结果
Linux
运维修改GitHub
上的配置文件内容做调整3344
,发现ConfigServer
配置中心立刻响应3355
,发现ConfigServer
客户端没有任何响应3355
没有变化除非自己重启或者重新加载1. POM引入actuator监控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 在yml文件中添加以下代码
management:
endpoints:
web:
exposure:
include: "*"
3. 修改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 ConfigClientController {
@Value("${spring.application.name}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
4. 需要发送一个请求刷新客户端
curl -X POST "http://localhost:3355/actuator/refresh"
5. 测试结果
(1)、访问3344端口
(2)、访问3355端口
1. 是什么
Spring Cloud Bus
是用来将分布式系统的节点与轻量级消息系统连接起来的框架,它整合了Java
的事件处理机制和消息中间件的功能。Spring Cloud Bus
目前支持RabbitMQ
和Kafka
。ConfigClient
实例都监听MQ
中同一个topic
默认是SpringCloudBus
。当一个服务刷新数据的时候,他会把这个消息放入到Topic
中,这样其它监听到同一Topic
的服务就能得到通知,然后去更新自身的配置。2. 能干嘛
Spring Cloud Bus
能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、时间推送等,也可以当作微服务间的通信通道。1. 建Module
Module
的名称为cloud-config-client-3366
。2. 改POM
<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>
3. 改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: "*"
4. 主启动
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);
}
}
5. 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 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;
}
}
/bus/refresh
,而刷新所有客户端的配置ConfigServer
的/bus/refresh
端点,而刷新所有客户端的配置(更加推荐)(1)、第二种架构方式更合适的原因
1. 给cloud-config-center-3344配置中心服务端添加消息总线支持
(1)、POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)、新添加的YML内容
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
2. 给cloud-config-center-3355客户端添加消息总线支持
(1)、POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)、新添加的YML内容
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
3. 给cloud-config-center-3366客户端添加消息总线支持
(1)、POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)、新添加的YML内容
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
curl -X POST "http://localhost:3344/actuator/bus-refresh"
之后的测试结果如下2.2
小节上述环境,只通知3355
不通知3366
的请求如下:curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_56727438/article/details/122447009
内容来源于网络,如有侵权,请联系作者删除!