springcloud config配置中心

spring cloud config

解决问题:

1、项目微服务化,配置文件很多,特别是集群之后,如果改了之后,需要改多处

2、修改了配置之后,必须重启服务,否则配置无法生效,无法及时更新。

一、简介
什么是配置中心

对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,
但是在微服务架构中全部手动修改的话很麻烦而且不易维护。微服务的配置管理一般有以下需求:
1、集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的。
2、不同环境不同配置,比如数据源配置在不同环境(开发,生产,测试)中是不同的。
3、运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小等
配置修改后可自动更新。如配置内容发生变化,微服务可以自动更新配置

常见配置中心

Spring Cloud Config

为分布式系统中的外部配置提供服务器和客户端支持。

Apollo(阿波罗)

是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。

Disconf

专注于各种「分布式系统配置管理」的「通用组件」和「通用平台」, 提供统一的「配置管理
服务」包括 百度、滴滴出行、银联、网易、拉勾网、苏宁易购、顺丰科技 等知名互联网公司正在使用!

springcloud config简介

Spring Cloud Confifig项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,

server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并

依据此数据初始化自己的应用。

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可
以为所有环境中的应用程序管理其外部属性。它非常适合spring应用,也可以使用在其他语言的应用
上。随着应用程序通过从开发到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用
程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置
环境,以及可以访问用于管理内容的各种工具。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RPQABfco-1613658379555)(…/image/image-20200808113308885.png)]

Spring Cloud Config服务端特性:

HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)
属性值的加密和解密(对称加密和非对称加密)
通过使用@EnableConfigServer在Spring boot应用中非常简单的嵌入。

Config客户端的特性(特指Spring应用)

绑定Config服务端,并使用远程的属性源初始化Spring环境。
属性值的加密和解密(对称加密和非对称加密)

二、springcloud config入门
搭建config-server

gitee上添加仓库,存放配置文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JcGh9JmZ-1613658379557)(…/image/image-20200809140410889.png)]

springcloud config访问规则

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

{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。

{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。

{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。

添加依赖

<!-- spring cloud config 服务端包 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

启动类增进注解 @EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

application.yml

server:
  port: 9007

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zengqingfa/springcloud-config.git #配置文件所在仓库
          #username: gitee #登录账号
          #password: gitee #登录密码
          default-label: master #配置文件分支
          search-paths: config  #配置文件所在根目录

启动项目,访问测试:

http://localhost:9007/product-service-dev.yml

data:
  env: product-service-dev
  user:
    password: zqf-dev-123456
    username: zqf-dev

http://localhost:9007/product-service-prod.yml

data:
  env: product-service-prod
  user:
    password: zqf-prod-123456
    username: zqf-prod
创建配置中心客户端

添加依赖

  	<!-- spring cloud config 客户端包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

修改仓库配置文件

product-service-dev.yml

server:
  port: 9008

data:
  env: product-service-dev
  user:
    username: zqf-dev
    password: zqf-dev-123456

product-service-prod.yml

server:
  port: 9009

data:
  env: product-service-prod
  user:
    username: zqf-prod
    password: zqf-prod-123456

增加bootstrap.yml配置文件

bootstrap.yml配置文件加载顺序优于application.yml

spring:
  profiles:
    active: dev


--- #使用--- 区分不同的环境配置
spring:
  profiles: dev
  cloud:
    config:
      uri: http://localhost:9007 #config-server的地址
      label: master  #git分支
      profile: dev  #开发环境
      name: product-service   #应用名称 product-service-dev.yml

---
spring:
  profiles: prod
  cloud:
    config:
      uri: http://localhost:9007
      label: master
      profile: prod
      name: product-service #product-service-prod.yml

使用@Value@ConfigurationProperties

@Component
@Data
public class ValueConfig {

    @Value("${data.env}")
    private String env;

    @Value("${data.user.username}")
    private String username;

    @Value("${data.user.password}")
    private String password;
}
@Component
@ConfigurationProperties(prefix = "data")
@Data
public class DataConfig {

    @Data
    public static class UserInfo {
        private String username;

        private String password;
    }

    private String env;

    private UserInfo user;
}

Rest

@RestController
public class ConfigDemoRest {

    @Autowired
    private DataConfig dataConfig;
    @Autowired
    private ValueConfig valueConfig;

    @GetMapping(value = "/valueConfig/show")
    public Object valueConfig() {
        return valueConfig;
    }

    @GetMapping(value = "/dataConfig/autoshow")
    public Object dataConfig() {
        return dataConfig;
    }
}

测试

http://localhost:9008/valueConfig/show

{"env":"product-service-dev","username":"zqf-dev","password":"zqf-dev-123456"}

http://localhost:9008/dataConfig/autoshow

{"env":"product-service-dev","user":{"username":"zqf-dev","password":"zqf-dev-123456"}}
三、实现自动刷新

当我们修改GitHub上面的值时,服务端(Config Server)能实时获取最新的值,但客户端(Config Client)读的是缓存,无法实时获取最新值。需要我们重启服务器。

它提供了一个刷新机制,但是需要我们主动触发。那就是 @RefreshScope 注解并结合 actuator ,注意要引入 spring-boot-starter-actuator 包。

四、结合 Eureka 使用 Spring Cloud Config

统中使用了 Eureka 作为服务注册发现中心,那么 Spring Cloud Config 也应该注册到 Eureka 之上,方便其他服务消费者使用,并且可以注册多个配置中心服务端,以实现高可用。

搭建注册中心eureka server

沿用之前的eureka

配置 Spring Cloud Config 服务端

添加依赖

 		 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

添加注解@EnableEurekaClient

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {

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

}

配置文件 application.yml添加eureka配置

server:
  port: 9007

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zengqingfa/springcloud-config.git #配置文件所在仓库
          #username: gitee #登录账号
          #password: gitee #登录密码
          default-label: master #配置文件分支
          search-paths: config  #配置文件所在根目录

# eureka相关配置
eureka:
  client:
    serviceUrl:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://127.0.0.1:9001/eureka
  instance:
    preferIpAddress: true
配置 Spring Cloud Config 客户端

添加依赖

        <!-- spring cloud config 客户端包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

添加注解@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication1 {

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

配置文件bootstrap.yml添加配置

spring:
  profiles:
    active: dev


--- #使用--- 区分不同的环境配置
spring:
  profiles: dev
  cloud:
    config:
      label: master  #git分支
      profile: dev  #开发环境
      name: product-service  #应用名称 product-service-dev.yml
      discovery:
        enabled: true
        service-id: config-server #config-server服务名称
# eureka相关配置
eureka:
  client:
    serviceUrl:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://127.0.0.1:9001/eureka
  instance:
    preferIpAddress: true


---
spring:
  profiles: prod
  cloud:
    config:
      name: product-service  #应用名称 product-service-dev.yml
      label: master
      profile: prod #生产环境
      discovery:
        enabled: true
        service-id: config-server #config server服务名称
# eureka相关配置
eureka:
  client:
    serviceUrl:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://127.0.0.1:9001/eureka
  instance:
    preferIpAddress: true

测试访问

http://localhost:9008/valueConfig/show

{"env":"product-service-dev","username":"zqf-dev","password":"zqf-dev-123456"}

http://localhost:9008/dataConfig/autoshow

{"env":"product-service-dev","user":{"username":"zqf-dev","password":"zqf-dev-123456"}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值