对Spring Cloud Config有所了解的话,就会知道,Spring Cloud Config注册到eureka后,其他服务会通过eureka找到Spring cloud config,并在Config服务中读取自己的配置文件。
此配置文件可以通过GIT服务器获取、在所在应该的目录中获取、在所在服务器中的文件夹中获取。
1.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.xichuan.dev</groupId>
<artifactId>xichuan-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xichuan-config-server</name>
<description>config server</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</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>Dalston.SR3</spring-cloud.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.yml配置
如果想将配置文件放到项目中文件,可以用一下配置
server:
port: 2003
spring:
application:
name: xichuan-config-server
profiles:
active: native
#本地测试使用
context-path: /config
eureka:
instance:
preferIpAddress: true
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
nonSecurePort: ${server.port}
client:
serviceUrl:
defaultZone: http://localhost:2001/eureka/
management:
security:
enabled: false
如果想让配置文件放到服务器中某一目录下,如/root/xichuan/configs目录下,可以使用以下配置:
server:
port: 2003
spring:
application:
name: xichuan-config-server
profiles:
active: native
##云服务器使用
cloud:
config:
server:
native:
searchLocations:
file:/root/xichuan/configs
inetutils:
ignoredInterfaces:
- eth0
eureka:
instance:
preferIpAddress: true
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
nonSecurePort: ${server.port}
client:
serviceUrl:
defaultZone: http://localhost:2001/eureka/
management:
security:
enabled: false
3.启动类添加注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class XichuanConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(XichuanConfigServerApplication.class, args);
}
}
我们可以看到eureka已经注册了config-server服务
项目的启动顺序是:1.eureka,2.config-server,3.zuul(可选),4.服务
github上有我更多的笔记:Raray-chuan (兮川) · GitHub,欢迎stars与following,如果有问题可以在issue中向我咨询
关注我的公众号,获取更多关于后端、大数据的知识