您的位置:首页 > 编程语言 > Java开发

Spring Cloud Config 集中式配置

2018-12-22 11:35 295 查看

本指南通过Spring cloud config服务器引导你建立和消费配置。

你要构建什么?

你需要设置一个config服务器,然后创建一个在启动时消费配置的client,并且不需要重启client刷新配置。

你需要什么?

大约15分钟时间

你喜好的文本编辑器或IDE

JDK1.8以上

Gradle4+或Maven3.2+

如何完成本指南?

用Maven构建

首先你要设置一个基本的构建脚本。你可以使用任何你喜欢的构建系统来构建spring应用。

创建目录结构

在你选择的项目目录里,创建下面子目录结构,例如,在*nix 系统中使用命令 :

mkdir -p src/main/java/hello
└── src

    └── main

        └── java

            └── hello

 

为了快速地开始,这里已经做好了服务器和客户端应用的配置

configuration-service/pom.xml

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

       <modelVersion>4.0.0</modelVersion>

       <groupId>com.example</groupId>

       <artifactId>configuration-service</artifactId>

       <version>0.0.1-SNAPSHOT</version>

       <packaging>jar</packaging>

       <parent>

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

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

              <version>2.0.5.RELEASE</version>

              <relativePath/> <!-- lookup parent from repository -->

       </parent>

       <properties>

              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

              <java.version>1.8</java.version>

       </properties>

       <dependencies>

              <dependency>

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

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

              </dependency>

              <dependency>

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

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

                     <scope>test</scope>

              </dependency>

       </dependencies>

       <dependencyManagement>

              <dependencies>

                     <dependency>

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

                            <artifactId>spring-cloud-dependencies</artifactId>

                            <version>Finchley.SR2</version>

                            <type>pom</type>

                            <scope>import</scope>

                     </dependency>

              </dependencies>

       </dependencyManagement>

       <build>

              <plugins>

                     <plugin>

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

                            <artifactId>spring-boot-maven-plugin</artifactId>

                     </plugin>

              </plugins>

       </build>

</project>

configuration-client/pom.xml

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

       <modelVersion>4.0.0</modelVersion>

       <groupId>com.example</groupId>

       <artifactId>configuration-client</artifactId>

       <version>0.0.1-SNAPSHOT</version>

       <packaging>jar</packaging>

       <parent>

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

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

              <version>2.0.5.RELEASE</version>

              <relativePath/> <!-- lookup parent from repository -->

       </parent>

       <properties>

              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

              <java.version>1.8</java.version>

       </properties>

       <dependencies>

              <dependency>

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

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

              </dependency>

              <dependency>

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

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

              </dependency>

              <dependency>

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

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

              </dependency>

              <dependency>

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

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

                     <scope>test</scope>

              </dependency>

       </dependencies>

       <dependencyManagement>

              <dependencies>

                     <dependency>

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

                            <artifactId>spring-cloud-dependencies</artifactId>

                            <version>Finchley.SR2</version>

                            <type>pom</type>

                            <scope>import</scope>

                     </dependency>

              </dependencies>

       </dependencyManagement>

       <build>

              <plugins>

                     <plugin>

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

                            <artifactId>spring-boot-maven-plugin</artifactId>

                     </plugin>

              </plugins>

       </build>

</project>

建立一个Config 服务器

你首先需要一个作为Spring应用之间媒介的Config服务器和一个配置文件的版本控制仓库。

你可以使用Spring Cloud的@EnableConfigServer注解去建立一个能够和其他应用对话的Config服务器。

这是一个常规的Spring boot应用程序,添加一个注解来启用config服务器。

configuration-service/src/main/java/hello/ConfigServiceApplication.java


 

package hello;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer

@SpringBootApplication

public class ConfigServiceApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigServiceApplication.class, args);

    }

}

Config服务器需要知道管理哪个仓库。

这里有几个选择,但是我们将使用基于Git的文件系统仓库。

你可以简单的把Config服务器指向Github或者GitLab仓库。

在文件系统里,创建一个新的目录,用git init初始化它。

然后添加一个a-bootiful-client.properties文件到Git仓库。

还要保证用git commit 来提交它。之后,你可以用Spring Boot应用连接到Config服务器,将Spring boot 应用的spring.application.name属性标识为a-bootiful-client。

这是Config服务器知道如何将配置的设置发送到具体客户端的关键。

它还将发送所有在Git仓库里的application.properties或者application.yml文件里的全部配置值。

在特殊名称的文件中的属性键(像a-bootiful-client.properties)会覆盖application.properties或者application.yml的对应值。

添加一个简单的属性和值,message = Hello world到新创建的a-bootiful-client.propertie文件,然后git commit 修改它。

通过在configuration-service/src/main/resources/application.properties的属性spring.cloud.config.server.git.uri 指定Git仓库的路径。

 

当你在同一台机子上运行本项目和其他的Spring boot 应用的时候,确保server.port也指定了不同的端口值,以免端口冲突。

configuration-service/src/main/resources/application.properties

server.port=8888

spring.cloud.config.server.git.uri=${HOME}/Desktop/config

 

在本例中,我们使用基于文件的位于${HOME}/Desktop/config的git仓库,你可以通过创建一个新的目录,很容易的用

git 提交一个properties 和YAML 文件

$ cd ~/Desktop/config

$ find .

./.git

...

./application.yml

或者使用一个远程的git仓库,例如 github,如果你改变了应用的配置文件,改为指向该文件代替。

 

Config客户端从Config服务器读取配置文件

现在我们已经创建了Config服务器,让我们创建一个新的Spring boot应用,使用Config服务器加载它自己的配置。不用重启JVM 去刷新其配置以按需反映对配置服务器的更改。为了连接到Config服务器,需要添加org.springframework.cloud:spring-cloud-starter-config依赖。

Spring 能看得配置属性文件,就像从自己的application.properties 或 application.yml 或其他PropertySource 文件一样加载任何属性

Config客户端的配置属性需要从Config服务器读取应用配置的其他内容前能够读取,在启动阶段,指定客户端的spring.application.name为

a-bootiful-client。在configuration-client/src/main/resources/bootstrap.properties用spring.cloud.config.uri指定Config服务器的位置,这个文件比其他配置文件更早加载。

 

configuration-client/src/main/resources/bootstrap.properties

spring.application.name=a-bootiful-client

# N.B. this is the default:

spring.cloud.config.uri=http://localhost:8888

我们还想启动refresh端点,以此我们可以演示动态改变配置:

configuration-client/src/main/resources/application.properties

management.endpoints.web.exposure.include=*

客户端可以使用传统的机制(例如:@ConfigurationProperties, @Value(“${…​}”)或者通过Environment 抽象)访问位于Config服务器的任何值。

创建一个 Spring MVC REST controller,返回解析的message的属性值

 

默认情况下,在客户端启动的时候读取了配置值,并且不再读取。你可以强制一个bean去refresh配置-从Config服务器拉取更新的值-通过在

MessageRestController添加Spring Cloud Config的@RefreshScope注解,去触发一个refresh事件。

 

configuration-client/src/main/java/hello/ConfigClientApplication.java


 

package hello;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication

public class ConfigClientApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigClientApplication.class, args);

    }

}

@RefreshScope

@RestController

class MessageRestController {

    @Value("${message:Hello default}")

    private String message;

    @RequestMapping("/message")

    String getMessage() {

        return this.message;

    }

}

测试这个应用

首先启动Config服务器,加载完成后,启动客户端。

在浏览器打开

http://localhost:8080/message

访问客户端应用,你应该可以看到响应反射的Hello world字符串。

 

把位于Git仓库的a-bootiful-client.properties文件的message 改为不同的值。你可以通过访问

http://localhost:8888/a-bootiful-client/default

来确认Config服务器看到了此更改。

你需要调用Spring Boot Actuator端点refresh,强制客户端刷新并且更新新的值。

Spring Boot’s Actuator 暴露了操作端点,像跟应用有关的健康检查和环境信息。

为了使用Actuator,需要在客户端的CLASSPATH 添加

org.springframework.boot:spring-boot-starter-actuator

你可以通过向client的refresh端点发送一个空的HTTP POST请求,调用refresh Actuator端点

http://localhost:8080/actuator/refresh,然后通过检查

http://localhost:8080/message

端点来确认它是否生效。

$ curl localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"

总结

恭喜你,刚刚通过创建一个服务器,然后动态更新配置,为你的所有服务提供了Spring的集中式配置。

原文地址:https://spring.io/guides/gs/centralized-configuration/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息