基于spring-cloud搭建自己的配置中心
1.新建配置中心服务端项目 g2-spring-cloud-config-service
加入 pom依赖
<?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> <groupId>g2.com</groupId> <artifactId>g2-spring-cloud-config-service</artifactId> <version>1.0-SNAPSHOT</version> <name>g2-spring-cloud-config-service</name> <!-- FIXME change it to the project's website --> <url>http://g2.com</url> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Dalston.SR5</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-javadoc-plugin</artifactId> <version>2.9.1</version> <configuration> <encoding>UTF-8</encoding> <charset>UTF-8</charset> <docencoding>UTF-8</docencoding> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
2.增加配置
server.port=8000 spring.application.name=g2-spring-cloud-config-service spring.cloud.config.server.git.uri=http://192.168.1.20:8000/config-repo
spring.cloud.config.server.git.uri 地址是基于上一遍 git使用http访问的地址
3.启动类增加 @EnableConfigServer
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * Hello world! * */ @SpringBootApplication @EnableConfigServer public class App { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(App.class); springApplication.run(args); } }
4.在 git的仓库里增加 配置文件
向config-repo 仓库提交 g2-admin-dev.properties 文件
5.启动后访问
http://127.0.0.1:8000/g2-admin/dev
不出意外是可以返回相应的配置信息的
6.普通项目( g2-admin)接入配置中心
增加/修改 bootstrap.properties 文件
spring.application.name=g2-admin spring.profiles.active=dev spring.cloud.config.uri=http://127.0.0.1:8000/ spring.cloud.config.enabled=true
pom文件增加 spring-cloud-config依赖
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
正常启动会打印
2020-05-22 15:24:49.670 INFO 9112 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://127.0.0.1:8000/ 2020-05-22 15:24:50.085 INFO 9112 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=g2-admin, profiles=[dev], label=null, version=afeb7ac1bf30512bf5fddcfd2e904360c10d05e6, state=null 2020-05-22 15:24:50.085 INFO 9112 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='http://192.168.1.20:8000/config-repo/g2-admin-dev.properties'}]] 2020-05-22 15:24:50.093 INFO 9112 --- [ main] com.g2.admin.App : The following profiles are active: dev