创建Config配置中心
本文需要对IDEA有一定的了解,清楚IDEA中项目与文件的创建,后续迭代补充基础知识
前期准备
- 通过IDEA创建module dsz-config
- 依赖项目dsz-root,dsz-eureka
Spring Cloud版本选型
- Greenwich SR2
- Spring Boot 2.1.6.RELEASE
- Spring 5.1.8.RELEASE
- jdk 1.8.0_172
最终展示pom.xml和项目结构
项目结构
项目结构
pom.xml
<?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">
<parent>
<artifactId>dsz-root</artifactId>
<groupId>com.dsz.platform</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dsz-config</artifactId>
<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-client</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8888
spring:
application:
name: dsz-config
cloud:
config:
server:
git:
## 填写自己git仓库地址
uri: https://github.com/***/dsz-root.git
search-paths:
- dsz-config
eureka:
instance:
hostname: 127.0.0.1
## 心跳间隔-5秒
lease-renewal-interval-in-seconds: 5
## 没有心跳的淘汰时间-10秒
lease-expiration-duration-in-seconds: 10
client:
## 刷新本地缓存-5秒
registry-fetch-interval-seconds: 5
service-url:
defaultZone: http://admin:admin@${eureka.instance.hostname}:8761/eureka/
com.dsz.base.config.ConfigApplication
package com.dsz.base.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}