1.简介

当服务比较多的时候,每个服务都有自己的配置文件;当配置文件需要变更的时候,需要重启服务才能使配置文件生效,不能做到实时更新。springcloud有自己的配置中心config,该组件支持将配置文件放置于本地也支持将配置文件放置于git。

2.配置中心服务端

新建一个项目,引入配置依赖,其作为配置中心服务端,也可以当做一个微服务注册到注册中心,当服务比较多的时候就可以启用多个服务,实现高可用。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>XiaYuApplication</artifactId>
        <groupId>com.xiayu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>configcenter</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
    </dependencies>
</project>

加入@EnableConfigServer注解

package com.xiayu.config.center;

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

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

配置中心配置文件

spring:
  application:
    name: configcenter
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/niruoanh/xiayuapplication #uri表示git公共仓库地址 私有仓库地址需要设置用户名及密码,
#由于github.com访问非常慢,换成国内的gitee
          search-paths: /config
# /config 配置文件的所在目录
#          username: root
#          password: root
      label: master
#指定分支

server:
  port: 8087
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8083/eureka/

配置文件的文件名:服务名+profiles.yml
如:CommonIntegration-dev.yml
配置文件和路径映射

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

3.配置中心客户端

引入依赖

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

不需要开启配置客户端注解,直接在bootstrap.yml文件中加入

spring:
  application:
    name: CommonIntegration
  cloud:
    config:
      label: master #对应配置中心的label
#      uri: http://localhost:8087 #配置中心服务地址
      profile: dev #profile
      discovery:
        enabled: true #设置从配置中心获取数据
        service-id: configcenter  #配置中心服务名称
server:
  port: 8091
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8083/eureka/ #注册中心url

启动项目,首先会去配置中心,获取该服务的配置文件

(在搭建过程中,遇到java.lang.IllegalStateException: No instances found of configserver (configserver)错误
,解决方法将application.yml文件中的注册中心的配置移到bootstrap.yml文件中即可。)

4.配置自动更新及配置文件加密

A.如果配置文件进行了修改,然后提交到git上了,应用修改后的配置文件就需要将服务重新启动,这么做非常麻烦,springcloud提供了一种自动更新配置的方法。

B.配置文件中有很多保密性的字段,如数据库用户名和密码,对象存储的key及秘钥,发送短信key及秘钥等等。所以需要对配置文件进行加密,对配置文件加密有对称加密及非对称加密。

详见下篇springcloud教程之消息总线

03-05 22:29