1 server依赖
4.0.0 org.sselab configserver 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.4.0.RELEASE org.springframework.cloud spring-cloud-dependencies Brixton.RELEASE pom import org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-config-server org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
2 server实现
package org.sselab; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.config.server.EnableConfigServer; /** * Created by pinker on 2016/10/31. */ @SpringBootApplication @EnableConfigServer public class application { public static void main(String[] args) { new SpringApplicationBuilder(application.class).web(true).run(args); } }
server: port: 8888 spring: application: name: config-server cloud: config: server: git: uri: https://git.oschina.net/xd03122049/springboot-config search-paths: config-repo username: xd03122049 password: 85523970
3 server结果
4 client配置
4.0.0 org.sselab configclient 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.4.0.RELEASE org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-dependencies Brixton.RELEASE pom import org.springframework.boot spring-boot-maven-plugin
5 client实现
spring.application.name=pinker spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.uri=http://localhost:8888/ server.port=7002
package org.sselab; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; /** * Created by pinker on 2016/10/31. */ @SpringBootApplication public class application { public static void main(String[] args) { new SpringApplicationBuilder(application.class).web(true).run(args); } }
package org.sselab.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by pinker on 2016/10/31. */ @RefreshScope @RestController public class TestController { @Value("${from}") private String from; @GetMapping("/from") public String from(){ return this.from; } public void setFrom(String from) { this.from = from; } public String getFrom() { return from; } }
6 client 结果
然后将server和client都注册到注册中心,略