解决SpringCloud Config结合github无法读取配置的问题

yizhihongxing

解决Spring Cloud Config结合GitHub无法读取配置的问题,可以按照以下完整攻略进行:

1. 配置GitHub Personal Access Token

首先需要在GitHub上配置Personal Access Token,用来在Spring Cloud Config中访问GitHub的私有仓库。具体步骤如下:

  1. 打开GitHub,进入Settings -> Developer settings -> Personal access tokens。

  2. 在Personal access tokens页面中,点击Generate new token按钮,填写Token description,勾选repo权限,点击Generate token按钮,生成新的token。

  3. 将生成的Token复制下来,备用。

2. 在配置文件中添加GitHub的Personal Access Token

在Spring Cloud Config的配置文件(application.yml或application.properties)中,需要添加GitHub的Personal Access Token,具体示例如下:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/user/repo #GitHub仓库地址
          search-paths: '{application}' #搜索应用配置文件的目录
          default-label: main #默认分支
          username: user #GitHub用户名
          password: token #GitHub Personal Access Token

3. 创建GitHub账户密钥

为了实现Spring Cloud Config与GitHub的SSH通信,需要在本机生成一个密钥,并将公钥添加到GitHub帐户中。具体步骤如下:

  1. 打开Git Bash,输入以下命令生成密钥对:
ssh-keygen -t rsa -C "youremail@example.com"

其中,youremail@example.com修改为自己的邮箱。

  1. 生成的公钥和私钥分别存储在C:\Users\username.ssh目录下的id_rsa.pub和id_rsa文件中。

  2. 打开id_rsa.pub文件,将文件内容复制到GitHub中,具体步骤如下:

  3. 登录GitHub,进入Settings -> SSH and GPG keys。

  4. 点击New SSH key按钮。

  5. 将公钥内容粘贴到Key域中,填写Title,并点击Add SSH key按钮。

4. 本地运行Spring Cloud Config

配置完成后,可以运行Spring Cloud Config,并访问GitHub中的配置文件。具体示例如下:

  1. 创建一个Spring Boot项目,引入Spring Cloud Config和相关依赖。
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  1. 在启动类上添加@EnableConfigServer注解。
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
  1. 创建配置文件src/main/resources/application.yml,并添加配置。
server:
  port: 8888 #Spring Cloud Config Server端口

spring:
  application:
    name: config-server #应用名称

  cloud:
    config:
      server:
        git:
          uri: https://github.com/user/repo #GitHub仓库地址
          search-paths: '{application}' #搜索应用配置文件的目录
          default-label: main #默认分支
          username: user #GitHub用户名
          password: token #GitHub Personal Access Token
  1. 启动项目,访问http://localhost:8888/config-client-dev.yml,可以看到从GitHub中读取到的配置文件。

示例说明

示例1:使用了错误的Personal Access Token

假如在config-client-dev.yml配置文件中,使用了错误的Personal Access Token,启动Spring Cloud Config Server将会报错。具体步骤如下:

  1. 将config-client-dev.yml配置文件中的password值修改为错误的Personal Access Token。

  2. 启动Config Server,控制台输出如下错误信息:

Caused by: org.eclipse.jgit.api.errors.TransportException: Cannot connect to remote repository
        at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:244) ~[org.eclipse.jgit-5.11.0.202105071810-r.jar:5.11.0.202105071810-r]
        ... 23 common frames omitted
Caused by: org.eclipse.jgit.errors.TransportException: https://github.com/user/repo: not authorized
        at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:546) ~[org.eclipse.jgit.http.apache-5.11.0.202105071810-r.jar:5.11.0.202105071810-r]
        ... 28 common frames omitted
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.github.com/repos/user/repo/git/refs/heads
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1920) ~[na:1.8.0_191]
        ... 34 common frames omitted
  1. 根据错误信息可以看出,连接GitHub仓库时发生403错误,提示“not authorized”,即没有授权访问此仓库。可以根据提示重新检查Personal Access Token是否正确。

示例2:未添加GitHub账户密钥

假如在启动Spring Cloud Config Server前,未添加GitHub账户密钥,启动后将会报错。具体步骤如下:

  1. 删除本机的密钥文件,默认路径为C:\Users\username.ssh。

  2. 启动Config Server,控制台输出如下错误信息:

Caused by: org.eclipse.jgit.api.errors.TransportException: git@github.com:user/repo: git-upload-pack not permitted on 'repo'
        at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:262) ~[org.eclipse.jgit-5.11.0.202105071810-r.jar:5.11.0.202105071810-r]
        ... 23 common frames omitted
Caused by: org.eclipse.jgit.errors.TransportException: git@github.com:user/repo: git-upload-pack not permitted on 'repo'
        at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:262) ~[org.eclipse.jgit.ssh.apache-5.11.0.202105071810-r.jar:5.11.0.202105071810-r]
        ... 29 common frames omitted
  1. 根据错误信息可以看出,连接GitHub仓库时发生错误,提示“git-upload-pack not permitted on 'repo'”,即无法访问此仓库。此时需要先添加GitHub账户密钥,然后重新启动Spring Cloud Config Server。

以上就是解决Spring Cloud Config结合GitHub无法读取配置的问题的完整攻略。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:解决SpringCloud Config结合github无法读取配置的问题 - Python技术站

(0)
上一篇 2023年5月16日
下一篇 2023年5月16日

相关文章

  • 使用git命令将本地代码上传到GitHub

    使用git命令将本地代码上传到GitHub分为以下几个步骤: 步骤一:创建GitHub仓库并获取仓库地址 在GitHub网站上创建一个仓库,获得一个仓库地址。在这个仓库地址中“用户名”是你的GitHub账户的用户名,”仓库名”是你要创建的仓库的名字,仓库地址类似于 https://github.com/用户名/仓库名.git。 步骤二:本地创建Git仓库 在…

    GitHub 2023年5月16日
    00
  • Python实现企业微信机器人每天定时发消息实例

    接下来我将为您详细讲解“Python实现企业微信机器人每天定时发消息实例”的完整攻略,进一步协助您了解此项技术。 一、 简介 在企业场景中,使用企业微信机器人向员工及时发消息,可以提高内部协作、沟通效率。本攻略主要介绍如何使用 Python 语言,实现企业微信机器人每天定时自动发消息的过程。 二、 创建企业微信机器人API 打开企业微信管理后台,选择应用管理…

    GitHub 2023年5月16日
    00
  • react+axios实现github搜索用户功能(示例代码)

    本文将详细讲解如何使用React和Axios来实现Github搜索用户的功能。其中包含两个示例说明,以带领读者逐步了解如何实现这一功能。 示例一:使用Github API搜索用户 在这个示例中,我们将使用Github API来搜索Github上的用户。首先,我们需要在Github上注册一个新的OAuth App,并获得一个访问令牌(access token)…

    GitHub 2023年5月16日
    00
  • vue devtools的安装与使用教程

    Vue DevTools是一个强大的浏览器扩展程序,可以帮助我们开发和调试Vue.js程序。下面是Vue DevTools的安装和使用教程: 安装Vue DevTools 首先,我们需要使用Vue CLI创建一个新的Vue项目。 在安装Vue CLI时,可以选择添加Vue DevTools插件。如果没有安装,可以使用以下命令在项目中安装: npm insta…

    GitHub 2023年5月16日
    00
  • Android动态绘制饼状图的示例代码

    下面是关于“Android动态绘制饼状图的示例代码”的完整攻略,包含两条示例说明。 示例一:使用Android Graphics绘制饼状图 1. 绘制饼状图基本思路 我们可以通过Android Graphics来绘制饼状图。具体的步骤包括: 根据数据计算每个扇形所占的角度; 根据半径和圆心位置,绘制圆弧; 绘制圆弧上的数据说明。 2. 示例代码 通过如下代码…

    GitHub 2023年5月16日
    00
  • git详细安装教程及下载太慢的解决办法

    Git详细安装教程及下载太慢解决办法 安装Git 下载Git安装程序 从Git官网(https://git-scm.com/downloads)下载适合您操作系统的安装包。 安装Git 运行安装程序,根据提示完成安装。在安装过程中,可以按照默认设置,也可以根据个人需要进行配置。 配置Git 打开命令行窗口,输入以下命令进行配置: bash $ git con…

    GitHub 2023年5月16日
    00
  • 基于go+vue实现的golang每日新闻数据浏览与检索平台(推荐)

    下面是“基于go+vue实现的golang每日新闻数据浏览与检索平台”的完整攻略: 一、安装并启动后端服务器 首先下载后端服务器的源代码,可以从这里下载:https://github.com/gocn/news 下载完成后,进入到代码所在目录,执行以下命令进行编译: go build -o news main.go 这会在当前目录生成一个名为“news”的可…

    GitHub 2023年5月16日
    00
  • 基于go微服务效率工具goctl深度解析

    基于Go微服务效率工具goctl深度解析 本文将介绍如何使用Go微服务效率工具goctl,从安装、使用到实际案例应用,完整攻略一网打尽。 安装 使用 go get 命令,将goctl工具安装到本地: $ go get -u github.com/tal-tech/go-zero/tools/goctl 安装完成后,使用 goctl 命令即可。 使用 创建项目…

    GitHub 2023年5月16日
    00
合作推广
合作推广
分享本页
返回顶部