使用SpringCloud Config结合github无法读取配置的问题

前言

配置中心存放文件在github是读取过程,可能你会出现读取不到配置信息。本次笔者将这一过程进行详细介绍。

准备

父工程

由于笔者是使用聚合工程,所以这次也是把相关的工程创建说明写上。当然你也可以完全创建两个独立的工程来引用。
创建父工程时直接只有一个pom文件,以下是这个文件的依赖信息

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>ch2-2-eureka-client</module>
        <module>ch2-3-config-server</module>
        <module>ch4-1-feign</module>
        <module>ch5-1-zuul</module>
        <module>config-client</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ch2-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ch2-1</name>
    <description>eureka</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

</project>

子工程-config-server

这个工程内容目录如下图
在这里插入图片描述

依赖

由于在父工程已经引入了WEB,所以这里只引入spring-cloud-config-server,另外一个spring-boot-starter-actuator主要是用来查看端点信息的,可以不引用,后续手机刷新时需要这个开启相关的访问端点(好像是,具体后续可能有相关文章再细说)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ch2-1</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ch2-3-config-server</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

</project>

启动主类

package springcloud;

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

@SpringBootApplication
//只加这个即可,表示这个启动的是config的服务中心
@EnableConfigServer
public class Ch21ConfigServerApplication {

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

}

配置文件

服务端的配置不需要注意什么的,按下面的配置即可,因为默认是git的,所以不需要写profiles 指向git了

server:
  port: 9090
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xxx1/xxx2.git
          #xxx2 是指存放当前配置文件的仓库名
          username: 写自己的github账号
          password: 写自己的github密码

启动项目后,访问http://localhost:9090/config/dev/main 即可以看到配置信息 这个地址要注意的是
http://localhost:9090/{配置文件名前缀}/{环境类型}/{仓库分支标签}
如在仓库创建的文件名为config-dev.yml,那么配置文件名前缀就是config,环境就是指文件名后缀 dev,仓库标签就是当前存放文件的仓库分支名
看到以下信息说明启动项目且配置获取成功
在这里插入图片描述

子工程-config-client

子工程就是个问题了,当时创建时一直无法读取配置信息就是子工程这里出现的问题,有两个问题觉得要说明的,先看下子工程创建过程。

pom文件


```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ch2-1</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>ch2.springcloud</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
</dependencies>

</project>

``

启动类

package cn.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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


配置类或者通过@Value获取配置信息说明

package cn.springcloud.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "cn.springcloud.book")
public class ConfigInfoProperties {
    private String config;

    public String getConfig() {
        return config;
    }

    public void setConfig(String config) {
        this.config = config;
    }
}

测试类Controller

这个类写了两个获取配置信息的方式,一个是通过@Autowired注入配置类,一个是通过@Value来获取

package cn.springcloud.controller;

import cn.springcloud.config.ConfigInfoProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {
    @Autowired
    private ConfigInfoProperties configInfoProperties;
    @Value("${demo.value}")
    private String value;
    @GetMapping("/getmsg")
    public String getMsg(){
        System.out.println("value: "+value);
        System.out.println("this config is: "+configInfoProperties.getConfig());
        return configInfoProperties.getConfig();
    }
}

配置文件

这里的配置文件就是一个问题点了,配置如下启动时会先摘取配置信息再启动,所以把配置中心的配置放到bootstrap.yml中
问题:label: main 这个是指配置指向当前创建的分支,如果没有则默认是master,网上就是这样说的,后来发现,现在的直接在github创建仓库后,显示的是main,所以当时我没有配置或者配置成master时一直获取不了配置信息,所以重新查看了仓库信息,如下图:
在这里插入图片描述

bootstrap.yml说明:

spring:
  cloud:
    config:
      label: main
      uri: http://localhost:9090
      name: config
      profile: dev

application.yml说明

server:
  port: 9000
spring:
  application:
    name: config-client

另一个问题

配置了配置中心的URL,即上面bootstrap.yml中的 uri: http://localhost:9090,但是项目一直启动的是访问 uri: http://localhost:8888,当时就纳闷,找了很久都没有找到在哪里配置了8888,后来又是清缓存,还是不行,最后在client添加server依赖包,再重启,结果发现正常了,正常后又把它删除,也正常了。

org.springframework.cloud
spring-cloud-config-server

从github拉取的文件在本地存放的目录

如果你不知道文件在哪里,在启动server时又显示了以下信息,则说明文件是拉取到本地了:
在这里插入图片描述
当前你也可以直接在电脑上查找文件名,看到以下类似目录即可以找到
在这里插入图片描述
不知道能不能指定目录的,这个读者可以试下

测试结果

启动server client,访问client提供的接口localhost:9000/getmsg

结果如图,说明正常获取信息了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值