美文网首页
Spring Cloud Config的使用

Spring Cloud Config的使用

作者: 程序员小杰 | 来源:发表于2020-09-12 12:55 被阅读0次

简介

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。

主要功能

集中管理各个环境、各个微服务的配置文件
提供服务端和客户端支持
配置文件修改后,可以快速生效
配置文件通过 Git/SVn 进行管理,天然支持版本回退功能。
支持高并发查询、也支持多种开发语言

准备工作

准备工作主要是给 GitHub 上提交数据。
本地准备好相应的配置文件,提交到 GitHub:https://github.com/nianq/configRepo

开发环境

JDK:1.8
SpringBoot:2.2.2.RELEASE
SpringCloud:Hoxton.SR8

ConfigServer

首先创建一个 ConfigServer 工程,创建时添加 ConfigServer 依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

项目创建成功后,项目启动类上添加注解,开启 config server 功能:

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

然后在配置文件中配置仓库的基本信息:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          #配置文件仓库地址
          uri: https://gitee.com/gongjienianq/config-repo.git
          #仓库中,配置文件的目录
          search-paths: config-client
          #如果是私有的就需要配置
          username: nianq
          password:
server:
  port: 8081

启动项目后,就可以访问配置文件了。访问地址:http://localhost:8081/config-client/dev/master

image.png
实际上,访问地址有如下规则:
/{application}/{profile}/[{label}]
/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.yml
/{label}/{application}-{profile}.properties
applicaiton 表示配置文件名
profile 表示配置文件 profile,例如 test、dev、prod
label 表示git 分支,参数可选,默认就是master
我平常喜欢这样访问:http://localhost:8081/config-client-dev.yml
image.png
接下来,可以修改配置文件,并且重新提交到 GitHub,此时,刷新 ConfigServer 接口,就可以及时看到最新的配置内容。

ConfigClient

创建一个 Spring Boot 项目,添加 ConfigClient 依赖:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

项目创建成功后,在resources目录下,添加 bootstrap.yml 配置,内容如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev
      label: master
      #config-server地址
      uri: http://localhost:8081
server:
  port: 8082

注:上面这些与spring-cloud相关的属性必须配置在bootstrap.yml中,config部分内容才能被正确加载。因为bootstrap.yml的相关配置会先于application.yml,而bootstrap.yml的加载也是先于application.yml。
接下来创建一个 HelloController 进行测试:

@RestController
public class ConfigController {

    @Value("${my.name}")
    String name;

    @GetMapping("hello")
    public String hello(){
        return name;
    }
}

访问接口:http://localhost:8082/hello

image.png
到此,Spring Cloud Config的基本使用我们已经成功实现了。但我们也能看出来一些问题。在config-server的yml中search-paths: config-client和config-client的yml中uri: http://localhost:8081以及profile: dev是配置死的。

配置

使用占位符灵活控制查询目录

修改 config-server 配置文件

#注意:这里要加引号不然会因为yml格式不合法而导致配置不生效
search-paths: '{application}'

这里的 {application} 占位符,表示链接上来的 client的 spring.application.name属性的值。

修改 config-client 配置文件

spring:
  profiles:
    active: ${DEPLOY_ENV}

然后在启动类上的绿色按钮右键

image.png
没有启动过就是Create
image.png
如果启动过就是Edit
image.png
然后指定环境变量
image.png
注:-DDEPLOY_ENV=prod 前面需要加-D
虽然在实际开发中,配置文件一般都是放在 Git 仓库中,但是,config-server 也支持将配置文件放在classpath 下。
image.png

在 config-server 中添加如下配置:

# 表示让 config-server 从 classpath 下查找配置,而不是去 Git 仓库中查找
spring:
  profiles:
    active: native

服务化

前面的配置都是直接在 config-client 中写死 config-server的地址,我们引入eureka。将config-client和config-server都注册到eureka上去,通过服务名称发现服务。

搭建Eureka

引入依赖

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

加入EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

配置

spring:
  application:
    name: eureka-gj  #服务名
server:
  port: 1999  #eureka 默认端口为8761
eureka:
  client:
    register-with-eureka: false  #是否注册到eureka上  默认为true  但是我这个是作为Eureka Server的,所以不需要注册
    fetch-registry: false  #是否从Eureka Server上获取注册信息
    service-url:
      defaultZone: http://localhost:1999/eureka   #注册地址

config-server

加入依赖

        <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);
    }
}

配置

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/gongjienianq/config-repo.git
          #注意:这里要加引号不然会因为yml格式不合法而导致配置不生效
          search-paths: '{application}'
          username: nianq
          password:
server:
  port: 8081
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1999/eureka

config-client

前面两步与config-server一模一样,将不阐述了。修改 config-client 的配置文件,不再直接写死 config-server 的地址了。

spring:
  application:
    name: config-client
  profiles:
    active: ${DEPLOY_ENV}
  cloud:
    config:
      profile: ${spring.profiles.active}
      label: master
      discovery:
      # 开启通过 eureka 获取 config-server 的功能
        enabled: true
      # 配置 config-server 服务名称
        service-id: CONFIG-SERVER

server:
  port: 8082
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1999/eureka

相关文章

网友评论

      本文标题:Spring Cloud Config的使用

      本文链接:https://www.haomeiwen.com/subject/lthesktx.html