Spring Cloud Hoxton 版本微服务项目搭建 config 配置中心客户端

Spring Cloud Hoxton 版本微服务项目搭建 config 配置中心客户端


前言

在上一篇文章博主已经讲解了项目如何创建,不会的话可以前往学习,传送门:Spring Cloud Hoxton 版本微服务项目搭建eureka注册中心 以及 Spring Cloud Hoxton 版本微服务项目搭建 admin 监控管理中心 以及 Spring Cloud Hoxton 版本微服务项目搭建 config 配置中心客户端。

本篇用来讲解–Spring Cloud Hoxton 版本微服务项目搭建 config 配置中心!


创建config-client模块

我们创建一个config-client模块来从config-server获取配置。

在pom.xml中添加相关依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</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-web</artifactId>
</dependency>

bootstrap.yml 中进行配置中进行配置

配置客户端启动时,它将通过配置服务器(通过引导配置属性spring.cloud.config.uri)绑定,并使用远程属性源初始化 Spring Environment。需要使用配置中心的客户端应用程序需要 bootstrap.yml 配置。bootstrap.yml 会在 application.yml 之前被加载。

eureka:
  instance:
    # 每隔5s发送一次心跳
    lease-renewal-interval-in-seconds: 5
    # 告知服务端10秒还未收到心跳的话,就将该服务移除列表
    lease-expiration-duration-in-seconds: 10
    # 健康检查路径
    # health-check-url-path: /actuator/health
  client:
    registry-fetch-interval-seconds: 5 # 默认为30秒
    serviceUrl:
      #eureka注册中心地址
      defaultZone: http://localhost:8888/eureka/
      #defaultZone: http://localhost:8888/eureka/,http://localhost:8889/eureka/,http://localhost:8890/eureka/

# Admin 管理配置
management:
  endpoints:
    web:
      exposure:
        # 开启了 'refresh' 端点
        include: '*'
  endpoint:
    health:
      show-details: always

server:
  #项目端口号
  port: 4010
  tomcat:
    max-connections: 200
    max-threads: 300
    min-spare-threads: 0
    uri-encoding: UTF-8

spring:
  application:
    #服务名称,随便写
    name: resource-center
  profiles:
    active: master
  cloud:
    # config客户端配置
    config:
      # 分支名称
      label: master
      # 启用配置后缀名称
      profile: dev
      # 配置文件名称
      name: config
      # 配置中心地址(子项目地址)
      uri: http://localhost:9010
      # 用于向配置中心进行安全认证,需要和配置中心的安全认证账户密码一致
      username: root
      password: 123456

hystrix:
  metrics:
    enabled: true
    polling-interval-ms: 2000

添加ConfigClientController类用于获取配置

package com.cyj.resourcecenter.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 配置中心配置控制器
 * @BelongsProject: Family
 * @BelongsPackage: com.cyj.configcenter.controller
 * @Author: ChenYongJia
 * @CreateTime: 2020-01-02 11:58
 * @Email: [email protected]
 * @Version: 1.0
 */
@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }

}

从配置中心获取配置

访问 http://localhost:4010/configInfo,可以获取到dev分支下dev环境的配置;

config info for dev(master)

在这里插入图片描述

获取子目录下的配置

我们不仅可以把每个项目的配置放在不同的Git仓库存储,也可以在一个Git仓库中存储多个项目的配置,此时就会用到在子目录中搜索配置信息的配置。

首先我们需要在 config-center 配置中心中添加相关配置,用于搜索子目录中的配置,这里我们用到了 application 占位符,表示对于不同的应用,我们从对应应用名称的子目录中搜索配置,比如config子目录中的配置对应config应用;

spring:
  cloud:
    config:
      server:
        git: 
          search-paths: '{application}'

访问http://localhost:4010/configInfo进行测试,可以发现获取的是config子目录下的配置信息。

config info for config dir dev(master)

刷新配置

当Git仓库中的配置信息更改后,我们可以通过SpringBoot Actuator的refresh端点来刷新客户端配置信息,以下更改都需要在config-client中进行。

在pom.xml中添加Actuator的依赖:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在bootstrap.yml中开启refresh端点:

management:
  endpoints:
    web:
      exposure:
        include: 'refresh'

在ConfigClientController类添加@RefreshScope注解用于刷新配置:

package com.cyj.resourcecenter.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description: 配置中心配置控制器
 * @BelongsProject: Family
 * @BelongsPackage: com.cyj.configcenter.controller
 * @Author: ChenYongJia
 * @CreateTime: 2020-01-02 11:58
 * @Email: [email protected]
 * @Version: 1.0
 */
@Slf4j
@RefreshScope // 用于刷新配置
@RestController
public class ConfigClientController {

    @Resource
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        log.info("configInfo=================>"+configInfo);
        return configInfo;
    }

}

重新启动config-client后,调用refresh端点进行配置刷新:

使用post请求 http://localhost:4010/actuator/refresh

在这里插入图片描述

访问http://localhost:9001/configInfo进行测试,可以发现配置信息已经刷新。

update config info for config dir dev(master)

其更多操作自己尝试一下!后面博主会给一些使用分享。(上一篇带有开启安全认证)


最后

  • 更多参考精彩博文请看这里:《陈永佳的博客》

  • 喜欢博主的小伙伴可以加个关注、点个赞哦,持续更新嘿嘿!

发布了397 篇原创文章 · 获赞 973 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Mrs_chens/article/details/103801631