为了账号安全,请及时绑定邮箱和手机立即绑定

SpringCloud-Config分布式配置中心

1. Config-Server端

(1) pom

parent依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
</parent>
dependencyManagement
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
dependencies
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>

(2)启动类:增加注解@EnableConfigServer,当然@SpringBootApplication也是必不可少的

package com.luych.configServer;

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

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

(3)application.yml

如果用git作配置仓库,则:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/LOVE0612/SpringCloud.git
          username: 562759534@qq.com
          password: 123@abc
  • 如果希望ConfigServer启动的时候就将配置文件clone下来,则可增加配置:spring.cloud.config.server.git.clone-on-start=true。
  • 如果配置文件不在git仓库的根目录下, 例如:如果配置文件在git仓库的properties目录下,则可增加配置spring.cloud.config.server.git.search-paths=properties。当有多个目录时逗号分隔。
  • 如果需要指定git clone后在本地存储位置,则可增加配置spring.cloud.config.server.git.baseDir。

如果用svn作配置仓库,则:

spring:
  cloud:
    config:
      server:
        svn:
          uri: https://192.168.1.111/svn/SpringCloud
          username: luyanchao
          password: abc@123
  • 同样存在spring.cloud.config.server.svn.search-paths配置项,使用方法同git。
  • 同样存在spring.cloud.config.server.git.baseDir配置项,使用方法同git。

如果使用本地文件系统作配置仓库,则:

spring:
  cloud:
    config:
      server:
        native:
          search-locations: properties
      profile: native
  • search-locations:可以是绝对路径也可以是classpath路径。
  • 使用本地文件系统作为配置仓库的最大缺点在于:如果ConfigServer多点部署以保证高可用时,需要将文件系统进行共享。

如果使用多种/多个配置仓库,则:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/LOVE0612/SpringCloud.git
          username: 562759534@qq.com
          password: 1226ai0612
          order: 1
        svn:
          uri: https://192.168.1.111/svn/SpringCloud
          username: luyanchao
          password: abc@123
          order: 2
        native:
          search-locations: properties
          order: 3
      profile: native, git, svn
  • profile:设定哪些配置仓库可用,逗号分隔
  • order:配置仓库的优先级顺序,数字越低优先级越高

(3) run,并测试

ConfigServer提供多种rest接口访问配置信息,可通过run日志查看:

Mapped "{[/{name}-{profiles}.properties],methods=[GET]}" onto ...
Mapped "{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}" onto ...
Mapped "{[/{name}/{profiles:.*[^-].*}],methods=[GET]}" onto ...
Mapped "{[/{label}/{name}-{profiles}.properties],methods=[GET]}" onto ...
Mapped "{[/{label}/{name}-{profiles}.json],methods=[GET]}" onto ...
Mapped "{[/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml],methods=[GET]}" onto...
Mapped "{[/{name}/{profiles}/{label:.*}],methods=[GET]}" onto ...
Mapped "{[/{name}-{profiles}.json],methods=[GET]}" onto ...
Mapped "{[/{name}/{profile}/{label}/**],methods=[GET],produces=[application/octet-stream]}" onto ...
Mapped "{[/{name}/{profile}/{label}/**],methods=[GET]}" onto ...
Mapped "{[/{name}/{profile}/**],methods=[GET],params=[useDefaultLabel]}" onto...
  • 参数name:即ConfigClient中定义的spring.application.name的值,后续会讲到
  • 参数profiles:这个不需要多说,用过SpringBoot的应该都知道
  • 参数label:如果是git或svn,label对应的是分之;如果是本地文件系统,label对应子目录名,例如:classpath:properties/{label}

2. Config-Client端

(1) pom:

parent依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
</parent>
dependencyManagement
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
dependencies
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
</dependencies>

(2) 启动类:有@SpringBootApplication即可

(3) bootstrap.yml,注意这里不是application.yml哦

spring:
  application:
    name: ServiceGateway
  cloud:
    config:
      uri: http://127.0.0.1:51011
      profile: company
  • spring.application.name:项目名称
  • spring.cloud.config.uri:ConfigServer的地址
  • spring.cloud.config.profile:这个不需要多说,用过SpringBoot的应该都知道

根据以上配置内容,可得知最终调用ConfigServer的接口地址为:http://127.0.0.1:51011/ServiceGateway-company.yml

3. 多个Config-Client端公用配置

如果多个Config-Client中有部分配置是相同的,为了避免出现重复代码,我们可以把这部分相同的配置抽出来单独做一个配置文件,命名为:application.yml、application.properties,或application-.yml、application-.properties,然后放到Config-Server端即可。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
489
获赞与收藏
43

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消