搭建spring config的坑 记录一下

spring config搭建

  1. 新项目准备使用spring config统一配置,然后遇到搭建中的问题,对英文比较厉害的建议看下spring cloud config文档 可以解决99%的问题https://spring.io/projects/spring-cloud-config#samples
  2. 首先搭建githup建立私有库,公开的也可以,建立仓库地址
  3. 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/> <!-- lookup parent from repository -->
</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>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
</dependencies>
  1. application.yml文件,我用的是ssh配置
server:
  port: 8079

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: git@github.com:xxxx.git
          # 配置文件分支 githup更新之后都是main为主
          default-label: main
          # 配置仓库文件夹 没有可以不需要配置
          search-paths: config
          # 拉取到本地目录
          basedir: target/config
          # 访问ssh私钥口令 没有可以不配置,一般都不会设置吧!
          passphrase: xxxx 
          strict-host-key-checking: false
          # SSH配置
          ignore-local-ssh-settings: true
          # 注意的是private-key后面有个|别忘记 xxxxx代表你自己的ssh私钥 记得使用RSA加密密钥,不要使用默认的
          private-key: |
            -----BEGIN RSA PRIVATE KEY-----
            xxxxx
            -----END RSA PRIVATE KEY-----


  1. 启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}
  1. 启动成功之后访问http://localhost:8079/{app}/{profile},下面的都可以
    或者觉得都复杂,直接http://localhost:8079/config-dev.yml,config-dev.yml就是你的yml文件名称,如果访问显示的就是你yml的配置信息,那么配置就成功了
    在这里插入图片描述
  2. 问题记录,如果spring boot 和cloud版本不对应,那么访问很容易为{},会出现后台很多报错
  3. 密钥加密是有要求的,觉得复杂的话,可以使用公开库 密钥加密有问题会报错git.privateKey’ is not a valid private key
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值