SpringCloud配置中心——ConfigServer搭建

在实际应用中,一个系统的每个微服务都会有相同的配置,如数据库配置信息等等。
为了将每个微服务的公共配置可以抽取出来。SpringCloud提供了Config配置中心的配置,作为配置中心(ConfigServer),提供给微服务(ConfigClient)读取并且加载配置。
使用git仓库存放配置文件,SpringCloudConfig读取到之后会自动加载配置。
配置文件可以是.yml、.properties后缀,SpringCloudConfig读取到之后会自动加载。

###搭建ConfigServer
######一、将写好的配置文件push到git仓库中
配置文件命名规则:
应用名-环境名.yml或者应用名-环境名.properties
push文件名为test-dev.yml的文件到git,文件内容:

serverAddress:
  activeMQ: http://192.168.10.116:61616
  redis: http://192.168.10.177:7777
  nginx: http://192.168.10.155:80
二、创建SonfigServer服务

1、添加ConfigServer依赖,由于要注册到Eureka中被其他服务发现,因此要添加EurekaClient依赖

<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>

2、主程序中添加注解@EnableConfigServer

package com.huiche51.www;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class);
    }
}

3、application.yml配置

spring:
  application:
    name: config-server #应用名称
  cloud:
    config:
      server:
        git:
          uri: root@192.168.2.220:/home/git/server-config.git #git远程仓库的地址
          search-paths: test
          basedir: /media/gao/work/project/java/Study/server-config #git本地仓库地址
          #此仓库如果是私有仓库,则需要用户名与密码
          username: root
          password: huiche51.

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8866/eureka/

server:
  port: 8899

4、服务创建完成

客户端访问并加载ConfigServer配置文件

1、作为Config客户端,需要添加ConfigClient的依赖

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

2、创建bootstrap.yml文件。
bootstrap.properties的优先级高于application.properties,服务启动时,会优先加载bootstrap.properties,所以,对于一些优先加载的配置信息,如数据库信息、配置中心信息等,应存放在bootstrap.properties文件中,可被正常加载。

spring:
  cloud:
    config:
      name: test #对应{application}部分
      profile: dev #对应{profile}部分
      label: master #对应{label}部分
      discovery:
        enabled: true #开启Config服务发现支持(从配置中心读取文件)
        service-id: config-server #目标Config服务端名称


eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8866/eureka/ #指定Eureka服务的地址

属性解释:
name: 指定git仓库中某个配置文件的应用名。
**profile:**指定git仓库中某个配置文件的环境名。
**label:**指定目标配置文件在git仓库中的分支名。

运行测试

1、先启动Eureka服务。
2、启动配置中心服务。
3、启动Config客户端
此时config客户端通过eureka访问配置中心,配置中心从git中获取配置文件给config客户端并且加载配置。

ConfigClient动态更新配置文件

*每个ConfigClient(微服务)只有在启动时会从配置中心加载配置,这样如果微服务需要更换配置时就要重启服务。有没有办法可以不重启就可以重新加载最新配置呢?
有滴,ConfigRefresh依赖帮到你。
*

一、配置

引入Refresh依赖

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

.yml文件配置启用refresh

management:
  endpoints:
    web:
      exposure:
        include: refresh
二、触发更新

1、新的配置文件push到git后,给需要重新加载配置的Config客户端发送一个”/actuator/refresh“的post请求

Config客户端就会重新加载新的配置文件

评论

暂无评论