目录
配置中心
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>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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/用户名/springcloud-config.git #github仓库名字,对应哪个地址
search-paths: #搜索目录
- springcloud-config #哪个仓库
force-pull: true
username: 用户名
password: 密码
label: mian #哪个分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
主启动
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args){
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
客户端
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>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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
yml
注意,取名为bootstrap.yml优先级高于application.yml
server: port: 3355 spring: application: name: config-client cloud: config: #客户端配置 label: master #分支名称 name: config #配置文件名称 profile: dev #读取的后缀的名称 结合起来就是读取 master/config-dev.yml的配置文件 uri: http://localhost:3344 #配置中心地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka
主启动
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args){
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
测试
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/getConfigInfo")
public String getConfigInfo(){
return configInfo;
}
}
存在问题,当GitHub仓库里面的配置文件修改以后,配置中心会随之变化,但是客户端并不会改变,必须要重启客户端的服务才能刷新出来,针对此问题,现阶段可以使用config动态刷新手动版,想要实现自动更新需要使用Bus
1、客户端的pom引入actuator
2、客户端修改yml,添加:
management: #暴露监控端点
endpoints:
web:
exposure:
include: "*"
3、控制器上添加注解
@RefreshScope //刷新
4、发送post请求
curl -X POST "http://localhost:3355/actuator/refresh"