SpringCloud Config配置中心服务端和客户端的配置

springcloud config官网社区

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml, 上百个配置文件的管理就很麻烦了,所以有了Config配置中心,一处配置,处处生效 

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

SpringCloud Config分为服务端客户端两部分。


服务端:也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口.


客户端:则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容.

配置中心作用:

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露,post、curl访问刷新均可....

由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式。

下面为配置中心的搭建:

服务端搭建:

1、先在git上创建仓库,然后将本地仓库初始化,创建config-dev.yml等文件(注意一定为utf-8格式,vim中可以通过:set fileencoding来查看和设置utf-8格式),上传到github上,如果此步骤有疑问,可参考这篇文章上传本地项目到github,config-dev文本

config:
     info: master,this is config-dev.yml version:3

2、构建pom文件,主要是config-server的配置文件

<dependencies>
        <!--springcloud 的config配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--web-->
        <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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3、构建application.yml文件,设置端口号,注册进注册中心,设置git仓库和目录及读取分支等

server:
  port: 3344

spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          # GitHub上面git仓库地址
          uri: https://github.com/demonruin/springcloud-config    #git仓库那个 https地址
#          uri: [email protected]:demonruin/springcloud-config.git   #ssh地址启动会报错,reject HostKey: github.com,所以使用https即可
          # 搜索目录
          search-paths:
            - springcloud-config
      #读取分支
      label: master

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机
      defaultZone: http://localhost:7001/eureka
      # 集群
      #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka

4、构建主启动类,加上注解@enableConfigServer

package com.king.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * created by king on 2020/4/27 10:44 上午
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
            SpringApplication.run(ConfigCenterMain3344.class,args);
        }
}

5、启动注册中心7001,然后启动配置中心3344项目,进行url访问进行验证读取远程仓库的配置数据,此处读取规则可以有很多种,如果读取的信息在git仓库中没有,会读取为空{}:官网提供的格式springcloud config官网

http://localhost:3344/springcloud-config/config-dev

{
name: "springcloud-config",
profiles: [
"config-dev"
],
label: null,
version: "10a87420ab98c60cd3961622f58b615593ee8712",
state: null,
propertySources: [ ]
}

http://localhost:3344/config-dev.yml

config:
  info: master,this is config-dev.yml version:3

http://localhost:3344/config/dev/master

{
name: "config",
profiles: [
"dev"
],
label: "master",
version: "10a87420ab98c60cd3961622f58b615593ee8712",
state: null,
propertySources: [
{
name: "https://github.com/demonruin/springcloud-config/config-dev.yml",
source: {
config.info: "master,this is config-dev.yml version:3"
}
}
]
}

此时代表 成功实现了用SpringCloud Config 通过GitHub获取配置信息,完成了服务端的配置中心的搭建!

 

客户端搭建:

搭建之前引入一个bootstrap.yml文件:

  • applicaiton. yml是用户级的资源配置项
  • bootstrap. yml是系统级的,优先级更加高

Spring Cloud会创建一个"Bootstrap Context",作为Spring应用的Application Context'的父上下文。初始化的时候," Bootstrap Context"负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的"Environment"。
"Bootstrap"属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.ymI文件, 保证Bootstrap Context和Application Context配置的分离。
要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,
因为bootstrap.yml是比application.yml先加载的。bootstrap.ymI优先级高 于application.yml

1、构建pom文件,添加依赖配置中心客户端的依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.king.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2、构建bootstrap.yml文件

server:
  port: 3355


spring:
  application:
    name: cloud-config-client
  cloud:
    #config 客户端配置
    config:
      label: master  # 分支名称
      name: config   # 配置文件名称
      profile: dev   # 读取后缀名称  上述3个综合: master分支上config-dev.yml的配置文件被读取http://localhost:3344/master/config-dev.yml
      uri: http://localhost:3344/ #配置中心地址

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机
      defaultZone: http://localhost:7001/eureka
      # 集群
      #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka

3、构建主启动类

package com.king.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * created by king on 2020/4/27 1:58 下午
 */
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
    public static void main(String[] args) {
            SpringApplication.run(ConfigClientMain3355.class,args);
        }
}

4、构建测试controller,来请求配置中心服务端来获取配置信息

package com.king.springcloud.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 king on 2020/4/27 2:04 下午
 */
@RestController
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping(value = "/configInfo")
    public String getValue(){
        return configInfo;
    }
}

5、通过服务端的5步测试可以得知服务端服务正常,此时通过请求url来验证客户端也能正常请求:

http://localhost:3355/configInfo

master,this is config-dev.yml version:3

此时代表客户端能通过服务端进行访问github上的配置了,表示客户端也搭建完成~

 

注意:

但是会有一个问题产生,Linux运维修改GitHub上的配置文件内容做调整,刷新3344,发现ConfigServer配置中心立刻响应,刷新3355,发现ConfigServer客户端没有任何响应,3355没有变化除非自己重启或者重新加载,难道每次运维修改配置文件,客户端都需要重启??针对此问题,需要对客户端进行下述手动配置

1、在pom中添加actuator监控依赖

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、在bootstrap.yml文件中添加配置

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3、在controller上加入@RefreshScope注解

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping(value = "/configInfo")
    public String getValue(){
        return configInfo;
    }
}

4、此时修改完github上的文件后,还是会有上述问题,此时需要在修改完github文件后,再手动访问一次url刷新

curl -X POST "http://localhost:3355/actuator/refresh"然后响应成功["config.client.version","config.info"]

5、此时再访问,客户端访问到的就是最新修改的文件数据了。

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

可能到此还有疑问,难道每次修改完配置,都需要一个curl的去请求吗?如果太多的微服务配置需要修改,虽然可以写个批处理文件去执行,但是也总归是麻烦啊,对于手动动态刷新配置的情况,后面就出来了 springcloud bus 消息总线,通过消息总线即可实现广播功能,一处修改,处处生效~请参阅接下来的这篇spingcloud bus消息总线配置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章