1、Config-Server使用之前的最基本的Config-Server的配置
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: git@github.com:wangchunself/ymlforconfig.git
启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
2、手动刷新只需要在Config-Client端进行特殊的配置即可
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
bootstrap.yml
此处要暴露出/refresh端点
server:
8081
spring:
cloud:
config:
uri: http://localhost:8080
label: master
profile: bar
application:
name: foo
management:
endpoints:
web:
exposure:
include: refresh
启动类
@SpringBootApplication
public class ConfigClientRefreshApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientRefreshApplication.class, args);
}
}
调用类
添加@RefreshScope注解,这个注解是能够保证,我们刷新不会影响到正在进行的业务
不能和@Configration在同一个类的注解上
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
String profile;
@GetMapping("/getProfile")
public String getProfile(){
return this.profile;
}
}
3、测试
启动ConfigServer和Client,
访问http://localhost:8081/getProfile
修改https://github.com/wangchunself/ymlforconfig仓库下,foo-bar.yml文件为
post请求http://localhost:8081/actuator/refresh(图中结果表示,profile属性更改刷新了)