SpringCloud 基础教程(四)-配置中心入门

   我的博客:兰陵笑笑生,欢迎浏览博客!

   上一章 SpringCloud基础教程(三)-Eureka进阶当中,我们在对Eureka的有了基本的基础认识之上,深入的了解Eureka高可用集群和其他的生产环境中用到的一些配置。本章将开始了解分布式环境下的配置中心。

前言

 为什么需要配置中心,在单体的应用中,配置文件就可以很好的解决配置问题。但是在微服务架构模式下,系统不可避免的被拆分了许多个微服务组件,如果还是通过以前的方式,配置的工作量会很大。为此,一个通用的分布式的配置管理中心是必不可少的。Spring Cloud提供了另外一个组件Spring Cloud Config。

 Spring Cloud Config提供了服务端和客户端的支持,基于Spring环境,能够无缝的集成Spring,默认的实现是基于Git仓库。当然也支持SVN,本地文件等,甚至自定义实现。

一 、快速构建Config Server配置服务端

 这里我们使用Git作为配置文件的存储仓库,首先建立Maven项目,并在Pom.xml文件中引入相关依赖。

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

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

 创建ConfigServerApplicaition.java 类,使用@EnableConfigServer注解,表示允许该服务以HTTP形式对外提供配置管理服务:

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplicaition {

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

 添加applicaiton.yml,配置如下:

spring:
  application:
    name: single
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/lnxxs/springCloudConfig.git
          password:
          username:
          search-paths: conf
      label: master

  • spring.cloud.config.server.git.uri:配置Git的仓库位置。
  • spring.cloud.config.server.git.search-paths:配置Git的仓库路径下的相对搜索位置。
  • spring.cloud.config.server.git.username:Git的用户名,如果仓库是公开的可以不填
  • spring.cloud.config.server.git.password:配置Git用户密码。
  • spring.cloud.config.lable:git的分支名称

 在这之前,事先创建Git仓库,添加配置文件,master分支添加如下文件和内容:

ConfigServer.properties:       k1=master-default-v1

ConfigServer-dev.properties:k1=master-dev-v1

ConfigServer-test.properties:k1=master-test-v1

ConfigServer-pro.properties:k1=master-pro-v1

 在服务启动后,可以基于HTTP请求访问如下的URL进行配置信息的获取,获取的格式有如下几种:

  • /{application}/{profile}
  • /{application}/{profile}[/{lable}]
  • /{application}-{profile}.properties
  • /{lable}/{application}-{profile}.properties
  • /{lable}/{application}/{profile}[/{lable}

其中applicaiton是文件的名称。如

1) http://localhost:6001/ConfigServer-dev.properties

file

2) http://localhost:6001/master/ConfigServer-dev.properties :加载指定分支的属性:

file

3) http://localhost:6001/ConfigServer/default :显示默认的配置

{
    "name":"ConfigServer",
    "profiles":[
        "default"
    ],
    "label":null,
    "version":"f516174f308769468f1ac683cfeffa803a63c9af",
    "state":null,
    "propertySources":[
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties",
            "source":{
                "k1":"master-default-v1"
            }
        }
    ]
}

4) http://localhost:6001/ConfigServer/dev :显示默认和dev的信息

{
    "name":"ConfigServer",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"f516174f308769468f1ac683cfeffa803a63c9af",
    "state":null,
    "propertySources":[
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer-dev.properties",
            "source":{
                "k1":"master-dev-v1"
            }
        },
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties",
            "source":{
                "k1":"master-default-v1"
            }
        }
    ]
}

5) http://localhost:6001/ConfigServer/test/master 显示指定的分支的test和默认的配置

{
    "name":"ConfigServer",
    "profiles":[
        "test"
    ],
    "label":"master",
    "version":"f516174f308769468f1ac683cfeffa803a63c9af",
    "state":null,
    "propertySources":[
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer-test.properties",
            "source":{
                "k1":"master-test-v1"
            }
        },
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties",
            "source":{
                "k1":"master-default-v1"
            }
        }
    ]
}

 如果通过以上的URL获取到Git仓库的配置信息,说明配置服务端正常。

二 、构建Config Client配置客户端

 接下来我们创建一个SpringBoot应用作为客户端来读取Config Server中提供的配置。(我们开发的任何一个微服务组件都可以认为是一个Config Client客户端。)

 新建ConfigClientApplication,java启动类,并在项目文件pom.xml中添加依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--web依赖-->
        <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>

 添加bootstrap.yml,注意这些配置必须是在bootstrap.yml中,配置才能生效,如果配置在applicaiton.yml中是没有任何效果的。

spring:
  application:
    name: server-client
  cloud:
    config:
        label: master
        name: ConfigServer
        profile: test
        uri: http://localhost:6001/
        

  • spring.cloud.config.label:指定分支
  • spring.cloud.config.name:指定配置文件的名称,我的git仓库的配置文件的名称为ConfigServer
  • spring.cloud.config.profile:指定激活那个配置文件
  • spring.cloud.config.uri:指定配置中心的url,所有的配置信息都是从配置中心获取。

 创建ConfigClientApplication.java启动类,并添加EnableAutoConfiguration注解,表示自动获取项目中的配置变量。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

 创建控制器,测试获取配置信息:

@RestController
public class ValueController {
    @Value("${k1}")
     String value;
    @GetMapping("/get")
    public String getValue(){
        return value;
    }
}

 调用接口测试,正确的获取到了配置中心的信息:

file

三、其他配置

3.1 客户端快速失败

 在无法连接Config Server的时候,我们有时候要求客户端需要启动失败,我们可以通过配置bootstrap配置项:

spring:
  cloud:
    config:
        fail-fast: true
        retry:
          initial-interval: 1000
          max-attempts: 6
          max-interval: 2000
          multiplier: 1.1

3.2 客户端重试

 如果我们要求客户端组件在Config Server不可用是,让客户端重试,那么我们也可以通过设置:

spring:
  cloud:
    config:
        fail-fast: false

 同时在客户端的配置文件中引入spring-retry和aop依赖:

         <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

3.3 HTTP权限

 如果需要对Config Server的http请求进行账号密码控制,在服务端配置:

spring:
  application:
    name: single
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/lnxxs/springCloudConfig.git
          password:
          username:
          search-paths: conf
      label: master
      username: 
      password: 
      uri: http://localhost:6001/
      enabled: true
  security:
    user:
      name: user
      password: pwd

 注意用户名、密码的属性是spring.security.user.name和spring.security.user.password

spring.cloud.config.username和spring.cloud.config.password服务端配置是没有什么作用,是客户端用来配置用的

 并在服务端的pom文件中引入security依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

 我们只需要将用户名和密钥复制并配置再每个客户端中bootstrap.xml中就可以,客户端是不需要引入security依赖的。

spring:
  application:
    name: server-client
  cloud:
    config:
        label: master
        profile: test
        uri: http://localhost:6001/
        name: ConfigServer
        username: user
        password: pwd

 在配置HTTP权限的时候,如果服务端在引入security之后,没有配置用户名和密码,那么服务端项目启动时,就会随机生成一个全局的密钥。这里我们不使用随机的密码。如果配置了用户名和密码,那么客户端必须同时配置一样的用户名和加密的密钥。

四、总结:

 这一篇文章简单的介绍了Spring Cloud ConfIf组件的使用,并结合git仓库搭建了分布式配置中心,实现了程序和配置的隔离,解耦编码与环境之间的耦合,这是非常重要的,当然这样的配置还是存在确定,在实际的生成环境中,所有的服务配置都依赖于配置中心,那么如果配置中心出现宕机该怎么办,下一章我们将继续了解分布式配置中心的集群搭建.

 以上就是本期的分享,你可以关注本博客的#Spring Cloud基础教程!#

​ 还可以关注公众号: 程序员笑笑生,关注更多精彩内容!

  • file
    *

本文由博客一文多发平台 OpenWrite 发布!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值