spring config搭建
- 新项目准备使用spring config统一配置,然后遇到搭建中的问题,对英文比较厉害的建议看下spring cloud config文档 可以解决99%的问题https://spring.io/projects/spring-cloud-config#samples
- 首先搭建githup建立私有库,公开的也可以,建立仓库地址
- maven依赖 使用spring boot 2.4.3 cloud为 2020.0.1(注意 spring boot 和 spring cloud版本一定要对应,不然会发生很多奇怪的问题!)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
- application.yml文件,我用的是ssh配置
server:
port: 8079
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: git@github.com:xxxx.git
default-label: main
search-paths: config
basedir: target/config
passphrase: xxxx
strict-host-key-checking: false
ignore-local-ssh-settings: true
private-key: |
-----BEGIN RSA PRIVATE KEY-----
xxxxx
-----END RSA PRIVATE KEY-----
- 启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 启动成功之后访问http://localhost:8079/{app}/{profile},下面的都可以
或者觉得都复杂,直接http://localhost:8079/config-dev.yml,config-dev.yml就是你的yml文件名称,如果访问显示的就是你yml的配置信息,那么配置就成功了
- 问题记录,如果spring boot 和cloud版本不对应,那么访问很容易为{},会出现后台很多报错
- 密钥加密是有要求的,觉得复杂的话,可以使用公开库 密钥加密有问题会报错git.privateKey’ is not a valid private key