Spring cloud config 使用gitHub或者gitee连接

1. 创建SpringCloud项目,引入对应的Spring-config-server对应的jar

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>3.7.1.201504261725-r</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

 2. 创建一个Spring boot启动类:

添加如下两个注解

@EnableConfigServer
@SpringBootApplication
package cn.lonecloud.config.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * @author lonecloud
 * @version v1.0
 * @Package cn.lonecloud.config
 * @Description: TODO
 * @date 2018/6/12下午7:58
 */
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}

  

3. 添加application.yml

由于连接git分两种:一种为共有没有访问权限密码的,一种使用账号密码登录,一种采用ssh登录,

(一).完全公开,无密码访问配置:

server:
  port: 3344 #设置端口
spring:
  application:
    name: config-server #设置名称
  cloud:
    config:
      server:
        git:
          uri: git@gitee.com:lonecloud/xxx.git #设置git仓库地址
          force-pull: true #设置强行pull拉取

(二).采用账号密码访问登录

server:
  port: 3344 #设置端口
spring:
  application:
    name: config-server #设置名称
  cloud:
    config:
      server:
        git:
          uri: git@gitee.com:lonecloud/xxx.git #设置git仓库地址
          force-pull: true #设置强行pull拉取
          username: lonecloud
          password:  password #填写你自己密码

(三).采用SSH无密码登录,最坑的则是第三个,如果您的主机配置了ssh则直接采用(一)方案即可,如果没有配置则需要生成对应的ssh key,将其复制到此处,既可访问

server:
  port: 3344
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: git@gitee.com:lonecloud/xxx.git
          ignoreLocalSshSettings: true
          force-pull: true
          privateKey: |   #这个地方复制你的RSA密码,记得这里有个| 别忘了
                      -----BEGIN RSA PRIVATE KEY-----
                      
                      -----END RSA PRIVATE KEY-----

  

4. 直接访问该地址,由于我的配置地址为3344端口,所以我的地址为http://localhost:3344/application-dev.yml

后面的参数为{你的git上的文件名}-{profile}.yml

profile,这就是你在你的配置文件中设置的配置文件分类,用于分别你的事dev环境还是test环境

有问题欢迎加入群:416052025。交流

原文地址:https://www.cnblogs.com/lonecloud/p/9189407.html