×
Spring Boot教程Spring Boot简介Spring Boot快速入门Spring Boot引导过程Spring Boot Tomcat部署Spring Boot构建系统Spring Boot代码结构Spring Boot Bean和依赖注入Spring Boot运行器(Runner)Spring Boot应用程序属性Spring Boot日志Spring Boot构建RESTful Web服务Spring Boot异常处理Spring Boot拦截器Spring Boot Servlet过滤器Spring Boot Tomcat端口号Spring Boot Rest模板Spring Boot文件处理Spring Boot服务组件Spring Boot Thymeleaf示例Spring Boot使用RESTful Web服务Spring Boot CORS支持Spring Boot国际化Spring Boot调度Spring Boot启用HTTPSSpring Boot Eureka服务器Spring Boost Eureka服务注册Spring Boot Zuul代理服务器和路由Spring Boot云配置服务器Spring Boot云配置客户端Spring Boot ActuatorSpring Boot管理服务器Spring Boot管理客户端Spring Boot启用Swagger2Spring Boot创建Docker镜像Spring Boot跟踪微服务日志Spring Boot Flyway数据库Spring Boot发送电子邮件Spring Boot HystrixSpring Boot Web SocketSpring Boot批量服务Spring Boot Apache KafkaSpring Boot单元测试用例Spring Boot Rest控制器单元测试Spring Boot数据库源(连接数据库)Spring Boot保护Web应用程序

Spring Boot云配置客户端


某些应用程序可能需要更改配置属性,开发人员可能需要将其关闭或重新启动应用程序才能执行此操作。 但是,这可能会导致生产停机并需要重新启动应用程序。 Spring Cloud Configuration Server允许开发人员加载新的配置属性,而无需重新启动应用程序,不需要任何停机。

使用Spring Cloud配置服务

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

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

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

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

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

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

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并提供应用程序名称。

- 在启动config客户端应用程序之前,应运行http://localhost:8888配置服务器。

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

编写简单REST端点以从配置服务器读取欢迎消息的代码如下 -

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应用程序 -

对于Maven,可以使用下面命令 -

mvn clean install

在“BUILD SUCCESS”之后,可以在target目录下找到JAR文件。

对于Gradle,可以使用下面命令 -

gradle clean build

在构建显示“BUILD SUCCESSFUL”之后,可在build/libs目录下找到JAR文件。

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

java –jar <JARFILE>

现在,应用程序已在Tomcat端口8080上启动。

在登录控制台窗口中看到; config-client应用程序从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/ ,程序将从配置服务器加载欢迎消息。

现在,转到配置服务器上的属性值并点击执行器端点 POST URL => http://localhost:8080/refresh并在URL=> http://localhost:8080/中查看新配置属性值。


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)