很多朋友只知道spring cloud config可以刷新远程git的配置到内存中,
却不知道spring cloud config的客户端可以脱离服务端使用,
更不知道spring cloud config客户端结合actuator还可以刷新本地的配置文件到内存中
主类如下:
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 @RestController @RefreshScope public class FirstDemoApplication { public static void main(String[] args) { SpringApplication.run(FirstDemoApplication.class, args); } private static int port; @Value("${server.port}") public void setPort(int port){ this.port=port; } @RequestMapping("/port") public int port(){ return port; } }
application.properties如下:
server.port=801 management.security.enabled=false
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"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.duke</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>firstDemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--监控+refresh配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!--拷贝依赖jar包--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/tmp/${project.artifactId}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!--拷贝资源文件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <executions> <!--拷贝resources--> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> </includes> </resource> </resources> <outputDirectory>${project.build.directory}/tmp/${project.artifactId}/config </outputDirectory> </configuration> </execution> </executions> </plugin> <!--打包jar--> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <!--MANIFEST.MF中Class-Path 加入前缀--> <classpathPrefix>lib/</classpathPrefix> <!--jar包不包含唯一版本标识--> <useUniqueVersions>false</useUniqueVersions> <!--指定入口类--> <mainClass>com.zte.demo.FirstDemoApplication</mainClass> </manifest> </archive> <!--不打包资源文件--> <excludes> <exclude>*.properties</exclude> <exclude>logback.xml</exclude> </excludes> <outputDirectory>${project.build.directory}/tmp/${project.artifactId}</outputDirectory> </configuration> </plugin> </plugins> </build> </project>
测试
1、启动项目,访问 http://localhost/port 显示 801
2、修改classpath(注意是classpath,即你编译后的class文件所处的目录)下的application.properties将server.port改为80(这里config配置文件在外边,可以直接修改)
3、发送空post(注意是post)请求到 http://localhost:80/refresh
4、再次访问 http://localhost/port 显示 801 测试成功
原文:https://www.cnblogs.com/share-duke/p/12191068.html