6.2:配置中心搭建

一. 搭建配置中心

  1. 准备git仓库:

    1. https://gitee.com/waitforxy/config_cloud.git

    2. 用户名:794666918@qq.com

    3. 密码:xdclass.net123

  2. 新建springboot应用,创建config-server

  3. pom.xml中添加依赖

<dependencies>

    <dependency>

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

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

    </dependency>

    <dependency>

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

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

    </dependency>

    <dependency>

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

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

        <scope>test</scope>

    </dependency>

</dependencies>

 

  1. 添加application.yml

#服务名称

spring:

  application:

    name: config-server

  cloud:

    config:

      server:

        git:

          uri: https://gitee.com/waitforxy/config_cloud

          username: 794666918@qq.com

          password: xdclass.net123

          timeout: 5

          default-label: master

 

#服务的端口号

server:

  port: 9100

 

#指定注册中心地址

eureka:

  client:

    serviceUrl:

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

 

  1. 启动类添加注解

@SpringBootApplication

@EnableConfigServer

public class ConfigServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigServerApplication.class, args);

    }

}

 

  1. 访问方式(一定要注意语法,如果有问题,会出错)

    1. 例子 http://localhost:9100/product-service.yml

    2. 类型

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

      2. /{name}-{profiles}.yml

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

      4. /{label}/{name}-{profiles}.yml

    3. 变量解释:

      1. name 服务器名称

      2. profile 环境名称,开发、测试、生产

      3. lable 仓库分支、默认master分支

    4. 注意:git放的yml但是后缀更改properties,git会自动转码

 


二. 配置客户端

  1. 添加依赖

<dependency>

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

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

</dependency>

 

  1. 修改对应服务的配置文件,把application.yml 改为 bootstrap.yml

#指定注册中心地址

eureka:

  client:

    serviceUrl:

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

 

 

#服务的名称

spring:

  application:

    name: product-service

  #指定从哪个配置中心读取

  cloud:

    config:

      discovery:

        service-id: CONFIG-SERVER

        enabled: true

      profile: test

      #建议用lable去区分环境,默认是lable是master分支

      #label: test

 

  1. 注意点:

    1. 配置文件要用bootstrap.yml

    2. 默认读取文件名是 服务名称

 


三. 配置中心动态刷新

  1. 在config-clinet的pom.xml中新增spring-boot-starter-actuator监控模块,其中包含了/refresh刷新API。

<dependency>

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

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

</dependency>

 

  1. 通过上面的介绍,大家不难想到,该功能还可以同Git仓库的Web Hook功能进行关联,当有Git提交变化时,就给对应的配置主机发送/refresh请求来实现配置信息的实时更新。但是,当我们的系统发展壮大之后,维护这样的刷新清单也将成为一个非常大的负担,而且很容易犯错,那么有什么办法可以解决这个复杂度呢?后续我们将继续介绍如何通过Spring Cloud Bus来实现以消息总线的方式进行通知配置信息的变化,完成集群上的自动化更新。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值