配置中心作为springcloud里最底层的框架,所发挥的意思是举足轻重的,所以的组件的配置信息都可以通过springcloud config来管理,它会把配置信息分布式的存储到git上,所以信息安全这块可以放心,其它应用程序在更新配置时,直接在远程GIT仓库更新即可,而且更新后自动同步到对应的程序里,不需要重启这个应用程序!

配置服务-服务端,最底层应用

依赖包

  1. dependencies {
  2. compile('org.springframework.cloud:spring-cloud-config-server',
  3. 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
  4. )
  5. testCompile('org.springframework.boot:spring-boot-starter-test')
  6. }

配置项

  1. server:
  2. port: 8200
  3. spring:
  4. application:
  5. name: lind-config-server
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://github.com/bfyxzls/lindconfig.repo.git/
  11. search-paths: config-repo
  12. username: bfyxzls@sina.com
  13. password:
  14. eureka:
  15. instance:
  16. prefer-ip-address: true
  17. instance-id: ${spring.application.name}:${server.port}
  18. client:
  19. serviceUrl:
  20. defaultZone: http://localhost:8761/eureka/

启动代码

  1. @EnableDiscoveryClient
  2. @EnableConfigServer
  3. @SpringBootApplication
  4. class Application {
  5.  
  6. public static void main(String[] args) {
  7. // demo http://localhost://8200/email-svt.yml
  8. SpringApplication.run(Application.class, args);
  9. }
  10. }

在github上添加对应的仓库,客户端的配置文件将会同步到GIT仓库,建议配置文件采用yml语法!

  1. /****************************************************************************************
  2. * 配置服务的路劲规则:
  3. *
  4. * /{application}/{profile}[/{label}]
  5. * /{application}-{profile}.yml
  6. * /{label}/{application}-{profile}.yml
  7. * /{application}-{profile}.properties
  8. * /{label}/{application}-{profile}.properties
  9. ****************************************************************************************/

仓储如图:

查看配置中心服务端是否正常

访问:http://localhost:8200/email-svt.yml

配置中心-客户端,遍及在所有应用中

依赖包

  1. dependencies {
  2. compile('org.springframework.boot:spring-boot-starter-web',
  3. 'org.springframework.cloud:spring-cloud-starter-config',
  4. 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
  5. testCompile('org.springframework.boot:spring-boot-starter-test')
  6. }

配置项

  1. spring:
  2. application:
  3. name: email #注意这里的email是指配置中心git仓库里yml文件的application的部分
  4. cloud:
  5. config:
  6. uri: http://localhost:8200
  7. server:
  8. port: 8300
  9.  
  10. eureka:
  11. client:
  12. serviceUrl:
  13. defaultZone: http://localhost:8761/eureka/

启动项

  1. @EnableEurekaClient
  2. @SpringBootApplication
  3. public class Application {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run(Application.class, args);
  7. }
  8. }

我们可以在客户端使用$Value注解完成配置文件的读取!

  1. @RestController
  2. public class HomeController {
  3. @Value("${server.port}") // git配置文件里的key
  4. String serverPort;
  5.  
  6. @RequestMapping("/")
  7. public String index() {
  8. return "serverPort=" + serverPort;
  9. }
  10. }

结果如图:

感谢各位的阅读!

springcloud~配置中心的使用的更多相关文章

  1. SpringCloud配置中心实战

    SpringCloud配置中心实战 1.统一配置中心(Config) 1.1 Spring项目配置加载顺序 1.2 配置规则详解 1.3 Git仓库配置 1.3.1 使用占位符 1.3.2 模式匹配 ...

  2. springcloud配置中心

    SpringCloud Config简介 Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持 ...

  3. Consul作为SpringCloud配置中心

    一.背景介绍 在分布式系统中动态配置中,可以避免重复重启服务,动态更改服务参数等.一句话非常重要. 另外一篇文章也是这样说的,哈哈. Consul 作为Spring 推荐的分布式调度系统其也具备配置中 ...

  4. SpringCloud配置中心config

    1,配置中心可以用zookeeper来实现,也可以用apllo 来实现,springcloud 也自带了配置中心config Apollo 实现分布式配置中心 zookeeper:实现分布式配置中心, ...

  5. springcloud~配置中心实例搭建

    server端 build.gradle相关 dependencies { compile('org.springframework.cloud:spring-cloud-config-server' ...

  6. springcloud配置中心客户端配置遇到的坑

    1. 出错信息如下: 在启动配置中心的客户端时,报以下错误信息: Caused by: java.lang.IllegalArgumentException: Could not resolve pl ...

  7. SpringCloud配置中心集成Gitlab(十五)

    一 开始配置config服务 config-server pom.xml <dependency> <groupId>org.springframework.cloud< ...

  8. springcloud~配置中心~对敏感信息加密

    简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...

  9. springcloud(七):配置中心svn示例和refresh

    上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...

随机推荐

  1. 把封装脚本做成jar包

    前提: eclipse, selenium, maven 把二次封装过的脚本做成jar包, 这样可以在新建工程里也调用封装过的方法. 实现步骤: 1. project 右键 => maven = ...

  2. com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定

    转自: http://blog.csdn.net/superdangbo/article/details/78732700 com.mysql.jdbc.Driver 和 com.mysql.cj.j ...

  3. spring boot 搭建web项目常见五种返回形式

    在web项目中一般常见的五种返回形式: 返回页面,使用模板引擎,spring boot推荐使用thymeleaf,类似的还有freemarker等. 返回字符串(json),一般用于完全的前后端分离开 ...

  4. java.lang.IllegalArgumentException异常 数据库别名问题

    java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expect ...

  5. BZOJ_3210_花神的浇花集会_切比雪夫距离

    BZOJ_3210_花神的浇花集会_切比雪夫距离 Description 在花老师的指导下,每周4都有一个集会活动,俗称“浇水”活动. 具体浇水活动详情请见BZOJ3153 但这不是重点 花神出了好多 ...

  6. 在线数据库表(sql语句)生成java实体类工具

    相信每个做java开发的读者,都接触过SQL建表语句,尤其是在项目开发初期,因为数据库是项目的基石. 在现代项目开发中,出现了许多ORM框架,通过简单的实体映射,即可实现与数据库的交互,然而我们最初设 ...

  7. Python+Appium 获取 toast 文本值方法的封装

    获取toast内容方法封装如下: def get_Toast(self,message): #查找toast值 ''' method explain:查找toast的值,与find_Toast实现方法 ...

  8. 用Python学分析 - 二项分布

    二项分布(Binomial Distribution)对Bernoulli试验序列的n次序列,结局A出现的次数x的概率分布服从二项分布- 两分类变量并非一定会服从二项分布- 模拟伯努利试验中n次独立的 ...

  9. RabbitMQ指南之一:"Hello World!"

    为什么要使用MQ消息中间件?它解决了什么问题?关于为什么要使用消息中间件?消息中间件是如何做到同步变异步.流量削锋.应用解耦的?网上已经有很多说明,我这里就不再说明了,读者可以参考(https://w ...

  10. mip-link 组件功能升级说明

    背景描述 某个页面被多少页面引用(在其他页面上有指向这个页面的 a 标签),是搜索引擎判断这个页面价值的其中一个因子.这里的搜索引擎不只是指百度,还包括国内外其他的搜索引擎. MIP 在最初设计 MI ...