- 创建项目
- 配置文件
- 启动类
- pom文件
- 验证
一.创建项目
选取Spring Cloud Config作为分布式配置中心组件
创建maven项目flow-config-server(8741)作为子模块
二.配置文件
application-config-server-dev.yml, 内容如下:
server:
port: 8741
eureka:
instance:
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
# 单个配置
# defaultZone: http://localhost:8761/eureka/
# 集群配置
defaultZone: http://127.0.0.1:8761/eureka/, http://127.0.0.1:8762/eureka/
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/zrobin/git-config-server/
search-paths: /
default-label: master
username:
password:
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
info:
company.name: org.flowframework
app.name: config-server
app.desc: springcloud-config-server
author: robin
注意:
配置文件访问规则如下
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
application–应用名称
profile–配置文件的版本
label–分支
githup配置文件直接访问https://github.com/zrobin/git-config-server/进行查看
三.启动类
Application.java
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
四.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>org.flowframework</groupId>
<artifactId>flow-config-server</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.flowframework</groupId>
<artifactId>springcloud</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<finalName>flow-config-server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
五.验证
http://localhost:8741/config-server/dev
{“name”:“config-server”,“profiles”:[“dev”],“label”:null,“version”:“5440fc7f3d4217d9793d06ce6e2294bbb87e7de2”,“state”:null,“propertySources”:[]}
http://localhost:8761/