欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringCloud Config Server加载远端Git服务器中的配置文件内容

程序员文章站 2022-07-04 16:01:02
...

介绍:

本实例是通过SpringCloud Config Server加载远端服务器中的文件内容,这样的好处是多个微服务可以动态的加载远端的配置文件,达到了共用的效果便于维护。

实例:

首先建立config-learring文件夹,在该文件夹下创建2个springboot项目分别为microservice-config-server、microservice-config-client,并在config-learring文件夹下创建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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<groupId>com.itmuch.cloud</groupId>
	<artifactId>microservice-spring-cloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	
	<properties>
		<java.version>1.8</java.version>
	</properties>
	
  <modules>
  	<module>microservice-config-server</module>
	<module>microservice-config-client</module>
  </modules>
  
  <dependencyManagement>
  	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.4.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<!--Greenwich.RELEASE-->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-dependencies</artifactId>
		    <version>Greenwich.RELEASE</version>
		    <type>pom</type>
		    <scope>import</scope>
		</dependency>
	</dependencies>
  </dependencyManagement>
  
  <build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
  </build>
</project>

microservice-config-server项目中的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">
	<modelVersion>4.0.0</modelVersion>
	
	<parent>
		<groupId>com.itmuch.cloud</groupId>
		<artifactId>microservice-spring-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	
	<artifactId>microservice-config-server</artifactId>
    <packaging>jar</packaging>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

</project>

microservice-config-server项目中的application.yml:

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/guigui2019/guiguick #git资源路径

microservice-config-server项目中的启动类文件:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}

}

microservice-config-client项目中的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">
	<modelVersion>4.0.0</modelVersion>
	
	<parent>
		<groupId>com.itmuch.cloud</groupId>
		<artifactId>microservice-spring-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	
	<artifactId>microservice-config-client</artifactId>
    <packaging>jar</packaging>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

</project>

microservice-config-client项目中的application.yml:

server:
  port: 8081

microservice-config-client项目中的bootstrap.yml:

spring:
  cloud:
    config:
      uri: http://localhost:8080
      profile: dev
      label: master # 当configserver的后缀存储是Git默认就是master
  application:
    name: test

microservice-config-client项目中Controller:

@RestController
public class ConfigClientController {
	
	@Value("${profile}")
	private String profile;

	@RequestMapping("/profile")
	public String getProfile() {
		return profile;
	}
	
}

microservice-config-client项目启动类: 

@SpringBootApplication
public class ConfigServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}

}

 登录码云服务后创建仓库上传2个配置文件test-dev.yml和application.yml。

  1. test.yml中内容:profile: test-dev。
  2. application.yml中内容:profile: profile-default 。

SpringCloud Config Server加载远端Git服务器中的配置文件内容

最后分别启动microservice-config-server和microservice-config-client两个项目,在浏览器中输入请求地址输出结果。

SpringCloud Config Server加载远端Git服务器中的配置文件内容

如果在client项目中的bootstrap.xml中的spring.application.name:是个不存在名称,找不到该文件会默认加载application.yml中的内容,所以修改名称后启动再次输入请求,输出结果为application.yml中的内容。

SpringCloud Config Server加载远端Git服务器中的配置文件内容