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>

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

@SpringBootApplication
@EnableConfigServer
public class SpringConfigServerApplication {

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

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****

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

这里写图片描述

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

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

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

/{application}-{profile}.yml
/{application}-{profile}.properties

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

/{label}/{application}-{profile}.yml
/{label}/{application}-{profile}.properties

本例: 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>

2.编写配置文件application.yml

server.port=8002

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

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;
    }
}

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>

在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;
    }
}

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/

修改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/

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

六、 Config Server的高可用

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

这里写图片描述

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值