六、SpringCloud学习 -- Config

Spring Cloud Config 分布式配置


官网地址https://www.springcloud.cc/spring-cloud-greenwich.html#_spring_cloud_config
代码地址https://github.com/becauseoflife/CodeDemo/tree/main/SpringCloud/config/springCloud


​ Spring Cloud Config为分布式系统中的外部化配置提供服务器端和客户端支持。使用Config Server,您可以在中心位置管理所有环境中应用程序的外部属性。客户端和服务器上的概念都与Spring EnvironmentPropertySource抽象映射相同,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。在应用程序从开发人员到测试人员再到生产人员的整个部署过程中,您可以管理这些环境之间的配置,并确保应用程序具有它们迁移时所需的一切。服务器存储后端的默认实现使用git,因此它轻松支持带标签的配置环境版本,并且可以通过各种工具来访问这些内容来管理内容。添加替代实现并将其插入Spring配置很容易。

分布式系统面临的–配置文件的问题

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

什么是 Spring Cloud Config 分布式配置中心

在这里插入图片描述

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

spring cloud config 分为服务端客户端两部分。

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

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

spring cloud config 分布式配置中心能干嘛?

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

spring cloud config 分布式配置中心与GitHub整合

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

实战练习

1、Git 环境搭建

在GitHub上新建仓库(推荐用码云:https://gitee.com/,github经常:Time out)

在这里插入图片描述

C:\Users\17521\.ssh文件夹下点击 Git Bash Here 生成密钥对

ssh-keygen -t rsa -C "邮箱"	# 一直回车

会生成两个文件 私钥:id_rsa 和 公钥:id_rsa.pub

登陆你的github帐户。点击你的头像,然后 Settings -> 左栏点击 SSH and GPG keys -> 点击 New SSH key

将公钥复制进去点击 生成即可。


将仓库克隆到本地新建 application.yml 文件,编写配置文件后提交到github上。

spring:
  profiles:
    active: dev
    
---
spring:
  profiles: dev
  application:
    name: springcloud-config-dev
    
---
spring:
  profiles: test
  application:
    name: springcloud-config-test

使用 Git 命令

git clone git@github.com:becauseoflife/SpringCloud-config.git # 克隆仓库
git add .	# 增加文件
git status	# 查看状态
git commit -m "init application.yml" # 提交文件
git push origin master	# 推送到github上
2、服务端连接Git配置

新建 springcloud-config-server-8888 模块

导入依赖

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- actuator 完善监控信息 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

编写配置

server:
  port: 8888

spring:
  application:
    name: springcloud-config-server
  # 连接远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://github.com/becauseoflife/SpringCloud-config/blob/master/application.yml

主启动类

/**
 * @desc
 * @auth llp
 * @date 2022年03月05日 19:39
 */
@SpringBootApplication
@EnableConfigServer		// 启用config
public class ConfigServer_8888 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServer_8888.class, args);
    }
}

启动访问地址: http://localhost:8888/application-dev.yml

在这里插入图片描述

HTTP服务具有以下形式的资源:

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

通过config-server 可以连接到git ,访问其中的资源及配置。

3、客户端连接服务端远程访问

编写 client-server.yml 配置文件,并提交到仓库

spring:
  profiles:
    active: dev
---
# spring 配置
server:
  port: 8201
spring:
  profiles: dev
  application:
    name: spring-provider-dept
# Eureka 配置
# 服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
---
# spring 配置
server:
  port: 8202
spring:
  profiles: test
  application:
    name: spring-provider-dept
# Eureka 配置
# 服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

新建模块 springcloud-config-client-9999,导入依赖

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>3.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>3.1.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

新建配置文件

  • bootstrap.yml 系统级别的配置
  • application.yml 用户级别的配置

bootstrap.yml 配置文件

# 系统级配置
spring:
  cloud:
    config:
      name: client-server # 需要从git上读取的资源名称。不需要后缀
      profile: dev
      label: master # 哪个分支
      uri: http://localhost:8888
      # http://localhost:8888/master/client-server-dev.yml

application.yml 配置文件

# 用户级配置
spring:
  application:
    name: springcloud-config-client-9999

编写controller

/**
 * @desc
 * @auth llp
 * @date 2022年03月05日 20:39
 */
@RestController
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;

    @Value("${server.port}")
    private String port;


    @RequestMapping("/config")
    public String getConfig(){
        return "applicationName: " + applicationName + "eurekaServer: " + eurekaServer + "port: " + port; 
    }
}

编写主启动类

/**
 * @desc
 * @auth llp
 * @date 2022年03月05日 20:37
 */
@SpringBootApplication
public class ConfigClient_9999 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClient_9999.class, args);
    }
}

启动测试

先启动服务端访问:http://localhost:8888/client-server-dev.yml 是否能获取配置

再启动客户端访问:http://localhost:8201/config 是否能获取配置 (dev 端口为 8201)

4、远程配置实战练习

新建配置文件 config-eureka.yml

spring:
  profiles:
    active: dev

---
server:
  port: 7001

# spring 配置
spring:
  profiles: dev
  application:
    name: springcloud-config-eureka
  
# Eureka 配置
eureka:
  instance:
    hostname: eureka7001.com # Eureka 服务端的示例名称
  client:
    register-with-eureka: false # 是否向 Eureka 注册中心注册自己
    fetch-registry: false # 是否从 Eureka 中获取注册信息
    service-url:  # 监控页面
      # 单机 : http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群(关联)
     defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
     
---
server:
  port: 7001

# spring 配置
spring:
  profiles: test
  application:
    name: springcloud-config-eureka
  
# Eureka 配置
eureka:
  instance:
    hostname: eureka7001.com # Eureka 服务端的示例名称
  client:
    register-with-eureka: false # 是否向 Eureka 注册中心注册自己
    fetch-registry: false # 是否从 Eureka 中获取注册信息
    service-url:  # 监控页面
      # 单机 : http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群(关联)
     defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

新建配置文件 config-dept.yml

spring:
  profiles:
    active: dev
    
---
server:
  port: 8001
# myBaits 配置
mybatis:
  type-aliases-package: com.example.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
# spring 配置
spring:
  profiles: dev
  application:
    name: spring-config-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_01?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password:

# Eureka 配置
# 服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept-8001 # 修改 Eureka 上的默认描述信息
    prefer-ip-address: true

# info 配置
info:
  app.name: mianbao-springcloud
  company.name: miaobao.com
  
---
server:
  port: 8001
# myBaits 配置
mybatis:
  type-aliases-package: com.example.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
# spring 配置
spring:
  profiles: test
  application:
    name: spring-config-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_02?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password:

# Eureka 配置
# 服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept-8001 # 修改 Eureka 上的默认描述信息
    prefer-ip-address: true

# info 配置
info:
  app.name: mianbao-springcloud
  company.name: miaobao.com

提交到远程仓库。

新建模块 springcloud-config-eurake-7001 复制 springcloud-eurake-7001模块。

导入config依赖

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

bootstrap.yml 配置

# 系统级配置
spring:
  cloud:
    config:
      name: config-eureka # 需要从git上读取的资源名称。不需要后缀
      profile: dev
      label: master # 哪个分支
      uri: http://localhost:8888

删除application.yml的配置。

启动访问Eureka地址:http://localhost:7001/ 可以访问


新建 springcloud-config-provider-dept-8001 复制一份 springcloud-provider-dept-8001。

boostrap.yml

# 系统级配置
spring:
  cloud:
    config:
      name: config-dept # 需要从git上读取的资源名称。不需要后缀
      profile: dev
      label: master # 哪个分支
      uri: http://localhost:8888

删除application.yml的配置。

启动访问Eureka地址:http://localhost:7001/ 可以看到实例。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值