详解SpringCloud Config配置中心

作者:liuhh 时间:2021-06-18 04:38:34 

一、创建Config配置中心项目

1.添加依赖


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

2.启动类,需要添加@EnableConfigServer


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

/**
* @author fusw
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterApplication {

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

}

3.配置文件


eureka.instance.appname=base-iov-config
security.user.name=test
security.user.password=test
# GitLab 的配置方式,必须有 .git 后缀
# 配置默认 git 仓库的地址
spring.cloud.config.server.git.uri=http://xxxx/config-repo.git
# git 仓库地址下的相对地址,可以配置多个,用“,”分割
spring.cloud.config.server.git.search-paths=*
#配置中心clone仓库配置文件后存放的地址
spring.cloud.config.server.git.basedir=/data/deploy/config-repo
spring.cloud.config.server.git.force-pull=true
# git 仓库的账号
spring.cloud.config.server.git.username=test
# git 仓库的密码
spring.cloud.config.server.git.password=test

# 配置 其它git 仓库的地址 spring.cloud.config.server.git.repos.x.uri, iot为例
spring.cloud.config.server.git.repos.iot.uri=http://xxxx/iot/config-repo.git
spring.cloud.config.server.git.repos.iot.search-paths=*
spring.cloud.config.server.git.repos.iot.basedir=/data/deploy/config-repo
spring.cloud.config.server.git.repos.iot.force-pull=true
# git 仓库的账号
spring.cloud.config.server.git.repos.iot.username=test
# git 仓库的密码
spring.cloud.config.server.git.repos.iot.password=test
#前缀一定要配置,用来和默认仓库区分
spring.cloud.config.server.git.repos.iot.pattern=Iot*

#注册中心
eureka.client.serviceUrl.defaultZone=http://xxx/eureka/

二、git 存放配置的仓库

创建一个git仓库用来存放各项目配置,可以在项目一级目录根据项目创建文件夹(各项目文件夹的名称可以随便起,Config配置中心只根据配置文件名找配置),然后各个项目文件夹存放不同环境的配置文件,

例如:

Iot-TestProject-dev.yml
Iot-TestProject-prd.yml
Iot-TestProject-test.yml

最好不要放置test.yml类似的默认配置,如果在各个项目配置文件中没有用spring.profiles.active指明使用的配置文件,那么会加载默认的配置文件。

三、各个SpringCloud的项目配置接入配置中心

在resource下的bootstrap.properties配置文件中,配置中心的相关配置如下


# 使用默认仓库的配置文件
spring.cloud.config.name=TestProject
# 使用iot厂库的配置文件
#spring.cloud.config.name=Iot-TestProject
#使用哪个环境的配置文件,和上面的配置配合,决定了使用哪一个配置文件:TestProject -test.yml
spring.cloud.config.profile=test

# 对应 存放配置文件仓库的git分支
spring.cloud.config.label=master

spring.cloud.config.username=test
spring.cloud.config.password=test

# 开启 Config 服务发现支持
spring.cloud.config.discovery.enabled=true
# 指定 Server 端的 name,也就是 Server 端 spring.application.name 的值
spring.cloud.config.discovery.serviceId=base-iov-config

来源:https://segmentfault.com/a/1190000018437089

标签:SpringCloud,Config,配置中心
0
投稿

猜你喜欢

  • SpringBoot实现动态定时任务的示例代码

    2022-06-22 19:27:39
  • 使用springCloud+nacos集成seata1.3.0搭建过程

    2022-06-19 02:48:47
  • springboot实现通过路径从磁盘直接读取图片

    2023-09-01 03:01:01
  • Java中的functor实现

    2023-11-26 08:30:50
  • IDEA下载并大学生edu邮箱认证免费使用教程(图文)

    2022-10-10 14:23:02
  • java中lambda表达式简单用例

    2023-11-24 23:17:52
  • 一文搞懂MyBatis多数据源Starter实现

    2023-07-19 03:34:22
  • spring mvc中@RequestBody注解的作用说明

    2022-04-07 14:15:02
  • 详解SpringBoot注入数据的方式

    2022-05-09 06:34:24
  • Java IO流之字符流的使用详解

    2023-10-18 11:28:08
  • Java定时器Timer简述

    2023-07-20 19:17:16
  • Java实战之用Swing实现通讯录管理系统

    2021-08-15 08:24:09
  • iOS实现从背景图中取色的代码

    2023-07-06 15:18:32
  • java @Value(

    2023-10-05 02:54:47
  • Maven打包jar包没有主属性问题解决方案

    2023-11-25 06:56:32
  • Springcloud Nacos基本操作代码实例

    2023-07-07 18:24:22
  • Java多线程编程详细解释

    2022-12-22 10:19:41
  • 关于SpringMVC在Controller层方法的参数解析详解

    2022-10-29 01:06:10
  • Java实现简单邮件发送功能

    2023-08-15 00:53:23
  • java.lang.NoClassDefFoundError错误解决办法

    2021-12-29 03:52:27
  • asp之家 软件编程 m.aspxhome.com