首页 > 编程语言 > 详细

Spring Cloud(四)搭建一个中心配置服务器Spring Cloud Config(下)

时间:2020-12-09 20:57:29      阅读:29      评论:0      收藏:0      [点我收藏+]

上一篇文章讲述如何搭建一个中心配置服务器,本编继续这个主题来实现一个从中心服务器拉取配置的服务实例。

1、设置好pom.xml

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

2、编写启动类

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);
	}
}

3、编写配置文件bootstrap.yml并放置在src/main/resources

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!