Spring Cloud学习--配置中心(Config)

Spring Cloud学习--配置中心(Config)

一、 Spring Cloud Config简介

微服务要实现集中管理微服务配置、不同环境不同配置、运行期间也可动态调整、配置修改后可以自动更新的需求,Spring Cloud Config同时满足了以上要求。Spring Cloud Config 分为Config Server和Config Client两部分,是一个可以横向扩展,集中式的配置服务器, 默认使用Git存储配置内容。

Spring Cloud Config 原理图如图所示:

这里写图片描述

二、 编写 Config Server

1.先在github上新建一个仓库,添加配置文件。

这里写图片描述

2.新建一个spring boot 项目,pom.xml中添加如下依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3.启动类上添加@EnableConfigServer注解,表示这个类是一个Config Server

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

4.配置application.yml文件

server:
 port: 8001
spring:
 cloud:
  config:
   server:
    git:
     uri: https://github.com/songxiansen521/spring-cloud-config-repo.git username: ****your git name**** password: ****your git pw****
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5.启动项目,访问http://localhost:8001/microservice-foo/dev,看到如下页面,Config Server 配置成功。

这里写图片描述

6.关于Config Server的端点。 
可以使用Config Server的端点获取配置文件的内容,映射规则如下:

/{application}/{profile}[/{label}]
  • 1

本例: http://localhost:8001/microservice-foo/dev

/{application}-{profile}.yml
/{application}-{profile}.properties
  • 1
  • 2

本例:http://localhost:8001/microservice-foo.properties

/{label}/{application}-{profile}.yml
/{label}/{application}-{profile}.properties
  • 1
  • 2

本例: http://localhost:8001/config-label-v1.0/microservice-foo-dev.properties

三 编写Config Client

1.新建spring boot 项目,添加如下依赖:

   <dependency>
            <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2.编写配置文件application.yml

server.port=8002
  • 1

3.编写配置文件bootstrap.yml。配置在bootstrap.xml中的属性有更高的优先级,默认情况下不会被本地覆盖。

spring:
 application:
  #对应config server中配置文件的{application}
  name: microservice-foo
 cloud:
  config:
    #访问config server的地址
    uri: http://localhost:8001
    #对应config server中配置文件的{profile}
    profile: dev
    #对应config server中配置文件的{label} label: master 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4.添加Controller类,

package com.swc.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/** * Created by chao on 2017-11-8. */ @RestController public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/getProfile") public String hello(){ return this.profile; } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

4.启动应用,访问http://localhost:8002/getProfile,得到如下页面,说明Client能成功通过Config Server获取Git仓库中对应环境的配置。

这里写图片描述

四、 使用/refresh端点手动刷新配置

很多时候,需要在运行期间动态调整配置,可以使用/refresh 实现微服务配置的刷新。 
在上面Config Client项目中,我已经添加了一个依赖:

<dependency>
            <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4

在controller上添加@RefereshScope,该注解会在配置更改时得到特殊的处理。

package com.swc.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 chao on 2017-11-8. */ @RestController @RefreshScope public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/getProfile") public String hello(){ return this.profile; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3.启动项目,http://localhost:8002/getProfile 访问修改Git仓库中配置文件内容,再次访问,发现结果没变化。 
这时,就需要手动刷新:以POST请求,访问http://localhost:8001/refresh 
第三次访问,结果如图所示,配置刷新

这里写图片描述

五、 Spring Config Server与Eurelka配合使用

将Config Server 和 Config Client都注册到Eureka Server上。 
修改 Config Client

spring:
 application:
  #对应config server中配置文件的{application}
  name: microservice-foo
 cloud:
  config:
    #访问config server的地址
    #uri: http://localhost:8001
    #对应config server中配置文件的{profile}
    profile: dev
    #对应config server中配置文件的{label} label: master discovery: #表示使用服务发现组件中提供的Config Server,默认是false #开启通过服务发现组件访问Config Server的功能 enabled: true #指定Config Server在服务发现组件中的serviceId 默认是configserver service-id: microservice-config-server-eureka eureka: client: service-url: defaultZone: http://localhost:8888/eureka/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

修改Config Server

server:
 port: 8001
spring:
 appliation:
  name: microservice-config-server-eureka
 cloud:
  config:
   server:
    git:
     uri: https://github.com/songxiansen521/spring-cloud-config-repo.git username: ***your git name*** password: ***your git pw*** eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

启动Eureka Server,Config Server和Config Client三个项目, 可获取到git仓库中配置文件的内容。

六、 Config Server的高可用

Config Server的高可用可以借助负载均衡实现,其原理图如下:

这里写图片描述

Config Server的高可用也可借助Eureka实现。 
这里写图片描述

猜你喜欢

转载自www.cnblogs.com/jiadp/p/9277762.html