- springCloud学习记录
- SpringCloud Config(分布式配置中心)
- 服务端
- pom
- yml
- 启动类
- 客户端
- pom
- yml
- 启动类
- 方法类获取配置信息
用于通过git配置数据访问等信息,不需要重启服务器。分为服务端和客户端,服务端负责读取git上的配置,客户端负责获取服务端获取到的配置;git上的配置文件需要按官方的格式配置不然不起效
服务端 pom
org.springframework.cloud
spring-cloud-config-server
yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
# 服务地址相关配置
config:
server:
git:
uri: git@github.com:leelovejava/springcloud-config.git
search-paths:
- spring-config
label: master
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
启动类
@SpringBootApplication
@EnableConfigServer // 配置中心服务
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}
客户端
pom
ymlorg.springframework.cloud spring-cloud-starter-configorg.springframework.boot spring-boot-starter-actuator
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master # 分支名称
name: config #配置文件名称
profile: dev # 读取的后缀,上述三个综合,为master分支上的config-dev.yml的配置文件被读取,http://config-3344.com:3344/master/config-dev.yml
uri: http://config-3344.com:3344 #配置中心的地址;这里做了host配置
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
# 配置监控;服务器改变后,进行监听
management:
endpoints:
web:
exposure:
include: "*"
启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
方法类获取配置信息
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
配置文件修改后需要同步-手动更新(自动更新需要用Bus):
curl -X POST “http://localhost:3355/actuator/refresh”



