1.配置中心服务端的搭建
- 创建模块cloud-config-center3344
- 添加坐标
<!--这是分布式的配置中心-->
<dependencies>
<!--需要引入配置中心的坐标-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--引入eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 编写application.yml
server:
port: 3344
spring:
application:
name: config-center
cloud:
config:
server:
git:
uri: 你的github地址
search-paths:
- springcloud-config
skip-ssl-validation: true
label: master
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
instance:
prefer-ip-address: true
instance-id: config-center
- 编写启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args) ;
}
}
- 测试
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
2.配置中心客户端的搭建
- 创建模块cloud-config-client-3355
- 引入坐标
<dependencies>
<!--导入spring-boot的web模块的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入spring-boot的测试模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--引入eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--客户端的配置信息-->
<!--读取bootstrap配置文件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
- 编写bootstrap.yml配置文件
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
uri: http://config-3344.com:3344
label: master
profile: dev
name: config
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
instance:
prefer-ip-address: true
instance-id: config-client3355
management:
endpoints:
web:
exposure:
include: "*" # 暴露当前端口共别人调用
- 编写controller
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo ;
@RequestMapping("/configInfo")
public String getConfigInfo() {
return configInfo ;
}
}
- 编写主配置类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args) ;
}
}
- 测试
3.手动刷新
问题描述:当手动修改了github上面的配置文件之后,发现服务端,可以感知到配置的变化,客户端不能实现动态的刷新的问题?
客户端解决方案如下:
- 添加坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 修改bootstrap的配置,添加暴露端口
management:
endpoints:
web:
exposure:
include: "*" # 暴露当前端口共别人调用
- 在控制器上面添加@RefreshScope
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo ;
@RequestMapping("/configInfo")
public String getConfigInfo() {
return configInfo ;
}
}
- 最后手动的发送一个post请求
curl -X POST "http://localhost:3355/actuator/refresh"
- 测试