配置config配置中心
config-server
新建model,叫service-config-server8688,依赖添加config-server
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置文件
server:
port: 8688
spring:
application:
name: service-config-server8688
cloud:
config:
server:
git:
#git仓库路径
uri: https://github.com/zgl-1/cloud-config.git
search-paths: cloud-config
eureka:
client:
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/
git仓库上面有三个配置文件
主启动类上面加注解
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
然后启动服务,测试http://localhost:8688/master/application-dev.yml,结果显示“servernameversion: 1”,服务连接git仓库中心成功
config-client
新建model,叫service-config-client8689,添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置文件bootstrap.yml
server:
port: 8689
spring:
application:
name: service-config-client8689
cloud:
config:
label: master
name: application
profile: dev
# 客户端直接去服务端找
uri: http://localhost:8688
eureka:
client:
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/
新建controller类
@RestController
public class HelloController {
@Value("${servernameversion}")
public String servernameversion;
@RequestMapping("/hello")
public String get(){
return servernameversion;
}
}
启动服务,测试http://localhost:8689/hello,结果显示“1”,客户端获取成功
动态刷新配置
如果说git仓库更改了,但是客户端肯定不知道,所以就需要刷新配置,这里就介绍的是手动的刷新配置
需要添加actuator的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置文件添加配置
management:
endpoints:
web:
exposure:
include: "*"
controller类添加刷新注解
@RestController
@RefreshScope//刷新注解
public class HelloController {
@Value("${servernameversion}")
public String servernameversion;
@RequestMapping("/hello")
public String get(){
return servernameversion;
}
}
启动服务
先测试http://localhost:8689/hello,肯定获取的是“1”
现在去更改git上的配置文件,将值改为2
在测试http://localhost:8689/hello,结果还是显示的“1”
所以现在就需要我们手动的刷新一下,执行一下post请求http://localhost:8689/actuator/refresh,
然后再次测试http://localhost:8689/hello,结果就会显示“2”了