上一篇文章讲述如何搭建一个中心配置服务器,本编继续这个主题来实现一个从中心服务器拉取配置的服务实例。
<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.github.ralgond</groupId>
<artifactId>sc1-confclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>
<properties>
<start-class>com.github.raglond.sc1.confclient.ConfigClientApplication</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.github.raglond.sc1.confclient;
@SpringBootApplication
@RestController
public class ConfigClientApplication {
@Value("${example.property}")
String exampleProperty;
@RequestMapping(method=RequestMethod.GET, path="/hello")
public String hello() {
return "Hello World, "+exampleProperty;
}
public static void main(String args[]) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
spring:
application:
name: licensingservice
profiles:
active:
default
cloud:
config:
uri: http://localhost:8888
到此为止,这个服务就写好了,编译打包后直接启动,可以看到在启动阶段它会从中心配置服务器加载配置:
2020-12-09 20:03:04.388 INFO 13416 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-12-09 20:03:07.069 INFO 13416 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=licensingservice, profiles=[default], label=null, version=56d63a8c0c3dcb0c5c93db1f00cf71856371db8b, state=null
2020-12-09 20:03:07.072 INFO 13416 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name=‘bootstrapProperties-configClient‘}, BootstrapPropertySource {name=‘bootstrapProperties-https://github.com/carnellj/config-repo//licensingservice/licensingservice.yml‘}]
2020-12-09 20:03:07.083 INFO 13416 --- [ main] c.g.r.s.c.ConfigClientApplication : The following profiles are active: default
打开浏览器输入http://127.0.0.1:8080/hello,可以看到一段文字“Hello World, I AM IN THE DEFAULT”,其中的“I AM IN THE DEFAULT”就是来自中心配置服务器的默认配置。
Spring Cloud(四)搭建一个中心配置服务器Spring Cloud Config(下)
原文:https://www.cnblogs.com/ralgo/p/14110697.html