带你入门SpringCloud统一配置 | SpringCloud Config

前言

在微服务中众多服务的配置必然会出现相同的配置,若是配置发生变化须要修改,一个个去修改而后重启项目的方案是绝对不可取的。而 SpringCloud Config 就是一个能够帮助你实现统一配置选择之一。html

若是你不懂 SpringCloud Config 环境搭建,那么该篇博客将会帮助到你,文中经过具体操做带你了解 SpringCloud Config 环境搭建的入门操做。java

阅读本文须要你熟悉 SpringBoot 项目的基本使用便可,还有一点须要注意的是在操做过程当中尽可能和我本地环境一致,由于环境不一致可能会带来一些问题。我本地环境以下:git

  • SpringBoot Version: 2.1.0.RELEASE
  • SpringCloud Version: Greenwich.RELEASE
  • Apache Maven Version: 3.6.0
  • Java Version: 1.8.0_144
  • IDEA:Spring Tools Suite (STS)

接下来就开始 SpringCloud Config 环境搭建操做介绍!github

搭建 SpringCloud Config 环境

SpringCloud Config 环境搭建最小环境须要 3个 SpringCloud 项目:一台 Eureka Server 端、一台 Config Server 端(也是Eureka Client 端)、一台普通服务端(便是 Config Client 也是 Eureka Client)。web

关于Eureka环境的搭建请参考个人另外一篇博客 带你入门SpringCloud服务发现 | Eurka搭建和使用spring

Config Server 端搭建

在 SpringBoot 项目中引入 spring-cloud-config-server 和 spring-cloud-starter-netflix-eureka-client 依赖,具体代码以下:bootstrap

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

在 SpringBoot Application 上声明 @EnableDiscoveryClient 和 @EnableConfigServer,具体代码以下:segmentfault

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class SpringCloudConfigServerApplication {

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

}

在 GitHub上建立私有仓库
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
而后添加配置信息到 GitHub 上。
在这里插入图片描述
在这里插入图片描述app

product.properties 配置能够添加一些公共的配置他会覆盖到 product-dev.properties上

在 application.properties 添加配置信息maven

spring.application.name=CONFIGSERVER
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

spring.cloud.config.server.git.uri=https://github.com/zhuoqianmingyue/config-repo
spring.cloud.config.server.git.username=github账号
spring.cloud.config.server.git.password=github账号密码
spring.cloud.config.server.git.basedir=config-repo/config-repo
  • spring.application.name:服务的名称
  • eureka.client.service-url.defaultZone:Eureka Server 地址
  • spring.cloud.config.server.git.uri:配置GitHub 私有仓库 HTTP 克隆地址
  • spring.cloud.config.server.git.username:配置你的 github账号
  • spring.cloud.config.server.git.password:配置你的github账号密码
  • spring.cloud.config.server.git.basedir:克隆配置文件存储地址
Config Server 端高可用只须要配置多台Config Server注册到Eureka 服务端便可。

测试

第一步启动 Eurka Server 端(具体代码请从个人GitHub项目获取,GitHub地址下面有介绍)

将 spring-cloud-config-eureka-service 进行打包,经过 mvn clean package -Dmaven.test.skip=true
在这里插入图片描述
进入 target 目录 经过 java -jar 方式启动。
在这里插入图片描述
第二步启动 Config Server 端
在这里插入图片描述
第三步最后访问 Eurka Server 端,以下图所示:

CONFIGSERVER 已经注册到 Eurka Server 服务端。
在这里插入图片描述
访问 Config Server 端获取配置信息,具体访问地址:http://localhost:8080/product-dev.properties。访问结果以下图所示:
在这里插入图片描述
到这里 Config Server 端搭建介绍完毕!接下来开始 Config Client 端搭建介绍。

Config Client 端搭建(商品服务)

在商品服务 SpringBoot 项目中引入 spring-cloud-config-client 和 spring-cloud-starter-netflix-eureka-client 依赖。具体代码以下:

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

在 SpringBoot Application上声明 @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudConfigProductServiceApplication {

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

}

在 srcmainresources 目录下建立 bootstrap.properties, 具体代码以下:

spring.cloud.config.discovery.service-id=CONFIGSERVER
spring.cloud.config.discovery.enabled=true
spring.cloud.config.profile=dev
spring.application.name=product

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
  • spring.cloud.config.discovery.service-id:Config Server 服务名
  • spring.cloud.config.discovery.enabled:是否开启配置发现
  • spring.cloud.config.profile:启用那个后缀的配置文件
  • eureka.client.service-url.defaultZone:Eureka Server 地址
  • spring.application.name:服务的名称
链接接Config Server配置 spring.cloud.config.xx 和 eureka Server 端配置eureka.client.service-url.defaultZone= http://localhost:8761/eureka/ 必定要配置到bootstrap.properties中,不然根不获取不到 Config Server 的配置信息。

测试

第一步启动 Eurka Server端 和 Config Server 端。
第二步启动 Config Client (商品服务) 端
第三步访问 Eurka Server 端,以下图所示:

PRODUCT 已经注册到 Eurka Server 服务端。
在这里插入图片描述
第四步:编写获取 Config Server 上配置信息的 Controller,具体代码以下:

获取的就是红色框的 env 配置项的值。
在这里插入图片描述

@RestController
public class EvnController {
    @Value("${env}")
    private String env;
    
    @RequestMapping("/env")
    public String evn() {
        return this.env;
    }
}

游览器访问 localhost:8763/product/env 进行测试,具体结果以下:
在这里插入图片描述

小结

SpringCloud Config 执行流程是通用的配置添加配置仓库中(默认使用Git),在由 Config Server 读取配置仓库配置并对外提供接口。其余服务(Config Client)能够经过调用Config Server 提供接口来获取配置信息。

搭建过程也并不复杂仍是SpringCloud 添加starter 依赖、添加EnableXX 注解、最后在添加相关配置便可。没有操做的最好操做一篇哈!

代码示例

若是你按照上述方式搭建并未成功,能够参考我在GitHub 项目 spring-cloud-get-started 仓库中模块名为:

  • spring-cloud-config-eureka-service
  • spring-cloud-config-server
  • spring-cloud-config-product-service

进行对比查看是否配置有误。

spring-cloud-get-started 项目地址:https://github.com/zhuoqianmingyue/spring-cloud-get-started

相关文章
相关标签/搜索