首先须要在Discovery Service章节中的创立Discovery Server服务
创立Config Server服务
//导入包,理论是通过Spring Initializr导入的,Eureka Discovery,Config Server,Actuactorspring-cloud-config-serverspring-cloud-starter-netflix-eureka-serverspring-boot-starter-actuator//配置启动类注解EnableConfigServer@SpringBootApplication@EnableConfigServer@EnableDiscoveryClientpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}//配置文件server.port=8888spring.cloud.config.server.git.uri=https://github.com/bu6030/scf-config-repository.gitspring.application.name=config-servereureka.client.service-url.defaultZone=http://localhost:8761/eureka
启动时须要默认的SpringCloudserver曾经启动
通过url能够找到对应的配置内容
//配置的Endpoint规定几种形式GET /{application}/{profile}[/{label}]/myap/dev/master,/myapp/prod/v2,/myapp/defaultGET /{application}-{profile}.(yml | properties)/myapp-dev.yml,/myapp-prod.properties,/myapp-default.propertiesGET /{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: globalsome.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,Actuactorsspring-cloud-starter-configspring-cloud-starter-netflix-eureka-clientspring-boot-starter-actuator//配置启动类注解EnableConfigServer@SpringBootApplication@EnableDiscoveryClient@RestControllerpublic 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=8080spring.application.name=config-client-appspring.cloud.config.discovery.enabled=trueeureka.client.service-url.defaultZone=http://localhost:8761/eureka
批改配置文件后主动刷新通过调用http://localhost:8080/actuato...刷新接口
配置文件须要减少
management.endpoints.enabled-by-default=truemanagement.endpoints.web.exposure.include=refresh,health,bus-refresh//启动类减少RefreshScope后,@Value也能够刷新@SpringBootApplication@EnableDiscoveryClient@RestController@RefreshScopepublic 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/refreshhttp://localhost:8080/actuator/bus-refresh