分布式、云服务 导致整个项目不同环境下的配置文件修改称为问题,为此springCloud做了配置中心这一块方便整体配置,springCloud默认远程是放到git上,然后每个服务都访问这个Git来获取配置,下面是基于Git的远程配置
创建maven项目,引入如下依赖
<?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>com.spring.cloud.conf</groupId>
<artifactId>springCloudConf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springCloudConf</name>
<description>springCloudConf</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
启动类添加两个注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.config.server.EnableConfigServer;
//开启配置中心
@EnableConfigServer
//开启注册客户端
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConfApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfApplication.class, args);
}
接下来是配置文件
#注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
#服务对外暴露接口
server.port=8889
#对外暴露服务名
spring.application.name=config-server
#远程配置中心配置(Git路径)
spring.cloud.config.server.git.uri=https://gitee.com/729564606.net/test
#searchPaths配置文件所在的文件夹在仓库中的路径, 在server端不需要指定具体配置文件名
#spring.cloud.config.server.git.searchPaths=spring-cloud/helloworldConfig
然后是Git上的配置文件
命名规范:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.propertie
配置文件的Git地址是 https://gitee.com/729564606.net/test
我的配置文件名是application-test.properties
内容是 test.name=newTest123
当这些配置好后启动配置中心服务(注册中心不能关,会报找不到注册中心)
这是你在注册中心的页面上就能看到配置中心的服务已经启动
然后访问链接:http://127.0.0.1:8889/application-test.properties
(这个url对应/{application}-{profile}.properties)
会显示值:test.name: newTest123
或者访问:http://127.0.0.1:8889/application/test
(这个url的对应/{application}/{profile})
显示: