spring cloud - 配置中心

spring cloud 配置中心

  1. 配置中心的搭建和简单使用

(1).为了更贴近生产,我们首先配置Host

127.0.0.1 config-server

(2).准备个配置文件

// 配置文件,命名规范为项目名称-环境名称.properties,本文在git仓库:https://github.com/eacdy/spring-cloud-study/中,新建目录config-repo,创建以下几个文件

microservice-config-client-dev.properties
microservice-config-client.properties

// 其中在:microservice-config-client-dev.properties 文件中添加如下内容:

profile=dev

(3).服务器端代码示例


//gradle依赖如下

compile "org.springframework.cloud:spring-cloud-config-server:Brixton.SR5"

//maven依赖如下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>Brixton.SR5</version>
</dependency>
// 启动类ConfigServerApplication.java

/**
 * 通过@EnableConfigServer注解激活配置服务.
 * 说明:
 * 在application.yml中有个git.uri的配置,目前配置的是https://github.com/eacdy/spring-cloud-study/
 * 获取git上的资源信息遵循如下规则:
 * /{application}/{profile}[/{label}]
 * /{application}-{profile}.yml
 * /{label}/{application}-{profile}.yml
 * /{application}-{profile}.properties
 * /{label}/{application}-{profile}.properties
 *
 * 例如本例:可使用以下路径来访问microservice-config-client-dev.properties:
 * http://localhost:8040/microservice-config-client-dev.properties
 * http://localhost:8040/microservice-config-client/dev
 * ...
 * @author eacdy
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}
// 配置文件:application.yml

server:
  port: 8040
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/eacdy/spring-cloud-study/     # 配置git仓库的地址
          search-paths: config-repo                             # git仓库地址下的相对地址,可以配置多个,用,分割。
          username:                                             # git仓库的账号
          password:                                             # git仓库的密码

(4).测试工作


// 获取git上的资源信息遵循如下规则 
{application}/{profile}[/{label}] 
{application}-{profile}.yml
{label}/{application}-{profile}.yml
{application}-{profile}.properties
{label}/{application}-{profile}.properties

// 例如本例:可使用以下路径来访问microservice-config-client-dev.properties:

http://localhost:8040/microservice-config-client-dev.properties http://localhost:8040/microservice-config-client/dev

// 按照上文,我们成功搭建了Config Server,并测试能够正常获取到git仓库中的配置信息。

(5).配置服务客户端示例


//gradle依赖如下

compile "org.springframework.cloud:spring-cloud-config-server:Brixton.SR5"

//maven依赖如下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>Brixton.SR5</version>
</dependency>
// 启动类ConfigClientApplication.java

@SpringBootApplication
public class ConfigClientApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigClientApplication.class, args);
  }
}

// 编写测试Controller

/**
 * 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
 * @author eacdy
 */
@RestController
@RefreshScope
public class ConfigClientController {
  @Value("${profile}")
  private String profile;

  @GetMapping("/hello")
  public String hello() {
    return this.profile;
  }
}

// 配置文件:application.yml

server:
  port: 8041

// 配置文件bootstrap.yml(为什么要使用bootstrap.yml而不直接放在application.yml中的原因见注意点)

spring:
  application:
    name: microservice-config-client    # 对应microservice-config-server所获取的配置文件的{application}
  cloud:
    config:
      uri: http://config-server:8040/
      profile: dev                      # 指定profile,对应microservice-config-server所获取的配置文件中的{profile} 
      label: master                     # 指定git仓库的分支,对应microservice-config-server所获取的配置文件的{label}

// 启动,并访问:http://localhost:8041/hello ,我们会发现此时可以显示git仓库中配置文件的内容:   dev   

(6).配置内容的热加载

// 我们将microservice-config-client-dev.properties中值改为

profile=abcd
// 并提交到git仓库。

// 然后使用命令(本文使用的是curl,Linux和Windows都有curl工具,当然也可以借助其他工具,例如Postman等):

curl  -X POST http://localhost:8041/refresh

// 然后再次访问http://localhost:8041/hello ,将会看到:abcd ,说明配置已经刷新。

(7).注意点

// client需要添加以下依赖,否则访问/refresh将会得到404:

//gradle依赖如下

compile "org.springframework.cloud:spring-boot-starter-actuator:Brixton.SR5"

//maven依赖如下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>Brixton.SR5</version>
</dependency>

// client的controller需要添加@RefreshScope注解,否则配置无法刷新。

// 本文的bootstrap.yml文件中的内容不能放到application.yml中,否则config部分无法被加载,因为config部分的配置先于application.yml被加载,而bootstrap.yml中的配置会先于application.yml加载,

// Config Server也可以支持本地存储或svn而不使用git,相对较为简单,故而本文不作赘述,有兴趣的可以自行阅读Spring Cloud的文档。

(8).描述来源于:http://book.itmuch.com/2%20Spring%20Cloud/2.5%20%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83.html

  1. 配置服务与注册中心联合使用
// 在生产环境中,我们可能会将Config Server 与 Eureka等注册中心联合使用(注意:目前Spring Cloud只支持与Eureka及Consul联合使用,不支持与Zookeeper联合使用),下面讲解如何将Config Server与 Eureka 联合使用。

(1).准备工作

// 启动服务microservice-discovery-eureka ;

// 和上文一样,准备好几个配置文件,命名规范为项目名称-环境名称.properties ,本文使用的名称是microservice-config-client-eureka-dev.properties 。

(2).服务器端代码示例

//gradle依赖如下

compile "org.springframework.cloud:spring-cloud-config-server:Brixton.SR5"
compile "org.springframework.cloud:spring-cloud-starter-eureka:Brixton.SR5"

//maven依赖如下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>Brixton.SR5</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>Brixton.SR5</version>
</dependency>

// 启动类:ConfigServerEurekaApplication.java

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerEurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerEurekaApplication.class, args);
  }
}

// 配置文件:application.yml

erver:
  port: 8050
spring:
  application:
    name: microservice-config-server-eureka
  cloud:
    config:
      server:
        git:
          uri: https://github.com/eacdy/spring-cloud-study/
          search-paths: config-repo
          username: 
          password: 
eureka:
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/

(3).客户端示例

//gradle依赖如下

compile "org.springframework.cloud:spring-cloud-config-server:Brixton.SR5"
compile "org.springframework.cloud:spring-cloud-starter-eureka:Brixton.SR5"
compile "org.springframework.cloud:spring-boot-starter-actuator:1.4.0.RELEASE"

//maven依赖如下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>Brixton.SR5</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>Brixton.SR5</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>1.4.0.RELEASE</version>
</dependency>
// 启动类:ConfigClientEurekaApplication.java

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientEurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigClientEurekaApplication.class, args);
  }
}

// 编写测试Controller

/**
 * 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
 * @author eacdy
 */
@RestController
@RefreshScope
public class ConfigClientController {
  @Value("${profile}")
  private String profile;

  @GetMapping("/hello")
  public String hello() {
    return this.profile;
  }
}

// 配置文件:application.yml

server:
  port: 8051

// 配置文件:bootstrap.yml

pring:
  application:
    name: microservice-config-client-eureka
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        enabled: true                                 # 默认false,设为true表示使用注册中心中的configserver配置而不自己配置configserver的uri
        serviceId: microservice-config-server-eureka  # 指定config server在服务发现中的serviceId,默认为:configserver
eureka:
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/

# 参考文档:https://github.com/spring-cloud/spring-cloud-config/blob/master/docs/src/main/asciidoc/spring-cloud-config.adoc#discovery-first-bootstrap

// 从示例代码我们发现,想要将Config Server 与 注册中心联合使用,只需要在客户端侧配置spring.cloud.config.discovery.enabled=true 和 spring.cloud.config.discovery.serviceId 两个配置项即可。Eureka的配置前文有讲到过,如有疑问,详见服务发现的相关章节。

// 注意:当服务发现是Eureka 及 Consul 时,Config Server支持与之联合使用;如果是Zookeeper 做服务发现,目前不支持与之联合使用。

(4).描述来源于:http://book.itmuch.com/2%20Spring%20Cloud/2.5%20%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值