配置中心Config

引入依赖

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.6.RELEASE</version>

</parent>

<properties>

<spring-cloud.version>Finchley.SR2</spring-cloud.version>

</properties>

<dependencies>

<!-- Eureka-Client 依赖 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

<!-- Config-Server 依赖 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-config-server</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<!-- SpringCloud 版本控制依赖 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>${spring-cloud.version}</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

启动类:

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication

@EnableConfigServer

public class ConfigCenterApplication {

public static void main(String[] args) {

SpringApplication.run(ConfigCenterApplication.class, args);

}

}

添加配置:application.yml

server:

port: 9001

spring:

application:

name: config-center

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8761/eureka/

创建Git仓库

配置Git仓库

spring:

cloud:

config:

server:

git:

uri: https://gitee.com/pengjunlee/config-cloud.git

username: 你的码云账号

password: 你的账号密码

搭建客户端

引入依赖

<!-- Starter-Config 依赖 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

</dependency>

修改配置

先将product-service中的application.yml文件改名为bootstrap.yml(bootstrap.yml在应用上下文启动阶段加载,比application.yml早),然后再对其内容进行修改:

# 设置服务(应用)名称

spring:

application:

name: product-service

# 指定用于获取配置的配置中心服务(应用)名称

cloud:

config:

discovery:

enabled: true

serviceId: config-center

profile: dev

# 指定分枝版本,默认为master

label: master

# 指定注册中心地址

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8761/eureka/

在Git仓库创建配置文件

客户端通过发送Http请求来从配置中心读取配置,这些Http请求的URI遵循以下规则:

/{name}-{profiles}.properties

/{name}-{profiles}.yml || /{name}-{profiles}.yaml

/{label}/{name}-{profiles}.properties

/{label}/{name}-{profiles}.json

/{name}/{profiles}/{label:.*}

/{name}-{profiles}.json

/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml

/{name}/{profiles:.*[^-].*}

/{name}/{profile}/{label}/**

/{name}/{profile}/{label}/**

/{name}/{profile}/**

其中各个参数的含义如下:

name 服务的ID,即spring.application.name的值,本例中为 product-service;

profiles 激活的profile,通过spring.cloud.config.profile指定,本例中为 dev;

label 分枝的版本,通过spring.cloud.config.label指定,本例中为默认值 master;

接下来我们需要按照上述规则,在Git仓库的相应位置创建配置文件。根据bootstrap.yml中的配置,资源请求地址可以为 /master/product-service-dev.yml 。

在Git仓库的master分枝中创建product-service-dev.yml和product-service-test.yml两个文件,分别将服务的启动端口指定为8771和8772:

# product-service-dev.yml

server:

port: 8771

# product-service-test.yml

server:

port: 8772

文件创建完成之后,启动配置中心,先在浏览器对两个文件进行访问。

此时,启动product-service将读取http://localhost:8888/product-service-dev.yml中的配置,即启动端口为8771。若将 bootstrap.yml中的spring.cloud.config.profile的值设置为test,则将读取http://localhost:8888/product-service-test.yml中的配置,应用的启动端口也会相应地变为8772,证明从配置中心读取配置成功。

二、工作流程

以上两张图对于理解自动刷新的工作流程很关键

1.在gitee配置文件仓库修改对应微服务(e.g. ServiceA)的配置文件并提交

2.手动触发自动刷新,使用Postman发送post请求到配置中心微服务例如http://localhost:9000/actuator/busrefresh

3.添加了sprint-boot-starter-actuator依赖的配置中心微服务在接收到Postman发送的请求后,将消息发送给spring cloud bus

4.spring cloud bus 将消息传递给消息代理 rabbitmq-server,加入消息队列

5. rabbitmq的消费端即微服务(ServiceA)监听到消息后消费了消息,主动发送了刷新配置文件的请求到配置中心微服务

6. 该配置文件对应的微服务(ServiceA)实现了自动刷新配置而无须重启微服务

自动刷新 客户端调用下面接口 实现配置的自动同步

http://127.0.0.1:3355/actuator/refresh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JAVA终结者

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

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

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

打赏作者

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

抵扣说明:

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

余额充值