spring cloud 版本:Greenwich.RELEASE
一、config server,依赖引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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>
application.yml配置
server:
port: 7010
servlet:
context-path: /config-server
eureka:
client:
healthcheck:
enabled: true
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
bootstrap.yml配置
spring:
application:
name: "canna-cloud-module-eureka-server"
cloud:
config:
uri: http://localhost:8888
server:
git:
search-paths: config
username: ""
password: ""
uri:""
label: master
name: config-client
profile: prod
启动类
@SpringBootApplication
@EnableConfigServer
@RestController
public class ConfigServerApplication {
@RequestMapping("/hello")
public String home() {
return "eureka server start success";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
二、config client使用
application.yml配置
server:
port: 7020
servlet:
context-path: /config-client
management: #actuator
server:
port: 7021
endpoints:
web:
base-path: /actuator #默认是/actuator 前缀,可以在这里修改
exposure:
include: "*" #打开全部请求端点
# include: refresh,health,info #打开部分
eureka:
client:
healthcheck:
enabled: true
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
bootstrap.yml配置
spring:
application:
name: "canna-cloud-module-eureka-server"
cloud:
config:
uri: http://localhost:7010/config-server
label: master
profile: dev
name: config-client
启动类
@SpringBootApplication
public class ConfigClientServerApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ConfigClientServerApplication.class, args);
}
}
配置刷新测试Controller。注意在需要的地方添加注解:@RefreshScope
@RestController
@RefreshScope
public class ConfigController {
@Value("${myww}")
String myww;
@RequestMapping("/hello")
public String home() {
return "config client server start success, value:" + myww;
}
}
调用postman,访问POST方法:
http://localhost:7021/actuator/refresh
再请求controller即可