springcloud~配置中心的使用

配置中心作为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语法!

 

/****************************************************************************************
 * 配置服务的路劲规则:
 *
 * /{application}/{profile}[/{label}]
 * /{application}-{profile}.yml
 * /{label}/{application}-{profile}.yml
 * /{application}-{profile}.properties
 * /{label}/{application}-{profile}.properties
 ****************************************************************************************/

 

仓储如图:

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

访问: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 #注意这里的email是指配置中心git仓库里yml文件的application的部分
  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;
  }
}
复制代码

结果如图:

感谢各位的阅读!

 

posted @   张占岭  阅读(3373)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2014-05-15 MVVM架构~knockoutjs系列之Mapping插件为对象添加ko属性
2012-05-15 Redis学习笔记~Redis提供的五种数据结构
点击右上角即可分享
微信分享提示