Spring Boot 简明教程

Spring Boot - Cloud Configuration Client

一些应用程序可能需要需要更改的配置属性,开发人员可能需要将它们关闭或重启应用程序来执行此操作。然而,这可能会导致生产中的停机时间和重启应用程序的需要。Spring Cloud Configuration Server 允许开发人员在不重启应用程序和不产生任何停机时间的情况下加载新的配置属性。

Some applications may need configuration properties that may need a change and developers may need to take them down or restart the application to perform this. However, this might be lead to downtime in production and the need of restarting the application. Spring Cloud Configuration Server lets developers to load the new configuration properties without restarting the application and without any downtime.

Working with Spring Cloud Configuration Server

首先,从 https://start.spring.io/ 下载 Spring Boot 项目,并选择 Spring Cloud Config Client 依赖项。现在,在你的构建配置文件中添加 Spring Cloud Starter Config 依赖项。

First, download the Spring Boot project from https://start.spring.io/ and choose the Spring Cloud Config Client dependency. Now, add the Spring Cloud Starter Config dependency in your build configuration file.

Maven 用户可以将下面的依赖项添加到 pom.xml 文件中。

Maven users can add the following dependency into the pom.xml file.

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

Gradle 用户可以将下面的依赖项添加到 build.gradle 文件中。

Gradle users can add the following dependency into the build.gradle file.

compile('org.springframework.cloud:spring-cloud-starter-config')

现在,您需要将 @RefreshScope 注解添加到您的 Spring Boot 主应用程序中。@RefreshScope 注解用于从配置服务器中加载配置属性值。

Now, you need to add the @RefreshScope annotation to your main Spring Boot application. The @RefreshScope annotation is used to load the configuration properties value from the Config server.

package com.example.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;

@SpringBootApplication
@RefreshScope
public class ConfigclientApplication {
   public static void main(String[] args) {
      SpringApplication.run(ConfigclientApplication.class, args);
   }
}

现在,在 application.properties 文件中添加配置服务器 URL,并提供您的应用程序名称。

Now, add the config server URL in your application.properties file and provide your application name.

Note − [role="bare"] [role="bare"]http://localhost:8888 配置服务器应在启动配置客户端应用程序之前运行。

Note − [role="bare"]http://localhost:8888 config server should be run before starting the config client application.

spring.application.name = config-client
spring.cloud.config.uri = http://localhost:8888

下面给出了用于编写一个简单的 REST 端点以从配置服务器读取欢迎消息的代码 −

The code for writing a simple REST Endpoint to read the welcome message from the configuration server is given below −

package com.example.configclient;

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
@RefreshScope
@RestController
public class ConfigclientApplication {
   @Value("${welcome.message}")
   String welcomeText;

   public static void main(String[] args) {
      SpringApplication.run(ConfigclientApplication.class, args);
   }
   @RequestMapping(value = "/")
   public String welcomeText() {
      return welcomeText;
   }
}

您可以创建一个可执行 JAR 文件并使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序 -

You can create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands −

对于 Maven,您可以使用下面所示的命令 −

For Maven, you can use the command shown below −

mvn clean install

“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

对于 Gradle,您可以使用下面显示的命令 −

For Gradle, you can use the command shown below −

gradle clean build

“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录中找到 JAR 文件。

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.

现在,使用此处所示的命令运行 JAR 文件:

Now, run the JAR file by using the command shown here:

java –jar <JARFILE>

现在,应用程序已在 Tomcat 端口 8080 上启动,如下所示 −

Now, the application has started on the Tomcat port 8080 as shown here −

started application on tomcat port 8080

您可以在控制台窗口中看到日志;配置客户端应用程序正在从 https://localhost:8888 中获取配置

You can see the log in console window; config-client application is fetching the configuration from the https://localhost:8888

2017-12-08 12:41:57.682  INFO 1104 --- [
   main] c.c.c.ConfigServicePropertySourceLocator :
   Fetching config from server at: http://localhost:8888

现在点击 URL, http://localhost:8080/ 欢迎消息从配置服务器加载。

Now hit the URL, http://localhost:8080/ welcome message is loaded from the Configuration server.

spring cloud config server

现在,进入配置服务器更改属性值,然后点击执行器端点 POST URL http://localhost:8080/refresh ,并在 URL http://localhost:8080/ 中查看新配置属性值

Now, go and change the property value on the Configuration server and hit the actuator Endpoint POST URL http://localhost:8080/refresh and see the new configuration property value in the URL http://localhost:8080/