SpringCloud之Config组件

1.Config简介

一句话,对配置进行统一管理

2.Config工作流程

首先我们需要开发一个ConfigServer服务,所有微服务的配置文件写在Github或者Gitee仓库上。运行服务时,Config Server会从远程仓库拉取配置到本地,然后其他微服务从Config Server中拿去自己的配置。

A.ConfigServer的搭建

  1. 引入依赖(因为需要注册到服务中心,所以这里用了consul)
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
		<!--config server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
  1. 配置文件
server.port=12345

//consul配置
spring.cloud.consul.port=8500
spring.cloud.consul.host=localhost
//服务名
spring.application.name=CONFIGSERVER

//config配置
spring.cloud.config.server.git.uri=https://gitee.com/xiang_pei/config.git    //gitee仓库地址
spring.cloud.config.server.default-label=master       //使用哪个分支
  1. 入口类加上@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}

B.ConfigClient的搭建(即微服务搭建)

  1. 引入依赖,SpringCloud 2020.0.*版本将bootstrap配置删除了,需要把它引进来,不然客户端跑不起来
    参考:No spring.config.import property has been defined
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
		<!--config client的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        
        <!--重新导入bootstrap-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>
  1. 配置文件(虽然说他们的配置文件都在gitee上管理着,但是必须要有一些配置才能实现config的效果)配置文件名必须是bootstrap.peoperties或者bootstrap.yml,首先我们应当知道SpringBoot加载配置文件的优先级bootstrap是大于application的。那为什么Config client需要用优先级高的配置文件呢?因为Config Client的配置文件中需要配置Config Server,这样才能保证Client能够拿到Server拉取下来的配置。至于bootstrap和application的区别,网上各有说法,有的说优先级区别,我学的spring cloud视频老师说bootstrap会等拉取完配置之后才启动。我思来想去,这两个说法都很勉强,不能说明为什么要使用。经过我一番思考,应该是这样的(仅仅是推测),springboot读取配置会从bootstrap开始,然后application,没有bootstrap就直接application,也就是优先级的说法。所以这里使用bootstrap是这样的:springboot读取到bootstrap会完成向ConfigServer获取自己的配置过程(这里也就是拿到application配置文件),然后执行application配置文件。所以需要使用bootstrap先拿取到配置!直接使用application就直接执行报错
//配置consul
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.application.name=CONFIGCLIENT

//配置configserve,为什么是以服务名的方式而不是写死uri,因为集群!
spring.cloud.config.discovery.service-id=CONFIGSERVER   //从名为某某的服务名的configserver获取配置
spring.cloud.config.discovery.enabled=true     //开启client去server拉取配置功能

//这里我远程的配置文件叫 config-dev.properties,大家比对阅读就好
spring.cloud.config.name=config       //要拉取配置的名称
spring.cloud.config.label=master		//分支
spring.cloud.config.profile=dev			//哪个环境
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为了我的架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值