Spring Cloud Config 入门

1.传统配置模式的缺点

  1. 微服务架构下, 为了保证高可用性, 同一个serivce 可以能会被部署多个实例, 一旦配置文件发生修改, 则要改多个实例
  2. 通常修改properties 或者 yml 某些属性(例如数据库连接), 需要重启服务
  3. 若是xml, 可能需要重新打包和重启, 高可用性就难以保证

spring cloud config 的架构

update
get config information
sync config information
get config info via api
get config info via api
get config info via api
online refresh
Developer
Git Remote Repository
Config server
Git local Repo
Service 1
Service 2
Service 3
Spring cloud bus POST /bus/refresh

解决了 配置 中心化, 版本控制, 平台独立, 语言独立的等功能

Git Remote Repository

Spring cloud config 支持从1个远程仓库上读出各个service的配置。
注意这个仓库并不是指Config server 本身的代码仓库, 而是1个单独用来放置配置文件的仓库。

Git local Repository

Spring Cloud config 一旦从远程仓库得到1个配置文件, 在传给对应service同时也会把这个文件放到Spring Clould config本身所在服务器上, 它会在本地再创建1个git 仓库 存放这些本地配置文件。

相当于1个缓存
即使连解远程仓库失败, Spring CLoud config 也能正常工作。

2.创建存放配置文件的远程git仓库

github/gitlab/gitee 都可以。
我在Gitee里创建了1个
在这里插入图片描述
其中根目录里放了1个test-project-prod.yml
把另1个配置文件demo-cloud-order-service-prod.yml 放在1个文件夹中

3. 创建spring cloud config的maven项目

pom.xml 依赖

要加上spring cloud config server 的依赖

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
项目配置文件 application.yml
server:
  port: 8888

spring:
  application:
    name: demo-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: git@gitee.com:xxxxx/cloud-config-server-repo.git
          ignore-local-ssh-settings: true
          strict-host-key-checking: false
          search-paths: '{application}'   # make config remo repo support sub folder
          privateKey: |
                      -----BEGIN RSA PRIVATE KEY-----
                      MIIJKQIBAAKCAgEApVb9zYEq7iOQfisp8ZUoYq49Yb0i1faBof4AWA+f5nDietXb
                      ltTuIjVhMajKZlat3FMnnBpiEXcUEeOU+z84UN1E8vGyeRdrrmuBpajD8/91mldA
                      35eJF9lI8l7GhxQD1fkVxAVBQBowZqGpo1EjjUAf3H4XRGaKWKldThkq2JYfQ9g1
                      0ocqRd1cPdur+ryezx7EWh72/cCmVbkkiPeuv001ckYn9FBX+Y6w39AfbToK5Brh
                      gn8D9yMoLcC2mrnPlLSPoHB1GrrayIRFYgY8/jKXvwgzBwFUB7fxeOlF43kTFkyw
                      auk+RyHcANuHGVS+YnRPZy6eugGqQvccweEIiSfWYdQV5bMI3p1IrSpP6BU2iIk5
                      Kv/g89c7JqFfg0Cbggc0o4WBb8ATZbi17IJSa8L5mDKeA8yHws0mf4rVE4rP7530
                      w3K2cfry8xsodxeSpX/ARqzxK/9fSpziypKX/t/GbXqC73WSU9rC9SjL72uyu5pU
                      BuyyNFcy9OefEugkOYJJerlRUPmmeCMQzosnutrmzwf5WvyZF3XgByDDm8htPa2X

server.port 建议保留默认8888

关键是 git remote repo的配置, 包括
cloud.config.server.git.uri 这个是 git remote repo 的url, 我这里使用的ssh登陆模式
cloud.config.server.git.search-paths, 这个也关键, 如果没有这个配置, config server只会从git remote repo的根目录去找配置文件, 如果加上这个, 会参考各service的application name去子目录下查找

启动类加上@EnableConfigServer 注解
package com.home.configserver;

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

@SpringBootApplication
@EnableConfigServer
public class DemoCloudConfigServerApplication {

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

}

启动项目和测试

启动项目很简单, 正常springboot 启动即可

接下来可以测试接口, 接口格式如下,
The HTTP service has resources in the following form:

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

其中appilcation 代表service name
profile代表环境名字, dev/uat/sit/prod 等
label 代表远程仓库的branch那么, 一般保持默认master即可
当然远程仓库里的配置文件命名要严格按照 application-profile.yml 的格式

测试从根目录里读取配置文件
在这里插入图片描述

测试从server name分层目录下读取配置文件
在这里插入图片描述

从日志里我们可以见到spring cloud config把远程配置文件下载后放到本地git local repo

2022-07-31 23:02:18.150  INFO 32192 --- [nio-8888-exec-3] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/gateman/AppData/Local/Temp/config-repo-573782205839535107/demo-cloud-order-service/demo-cloud-order-service-prod.yml

4. 把1个client server 令到它的配置文件可以被cloud config server 管理

我们以之前的cloud-demo-order-service 作为第一个例子

pom.xml 依赖

要加上spring cloud starter 的依赖

         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
创建boostrap.yml

一旦使用configserver, 我们必须创建boostrap.yml, 这个会比 application.yml优先级高, 而留着application.yml也可以
在这里插入图片描述
如图, 我创建了两个bootstrap 1个for prod, 1个 for uat.
prod

spring:
  cloud:
    config:
      name: demo-cloud-order-service   # means the config file service name
      uri: http://10.0.1.223:8888 # config-server ip
      label: master # git branch
      profile: prod

uat

spring:
  cloud:
    config:
      name: demo-cloud-order-service   # means the config file service name
      uri: http://10.0.1.223:8888 # config-server ip
      label: master # git branch
      profile: uat

当然,在远程配置文件仓库中, 必须保证有这个两个文件
在这里插入图片描述

创建一个测试接口

去读取coder.name 这个配置。
而且要令到这个配置在 prod 和 uat 的profile中是不同的

@RestController
public class TestController {

    @Value("${coder.name}")
    private String coderName;

    @GetMapping("/codername")
    public String getCoderName(){
        return coderName;
    }

}

启动和测试

如果我们用spring.profiles.active=uat 的来启动, 则读取uat的profile
如果我们用spring.profiles.active=prod 的来启动, 则读取prod 的profile
在这里插入图片描述

4. 整合Eureka

接下来介绍怎么把config server整合到spring cloud framework中。

在Config Server 的Pom.xml 引入 Eureka client 依赖
        <!-- Eureka Client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
修改config server 的配置

application-uat.xml

eureka:
  client:
    service-url:
      defaultZone: http://10.0.1.223:8761/eureka #url of eureka service
  instance:
    prefer-ip-address: true

UAT 的config server 会被deployed 到家庭服务器, 所以向home server的Eureka注册

application-prod.xml

eureka:
  client:
    service-url:
      defaultZone: http://43.138.194.xxx:8761/eureka #url of eureka service
  instance:
    prefer-ip-address: true

prod config server 会部署到云服务器, 向云服务器的eureka 注册

部署上云服务器后, 看到 config server 在注册服务列表就好
在这里插入图片描述

修改order service 的配置

order service 本身已经整合eureka client

所以我们不再需要指定config server 的ip地址, 只需要指定config service在eureka中的服务名字

bootstrap-uat.yml

spring:
  cloud:
    config:
      name: demo-cloud-order-service   # means the config file service name
      # uri: http://10.0.1.223:8888 # config-server ip
      label: master # git branch
      profile: uat
      discovery: # to use the config server in Eureka
        enabled: true
        service-id: demo-cloud-config-server

eureka:
  client:
    service-url:
      defaultZone: http://10.0.1.223:8761/eureka #url of eureka service
  instance:
    prefer-ip-address: true

bootstrap-prod.yml

spring:
  cloud:
    config:
      name: demo-cloud-order-service   # means the config file service name
      # uri: http://10.0.1.223:8888 # config-server ip
      label: master # git branch
      profile: prod
      discovery: # to use the config server in Eureka
        enabled: true
        service-id: demo-cloud-config-server

eureka:
  client:
    service-url:
      defaultZone: http://43.138.194.xxx:3366/eureka #url of eureka service
  instance:
    prefer-ip-address: true

启动测试pass即可!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nvd11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值