十、Spring Cloud Config 分布式配置

狂神-SpringCloud笔记目录



十、Spring Cloud Config 分布式配置

Dalston.RELEASE

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

10.1 概述

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

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

什么是SpringCloud 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 访问的形式。

10.2 入门案例

10.2.1 服务端

我们需要在远程建立一个仓库springcloud-config,来存放我们的配置,先在本地写好,然后push到远程仓库。

application.yml文件:

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

新建springcloud-config-server-3344模块导入pom.xml依赖

<dependencies>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
    </dependencies>

resource下创建application.yml配置文件,Spring Cloud Config服务器从git存储库(必须提供)为远程客户端提供配置:

server:
  port: 3344

spring:
  application:
    name: springcloud-config-server
  # 连接码云远程仓库
  cloud:
    config:
      server:
        git:
          # 注意是https的而不是ssh
          uri: https://gitee.com/wlw330/springcloud-config.git 
            # 通过 config-server可以连接到git,访问其中的资源以及配置~

# 不加这个配置会报Cannot execute request on any known server 这个错:连接Eureka服务端地址不对
# 或者直接注释掉eureka依赖 这里暂时用不到eureka
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

主启动类

/**
*主启动类
*/
@EnableConfigServer // 开启spring cloud config server服务
@SpringBootApplication
public class Config_server_3344 {
    public static void main(String[] args) {
        SpringApplication.run(Config_server_3344.class,args);
    }
}

将本地git仓库springcloud-config文件夹下新建的application.yml提交到码云仓库:

在这里插入图片描述

定位资源的默认策略是克隆一个git仓库(在spring.cloud.config.server.git.uri),并使用它来初始化一个迷你SpringApplication。小应用程序的Environment用于枚举属性源并通过JSON端点发布。

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

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

其中“应用程序”作为SpringApplication中的spring.config.name注入(即常规的Spring Boot应用程序中通常是“应用程序”),“配置文件”是活动配置文件(或逗号分隔列表的属性),“label”是可选的git标签(默认为“master”)。

测试访问http://localhost:3344/application-dev.yml

在这里插入图片描述

测试访问 http://localhost:3344/application/test/master

在这里插入图片描述

测试访问 http://localhost:3344/master/application-dev.yml

在这里插入图片描述

如果测试访问不存在的配置则不显示 如:http://localhost:3344/master/application-aaa.yml

在这里插入图片描述

10.2.2 客户端

在建立客户端之前我们还需要新建一个config-client.yml,push到远程仓库

config-client.yml文件:

#启动环境选择的配置
spring:
  profiles:
    active: dev

#springboot启动多环境的配置
---
server:
  port: 8201
#spring的配置
spring:
  profiles: dev
  application:
    name: springcloud-config-client-dev
#Eureka的配置。 服务注册到Eureka中,需要一个路径地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
---
server:
  port: 8202
#spring的配置
spring:
  profiles: test
  application:
    name: springcloud-config-client-test

#Eureka的配置。 服务注册到Eureka中,需要一个路径地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

将本地git仓库springcloud-config文件夹下新建的config-client.yml提交到码云仓库:

在这里插入图片描述

新建一个springcloud-config-client-3355模块,并导入依赖

<!--config-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-start -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<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>

resources下创建application.yml和bootstrap.yml配置文件

bootstrap.yml 是系统级别的配置

# 系统级别的配置
spring:
  cloud:
    config:
      name: config-client # 需要从git上读取的资源名称,不要后缀
      profile: dev
      label: master
      uri: http://localhost:3344

application.yml 是用户级别的配置

# 用户级别的配置
spring:
  application:
    name: springcloud-config-client-3355

创建controller包下的ConfigClientController.java 用于测试

package com.wlw.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {

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

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

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

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

主启动类

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

测试:

启动服务端Config_server_3344 再启动客户端ConfigClient

访问:http://localhost:8201/config/

在这里插入图片描述

10.2.3小案例

本地新建config-dept.yml和config-eureka.yml并提交到码云仓库

config-eureka.yml:

#启动环境选择的配置
spring:
  profiles:
    active: dev
---
server:
  port: 7001
#spring的配置
spring:
  profiles: dev
  application:
    name: springcloud-config-eureka-dev

#Eureka配置
eureka:
  instance:
    hostname: eureka7001.com #Eureaka服务端的实例名称
  client:
    register-with-eureka: false  #表示是否向Eureka注册中心注册自己
    fetch-registry: false #如果fetch-registry为false,则表示自己为注册中心
    service-url: #监控页面
      #单机:点进去参考源码,可看到默认的url端口配置为8761,我们设置为自己的端口。
      #defaultZone: 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-test

#Eureka配置
eureka:
  instance:
    hostname: eureka7001.com #Eureaka服务端的实例名称
  client:
    register-with-eureka: false  #表示是否向Eureka注册中心注册自己
    fetch-registry: false #如果fetch-registry为false,则表示自己为注册中心
    service-url: #监控页面
      #单机:点进去参考源码,可看到默认的url端口配置为8761,我们设置为自己的端口。
      #defaultZone: 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

#mybatis配置
mybatis:
  type-aliases-package: com.lemon.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

#spring的配置
spring:
  profiles: dev
  application:
    name: springcloud-config-dept-dev
    #数据源的配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #数据源为druid
    driver-class-name: com.mysql.jdbc.Driver #数据库驱动
    url: jdbc:mysql://localhost:3306/springcloud1?useUnicode=true&characterEncoding=utf-8&servetTimeZone=Asia/Shanghai
    username: root
    password: 12345678

#Eureka的配置。 服务注册到Eureka中,需要一个路径地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
  instance:
    #修改Eureka中status的默认描述信息。不配置默认为DESKTOP-XXX描述
    instance-id: springcloud-provider-dept8001
    prefer-ip-address: true #改为true后 Eureka中的status就会显示真实ip地址
#info配置   Eureka的status的xx/info链接点开后的info监控信息。没有太大意义
info:
  app.name: ti zi zui bang,jiayou
  company.name: tizi.lemon.com
  test.name: hahah test
---
server:
  port: 8001

#mybatis配置
mybatis:
  type-aliases-package: com.lemon.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

#spring的配置
spring:
  profiles: test
  application:
    name: springcloud-config-dept-test
    #数据源的配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #数据源为druid
    driver-class-name: com.mysql.jdbc.Driver #数据库驱动
    url: jdbc:mysql://localhost:3306/springcloud2?useUnicode=true&characterEncoding=utf-8&servetTimeZone=Asia/Shanghai
    username: root
    password: 12345678

#Eureka的配置。 服务注册到Eureka中,需要一个路径地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
  instance:
    #修改Eureka中status的默认描述信息。不配置默认为DESKTOP-XXX描述
    instance-id: springcloud-provider-dept8001
    prefer-ip-address: true #改为true后 Eureka中的status就会显示真实ip地址
#info配置   Eureka的status的xx/info链接点开后的info监控信息。没有太大意义
info:
  app.name: ti zi zui bang,jiayou
  company.name: tizi.lemon.com
  test.name: hahah test
 

在这里插入图片描述

在这里插入图片描述

新建springcloud-config-eureka-7001模块,并将原来的springcloud-eureka-7001模块下的内容拷贝的该模块。

1.清空该模块的application.yml配置,并新建bootstrap.yml连接远程配置

spring:
  cloud:
    config:
      name: config-eureka
      label: master
      profile: dev
      uri: http://localhost:3344  #指向config-server

2.在pom.xml中添加spring cloud config依赖

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

3.主启动类

@SpringBootApplication
@EnableEurekaServer //EnableEurekaServer 服务端的启动类,可以接受别人注册进来
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}

4.测试

第一步:启动 Config_Server_3344,并访问 http://localhost:3344/master/config-eureka-dev.yml (或 localhost:3344/config-eureka/dev/master)测试

在这里插入图片描述
第二部:启动ConfigEurekaServer_7001,访问 http://localhost:7001/ 测试

在这里插入图片描述
显示上图则成功

新建springcloud-config-dept-8001模块并拷贝springcloud-provider-dept-8001的内容

同理导入spring cloud config依赖、清空application.yml 、新建bootstrap.yml配置文件并配置

bootstrap.yml:

spring:
  cloud:
    config:
      name: config-dept
      label: master
      profile: dev
      uri: http://localhost:3344

application.yml:

spring:
  application:
    name: springcloud-config-dept-8001

主启动类

@SpringBootApplication
@EnableEurekaClient //在服务启动后自动注册到Eureka中!
@EnableDiscoveryClient //服务发现~
@EnableCircuitBreaker //
public class ConfigDeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigDeptProvider_8001.class,args);
    }

    //增加一个 Servlet
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        return registrationBean;
    }
}

测试 (略)

启动 Config_Server_3344、启动ConfigEurekaServer_7001、启动DeptProvider_8001、

localhost:3344/config-dept/dev/master 先测试,

之后访问:http://localhost:7001/,发现已经注册进来了,

之后再访问,localhost:8001/dept/get/1 得到数据

总结:通过这个小案例,改动eureka与dept,发现就是把配置文件改成远程读取的方式,其他(例如:pom文件、controller、启动类)什么都没有变化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

悬浮海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值