1.分布式架構(gòu)
1.1傳統(tǒng)架構(gòu)
1.2分布式架構(gòu)
1.3項目架構(gòu)
springcloud
版本 Hoxton.SR6
├── config-sever
│ ├── pom.xml
│ ├── src
│ │ ├── main
│ │ │ ├── java
│ │ │ │ └── com
│ │ │ │ └── zzm
│ │ │ │ └── zcd
│ │ │ │ └── ConfigServerApplication.java
│ │ │ └── resources
│ │ │ └── application.yml
├── eureka-server
│ ├── eureka-server.iml
│ ├── pom.xml
│ ├── src
│ │ ├── main
│ │ │ ├── java
│ │ │ │ └── com
│ │ │ │ └── zzm
│ │ │ │ └── zcd
│ │ │ │ └── ConfigEurekaServer.java
│ │ │ └── resources
│ │ │ └── application.yml
├── pom.xml
├── spring-cloud-config-demo.iml
└── system-service
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── zzm
│ │ │ └── zcd
│ │ │ ├── SystemServiceApplication.java
│ │ │ ├── config
│ │ │ │ └── ConfigInfoProperties.java
│ │ │ └── controller
│ │ │ └── HelloController.java
│ │ └── resources
│ │ ├── application.yml
│ │ └── bootstrap.yml
2.Spring Cloud 配置服務(wù)器
-
Spring Cloud Config Server
Spring Cloud 配置服務(wù)器器提供分布式葱峡、動態(tài)化集中管理理應(yīng)?用配置信息的能?
- 構(gòu)建 Spring Cloud 配置服務(wù)器
? @EnableConfigServer
啟動類
```java
/**
- 服務(wù)配置中心
*/
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
```
配置文件
server:
port: 7071
spring:
cloud:
config:
server:
git:
#git倉庫文件地址
uri: https://gitee.com/develop-alan-knowledge/zcd-config-file.git
username: 1299076979@qq.com
password: zcd123456
#搜索文件夾配置文件
search-paths: demo-config
application:
name: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:7070/eureka/
healthcheck:
enabled: true
3.Spring Cloud 配置客戶端
- Spring Cloud Config Client
? Spring Cloud 配置客戶端提供連接 Spring Cloud 服務(wù)端浪南,并且獲取訂閱的配置信息
-
配置Spring Cloud 配置客戶端
- 創(chuàng)建 bootstrap.yml 或者 bootstrap.properties
配置文件服務(wù)器地址一定要放在bootstrap.yml
spring: cloud: config: #配置服務(wù)器地址 uri: http://localhost:7071 label: master name: application profile: dev
- 配置 spring.cloud.config.* 信息
4.動態(tài)配置屬性Bean
-
@RefreshScope
rest層需要加入
@RefreshScope
注解才能獲取最新配置@RefreshScope @RestController public class HelloController { @Autowired private ConfigInfoProperties configInfoProperties; @GetMapping("getInfo") public String getInfo(){ return configInfoProperties.getConfig(); } }
配置信息
@Component
@ConfigurationProperties(prefix = "com.springcloud")
public class ConfigInfoProperties {
private String config;
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
}
-
/refresh Endpoint
? git文件中配置文件修改需要更新到客戶端需要執(zhí)行下面
Endpoint
http://localhost:端口/actuator/refresh
執(zhí)行上面
Endpoint
需要打開Endpoint
配置management: endpoints: web: exposure: include: '*' endpoint: health: show-details: always
-
ContextRefresher
org.springframework.cloud.endpoint.RefreshEndpoint源碼
@Endpoint(id = "refresh") public class RefreshEndpoint { private ContextRefresher contextRefresher; public RefreshEndpoint(ContextRefresher contextRefresher) { this.contextRefresher = contextRefresher; } @WriteOperation public Collection<String> refresh() { Set<String> keys = this.contextRefresher.refresh(); return keys; } }
actuator/refresh
執(zhí)行是this.contextRefresher.refresh()
方法