spring cloud 2.x版本 Config配置中心教程

前言

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 java

本文基于前面的文章eureka-server的实现。 参考git

概述

在分布式系统中,因为服务数量巨多,为了方便服务配置文件统一管理,因此须要分布式配置中心组件。Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。github

本篇涉及项目的结构为一个Config Server单机模式和适用于连接Git仓库,一个Config Client经过server来展现配置文件数据。 同时使用两个bus来模拟配置文件刷新。web

建立config-server工程

Config-server功能算法

  • 用于外部配置的http,基于资源的api
  • 加密和解密属性
  • 使用可嵌入spring boot的应用程序

1.1 建立配置中心server:config-server

1.2 添加config-server的pom.xml相关依赖

<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>
复制代码

1.3 添加config-server的application添加配置信息

#加载本地文件配置
spring:
 application:
 name: config-server
 profiles:
 active: native #加载本地配置
 cloud:
 config:
 server:
 native: #加载本地目录文件
 search-locations: /Users/xxxx/config-server

server:
 port: 13081

eureka:
 instance:
 hostname: eureka1.client.com
 lease-renewal-interval-in-seconds: 5
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
复制代码

1.4 启动类ConfigServerApplication增长注解

package spring.cloud.demo.configserver;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}
复制代码

至此,一个单机本地config-server就搭建完成spring

1.5 建立配置中心client:config-client

1.6 添加config-client的pom相关依赖

<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>
复制代码

1.7添加config-client的bootstrap.yml相关配置(注意这里不是application.yml)

bootstrap.ymljson

spring:
 application:
 name: config-client
 cloud:
 config:
 label: master
 profile: dev
 fail-fast: true
 uri: http://localhost:13081 #经过域名访问配置中心服务端
 discovery:
 enabled: true
eureka:
 instance:
 hostname: eureka1.client.com
 lease-renewal-interval-in-seconds: 5
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
复制代码

1.8新建从服务端请求的配置文件config-client-dev.yml

客户端从服务端获取资源配置的路径规则:bootstrap

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

本文采用第二种规则。api

配置内容:服务器

spring:
 application:
 name: config-client

server:
 port: 52601

eureka:
 instance:
 hostname: eureka1.client.com
 lease-renewal-interval-in-seconds: 5
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

info: local-config-client-dev
复制代码

将新建的config-client-dev.yml放到config-server配置的/Users/xxxx/config-server目录下,若是config-server没有配置目录,默认使用resources目录下。

1.9 ConfigClientApplication增长注解

package spring.cloud.demo.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

}

复制代码

2.0 建立测试类

在config-client建立测试Controller:ConfigClientController

package spring.cloud.demo.configclient.controller;

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

/** * 测试是否能获取到配置信息的controller * @auther: maomao * @DateT: 2019-09-17 */
@RestController
public class ConfigClientController {

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

    @RequestMapping("/config/info")
    public String info() {
        return info;
    }

}
复制代码

2.1 启动相关服务

分别启动eureka-server、config-server、config-client。访问:http://localhost:52601/config/info,显示以下

证实本地配置文件已经生效。

2.2 从远程git加载资源配置文件

修改config-server的application.yml配置文件:

##加载本地文件配置
#spring:
# application:
# name: config-server
# profiles:
# active: native #加载本地配置
# cloud:
# config:
# server:
# native: #加载本地目录文件
# search-locations: /Users/fengfujie/config-server

#加载远程git仓库资源文件
spring:
 application:
 name: config-server
 cloud:
 config:
 server:
 git:
          # 配置git仓库的地址
 uri: https://github.com/fengfujie25/sping-cloud-config
          # git仓库的帐号
 username: xxxxxx
          # git仓库的密码
 password: xxxxxx

server:
 port: 13081

eureka:
 instance:
 hostname: eureka1.client.com
 lease-renewal-interval-in-seconds: 5
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
复制代码

从新启动config-server服务。而后刷新http://localhost:52601/config/info地址,显示以下

证实已经成功获取了远程git资源的配置信息。

2.3 经过服务名称访问config-server

以上配置都是经过域名访问的config-server,为了保证系统的高可用,由于生产环境的配置服务中心都是集群配置,全部客户端才经过服务名称来访问。

修改config-client的bootstrap.yml

spring:
 application:
 name: config-client
 cloud:
 config:
 label: master
 profile: dev
 fail-fast: true
      #uri: http://localhost:13081 #经过域名访问配置中心服务端
 discovery:
 enabled: true
 service-id: config-server #经过服务访问配置中心服务端

eureka:
 instance:
 hostname: eureka1.client.com

 lease-renewal-interval-in-seconds: 5
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
复制代码

spring.cloud.config.discovery.service-id:经过服务名称访问配置中心服务端

从新启动config-client.并访问http://localhost:5301/config/info,显示结果同【2.2】则表明配置信息已生效,证实是经过服务名称访问的config-server.

至此,spring cloud集成config的配置就所有完成。可是存在一个问题,若是修改远程git仓库的资源配置,项目并不会刷新,因此配置信息是不生效的。

2.4 动态刷新config-server配置

动态刷新config-serve配置方式

  • 基于RabbitMQ动态刷新
  • 原生刷新(伪动态刷新)

本文采用比较简单的原生刷新方式。

2.4.1增长相关pom.xml依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制代码
2.4.2 在ConfigClientController增长@RefreshScope注解
package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/** * @auther: maomao * @DateT: 2019-09-17 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${info:error}")
    private String info;

    @RequestMapping("/config/info")
    public String info() {
        return info;
    }

}
复制代码

此方式同时还要修改@Value注解内容为@Value("${info:error}"),由于刷新的时候须要配置信息有默认值,不然会报错。

2.4.3 从新启动config-client

访问http://localhost:5301/config/info,看服务是否能够正常访问。

而后能够修改git资源仓库中的配置信息。

  • 刷新http://localhost:5301/config/info,结果显示:

证实refresh已经生效。

此方式每次都须要手动刷新一下才行,比较麻烦。GitHub提供一种Webhooks方法能够实现不用每次手动刷新。

Payload URL: 触发后回调的URL

Content type: 数据格式,两种通常使用json

Secret: 用做给POST的Body加密的字符串,采用HMAC算法

Events: 触发的事件列表

事件类型 描述
Just the push event 仓库有push的时候触发,默认事件
Send me everything 派我来一切
Let me select individual events 选择个别事件

这样咱们就能够利用Webhook的机制去触发客户端的更新,可是当客户端愈来愈多的时候,Webhook机制也不够优雅,每次增长客户端都须要改动Webhook也不现实。

其实,Spring cloud给了咱们更好的解决方案-spring cloud bus。

spring cloud bus后续更新。

总结

本文简单的实现了config-server和config-client的单机和远程git仓库的配置的调用以及配置信息的简单的动态更新。

代码地址


《Srping Cloud 2.X小白教程》目录

转载请注明出处,

  • 联系方式:4272231@163.com
相关文章
相关标签/搜索