spring cloud config server使用ssh方式连接config-repo

本文详细介绍了Spring Cloud Config Server通过三种方式连接Git仓库的方法,包括用户名密码方式、SSH公钥方式及Java属性配置SSH连接的方式。并针对每种方式提供了具体配置步骤,同时分享了解决连接异常的经验。

spring cloud config server 有三种连接git方式,从而读取config repo中的配置信息。

方式一:用户名密码方式

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git.oschina.net/config-repo
          username: username
          password: password

uri使用https地址。

方式二:SSH属性配置

生成SSH公钥,将公钥信息添加到git上。

生成公钥方式 

注意配置文件中的uri使用ssh地址:git@gitee.com:config-repo.git

因为之前生成过公钥,但是无法链接。索性清除key-pair重新生成密钥对。

首先,清除所有的key-pair
ssh-add -D
rm -r ~/.ssh
删除你在github中的public-key

重新生成ssh密钥对
ssh-keygen -t rsa -C "xxx@xxx.com"
chmod 0700 ~/.ssh
chmod 0600 ~/.ssh/id_rsa*

接下来正常操作
在github上添加公钥public-key:
1、首先在你的终端运行 xclip -sel c ~/.ssh/id_rsa.pub将公钥内容复制到剪切板
2、在github上添加公钥时,直接复制即可
3、保存

链接: https://www.zhihu.com/question/21402411/answer/42836492
如果出现Reject HostKey的错误,可参考 https://blog.youkuaiyun.com/czq7511/article/details/76038386

方式三:用java属性设置SSH连接

spring:
  cloud:
    config:
      server:
        git:
          uri: git@gitee.com:config-repo.git
          ignoreLocalSshSettings: true  #为了激活基于属性的SSH配置
#          hostKey: someKey
#          hostKeyAlgorithm: ssh-rsa
#          strictHostKeyChecking: true
          privateKey: |
                       -----BEGIN RSA PRIVATE KEY-----
                       MIIEpQIBAAKCAQEAzlveAu1ttIQpxbLhGxKXu4OCfMFmHecFqtk21qz0wAxDGNhh
                       HntkXVBoBLhWGrhMSuGlGz3PU0MkvTheRYLzf3r75lK5R0EqULJ7fcYxRVwiSNTb
                       **************************************
                       nEgjIlotLeGb+FxegNgZZd/4Dywk36tuvzLWlY6YBJlOxYYg54aYE61X85PzHJM+
                       P4hXH3ECgYEA7aGXCEBZmtRF4Z9hkEhwI0StXAvCqXibFw1kMD1viP4z43VNrSQb
                       4aW4u3pkKeMA9ezcp8J40zsCql3vz65Gokj1haS04MVkDVNK9P/g3jlfx9zSxSlB
                       +JYIL0yduAUBZx+FJ4jZe4S4DkS5X43XKfilfbrXX4kjVaeSOwbe76I=
                       -----END RSA PRIVATE KEY-----

用方式三的时候遇到的问题:

1、根据官方文档配置好信息之后,会出现异常com.jcraft.jsch.JSchException: reject HostKey: gitee.com 和 com.jcraft.jsch.JSchException: Auth fail .

尝试过各种解决方式,最后无聊看spring cloud版本都已经更新到F了,我们还是用的之前的d的release,已经跳过坑的我,似乎觉察到了什么。稍微尝试了下更新到Dalston.SR5,果然,又在新旧版本的坑里折腾了很久。

2、privateKey添加方式,注意别少了 |

3、升级了下版本之后,不再报500异常了,但是又出现了404.控制台warn如下:

2018-03-29 09:54:03.819  WARN 52093 --- [nio-8021-exec-6] .c.s.e.MultipleJGitEnvironmentRepository : Could not fetch remote for master remote: git@gitee.com:config-repo.git

又读官网配置,检查配置,无果。无意中把hostKey、hostKeyAlgorithm、strictHostKeyChecking这三个属性注释掉之后,刷新页面,配置信息居然出来了。

众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

泪!!!

参考:

http://projects.spring.io/spring-cloud/

http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign


2018.3.30--------

版本升级之后,发现之前加密过的字符串不能解析了。但是昨天线上更新一版,并没有出现问题。

本地配置问题?可是换回低版本之后又可以,排除本地环境配置问题。

最后没办法之际,觉得刚才粗粗略过的一个回答,尝试下?嗯,解析出来了。

总结:spring cloud Dalston.SR5 版本之后,application.yml文件里不能有encrypt.key,启动的时候再配置,就不会出问题了。

参考:https://github.com/spring-cloud/spring-cloud-config/issues/800







<think>好的,我现在需要回答用户关于如何添加Spring Cloud Config Server依赖的问题。首先,我得回忆一下Spring Cloud Config的基本知识。用户提到了引用中的内容,特别是引用[2]和[5],可能需要参考这些资料。 首先,用户想知道如何添加相关依赖。我记得在Maven项目中,通常是在pom.xml中添加依赖项。Spring Cloud Config Server属于Spring Cloud项目的一部分,所以应该使用相应的starter依赖。比如,spring-cloud-config-server应该就是正确的依赖项。然后需要确定Spring Boot和Spring Cloud的版本兼容性,这可能需要查看官方文档或者引用中的例子。例如,引用[5]中的配置文件可能对应某个特定版本,需要确认依赖版本是否匹配。 接下来,配置教程部分。用户需要创建一个配置服务器,可能需要在启动类上添加@EnableConfigServer注解,如引用[4]所示。同时,配置文件application.yml需要设置Git仓库的URI、用户名和密码等信息,如引用[5]中的例子。这些步骤需要详细说明,包括可能的路径搜索配置和Eureka集成。 还要考虑用户可能遇到的常见问题,比如私有仓库的凭证配置,这在引用[3]中提到过。需要提醒用户如果使用私有Git仓库,必须正确填写用户名和密码,或者使用SSH密钥等方式。 另外,引用[2]提到创建Maven项目,所以应该指导用户如何正确设置pom.xml文件,添加必要的父项目、依赖管理等。可能需要说明使用Spring Cloud的依赖管理BOM,确保各个库版本兼容。 最后,需要验证配置是否正确,比如启动服务器后访问端点检查配置是否加载成功。可能还要提到如何注册到Eureka,如引用[5]中的eureka.client.service-url配置。 总结步骤应该是:添加依赖、配置启动类、设置配置文件、处理私有仓库认证、测试配置服务器。同时,生成相关问题,可能涉及性能优化、安全加密(JCE)等,如引用[3]提到的JCE保障数据安全。</think>### 添加 Spring Cloud Config Server 依赖及配置教程 #### 1. 添加 Maven 依赖 在项目的 `pom.xml` 中添加以下依赖: ```xml <!-- Spring Cloud Config Server 核心依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- Spring Boot Starter Web(若需Web支持) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Cloud Starter Netflix Eureka Client(若需注册到Eureka) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ``` 同时,确保在 `<dependencyManagement>` 中声明 Spring Cloud 版本: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.8</version> <!-- 根据Spring Boot版本选择 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` #### 2. 配置启动类 在启动类上添加 `@EnableConfigServer` 注解以启用配置服务器功能: ```java @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` [^4] #### 3. 配置文件设置 在 `application.yml` 中配置 Git 仓库信息: ```yaml server: port: 8080 spring: application: name: config-server cloud: config: server: git: uri: https://github.com/your-username/config-repo # Git仓库地址 search-paths: configs # 配置文件搜索路径 username: git-username # 私有仓库需提供凭证 password: git-password eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ # 注册到Eureka ``` [^5] #### 4. 私有仓库认证说明 -使用 **SSH 协议**,需配置 `ignore-local-ssh-settings: false` 并添加私钥路径[^3]。 - 对于 HTTPS 私有仓库,直接填写 `username` 和 `password` 字段即可[^5]。 #### 5. 验证配置服务器 启动服务后,访问以下端点验证配置: - `http://localhost:8080/{application}/{profile}/{label}` 例如:`http://localhost:8080/myapp/dev/master` 若返回 JSON 格式配置,则说明配置成功。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值