配置中心作为springcloud里最底层的框架,所发挥的意思是举足轻重的,所以的组件的配置信息都可以通过springcloud config来管理,它会把配置信息分布式的存储到git上,所以信息安全这块可以放心,其它应用程序在更新配置时,直接在远程GIT仓库更新即可,而且更新后自动同步到对应的程序里,不需要重启这个应用程序!
依赖包
dependencies { compile('org.springframework.cloud:spring-cloud-config-server', 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' ) testCompile('org.springframework.boot:spring-boot-starter-test') }
配置项
server: port: 8200 spring: application: name: lind-config-server cloud: config: server: git: uri: https://github.com/bfyxzls/lindconfig.repo.git/ search-paths: config-repo username: bfyxzls@sina.com password: 纟 eureka: instance: prefer-ip-address: true instance-id: ${spring.application.name}:${server.port} client: serviceUrl: defaultZone: http://localhost:8761/eureka/
启动代码
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication class Application { public static void main(String[] args) { // demo http://localhost://8200/email-svt.yml SpringApplication.run(Application.class, args); } }
在github上添加对应的仓库,客户端的配置文件将会同步到GIT仓库,建议配置文件采用yml语法!
查看配置中心服务端是否正常
访问:http://localhost:8200/email-svt.yml
依赖包
dependencies { compile('org.springframework.boot:spring-boot-starter-web', 'org.springframework.cloud:spring-cloud-starter-config', 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') testCompile('org.springframework.boot:spring-boot-starter-test') }
配置项
spring: application: name: email-client cloud: config: uri: http://localhost:8200 server: port: 8300 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
启动项
@EnableEurekaClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
我们可以在客户端使用$Value注解完成配置文件的读取!
@RestController public class HomeController { @Value("${server.port}") // git配置文件里的key String serverPort; @RequestMapping("/") public String index() { return "serverPort=" + serverPort; } }
感谢各位的阅读!