首先需要在Discovery Service章节中的创建Discovery Server服务
创建Config Server服务
//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Config Server,Actuactor
spring-cloud-config-server
spring-cloud-starter-netflix-eureka-server
spring-boot-starter-actuator
//配置启动类注解EnableConfigServer
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
//配置文件
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/bu6030/scf-config-repository.git
spring.application.name=config-server
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
启动时需要默认的SpringCloudserver已经启动
通过url可以找到对应的配置内容
//配置的Endpoint规则几种方式
GET /{application}/{profile}[/{label}]
/myap/dev/master,/myapp/prod/v2,/myapp/default
GET /{application}-{profile}.(yml | properties)
/myapp-dev.yml,/myapp-prod.properties,/myapp-default.properties
GET /{label}-{application}-{profile}.(yml | properties)
/master/myapp-dev.yml,/v2/myapp-prod.properties,/master/myapp-default.properties
//http://localhost:8888/config-client-app.yml返回内容:
some:
other:
property: global
property: app specific overridden value
//http://localhost:8888/config-client-app.properties返回内容
some.other.property: global
some.property: app specific overridden value
//http://localhost:8888/config-client-app/prod返回内容
{
"name": "config-client-app",
"profiles": [
"prod"
],
"label": null,
"version": "1cb37c9ba1fa4c17d2ae7489c2e592eba1688547",
"state": null,
"propertySources": [
{
"name": "https://github.com/bu6030/scf-config-repository.git/config-client-app-prod.properties",
"source": {
"some.property": "profile specific value",
"some.other.property": "profile specific value"
}
},
{
"name": "https://github.com/bu6030/scf-config-repository.git/config-client-app.properties",
"source": {
"some.property": "app specific overridden value"
}
},
{
"name": "https://github.com/bu6030/scf-config-repository.git/application.properties",
"source": {
"some.property": "global",
"some.other.property": "global"
}
}
]
}
//http://localhost:8888/config-client-app/default返回内容
{
"name": "config-client-app",
"profiles": [
"default"
],
"label": null,
"version": "1cb37c9ba1fa4c17d2ae7489c2e592eba1688547",
"state": null,
"propertySources": [
{
"name": "https://github.com/bu6030/scf-config-repository.git/config-client-app.properties",
"source": {
"some.property": "app specific overridden value"
}
},
{
"name": "https://github.com/bu6030/scf-config-repository.git/application.properties",
"source": {
"some.property": "global",
"some.other.property": "global"
}
}
]
}
配置config client
//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Config Client,Actuactor
sspring-cloud-starter-config
spring-cloud-starter-netflix-eureka-client
spring-boot-starter-actuator
//配置启动类注解EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConfigClientAppApplication {
@Autowired
private ConfigClientAppConfiguration properties;
@Value("${some.other.property}")
private String someOtherPorperty;
public static void main(String[] args) {
SpringApplication.run(ConfigClientAppApplication.class, args);
}
@RequestMapping("/")
public String printConfig(){
StringBuffer sb = new StringBuffer();
sb.append(properties.getProperty());
sb.append(" || ");
sb.append(someOtherPorperty);
return sb.toString();
}
}
@Component
@ConfigurationProperties(prefix="some")
public class ConfigClientAppConfiguration {
private String property;
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
//配置文件
server.port=8080
spring.application.name=config-client-app
spring.cloud.config.discovery.enabled=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
修改配置文件后自动刷新通过调用http://localhost:8080/actuato...刷新接口
配置文件需要增加
management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=refresh,health,bus-refresh
//启动类增加RefreshScope后,@Value也可以刷新
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class ConfigClientAppApplication {
@Autowired
private ConfigClientAppConfiguration properties;
@Value("${some.other.property}")
private String someOtherPorperty;
public static void main(String[] args) {
SpringApplication.run(ConfigClientAppApplication.class, args);
}
@RequestMapping("/")
public String printConfig(){
StringBuffer sb = new StringBuffer();
sb.append(properties.getProperty());
sb.append(" || ");
sb.append(someOtherPorperty);
return sb.toString();
}
}
//刷新服务的方式,
http://localhost:8080/actuator/refresh
http://localhost:8080/actuator/bus-refresh
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。