Spring Cloud(四)搭建一个中心配置服务器Spring Cloud Config(上)
大型的系统里面有可能有上百种微服务,假设每种服务平均有10个实例,那么全部实例的数量就是1000个了,这么多实例管理起配置来一定会让人崩溃的。所以Spring Cloud提出了集中式的配置存储方案叫Spring Cloud Config。这个方案里有一个提供REST服务器的配置服务器,系统内所有实例启动时都要访问这个服务器获得更多的配置项;该服务器还有一个后端:Git,配置服务器的数据都来源自于一个Git库。下面就看看如何搭建这个配置服务器吧。
1、创建一个Maven工程并修改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-confsvr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Config Server</name>
<description>Config Server demo project</description>
<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.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<properties>
<start-class>com.github.raglond.sc1.confsvr.ConfigServerApplication</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.confsvr
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String args[]) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3、添加中心配置服务器的配置bootstrap.yml:
spring:
application:
name: configserver
4、添加中心配置服务器的配置application.yml
server:
port: 8888
spring:
cloud:
config:
server:
encrypt.enabled: false
git:
uri: https://github.com/carnellj/config-repo/
searchPaths: licensingservice,organizationservice
username: native-cloud-apps
password: 0ffended
5、编译打包并执行
到此为止,项目需要代码都已备齐,接下来就是执行mvn clean package。然后启动配置服务器:
java -jar target\sc1-confsvr-0.0.1-SNAPSHOT.jar
用Postman发送GET http://127.0.0.1:8888/licensingservice/dev,便可获得licensingservice的开发环境的配置。

浙公网安备 33010602011771号