如果设置了spring.cloud.config.server.bootstrap=true,则需要使用复合配置

问题描述 投票:0回答:4

问题:如何获取该位置的数据https://github.com/TEK-Leads/Config-Properties.git

当我尝试运行此应用程序时,它不显示任何结果,它只显示默认的传递结果(参见。 MessageController)。或 ConfigClient 端日志显示 这种类型的按摩。

无法找到 PropertySource:“http://localhost:8888/application/default”的 GET 请求出现 I/O 错误:连接被拒绝;嵌套异常是 java.net.ConnectException: 连接被拒绝

config-Serverapplication.yml文件更改为bootstrap.yml时,在下面抛出异常时。

If you are using the git profile, you need to set a Git URI in your configuration.  If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

配置服务器部分


Pom.xml

    <properties>
    <java.version>11</java.version>
    <spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

application.yml

server:
  port: 9090

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/TEK-Leads/Config-Properties.git
          clone-on-start: true
        bootstrap: true
      enabled: true
   
  application:
    name: Config-Server

management:
  security:
    enabled: false

ConfigurationServerApp.class

package com.ramgovindhare.configserverapp;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServerAppApplication {

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

配置客户端部分


Pom.xml

<properties>
    <java.version>11</java.version>
    <spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

application.yml

spring:
  application:
    name: client-config
    
  cloud:
    config:
      uri: http://localhost:9090
  config:
    activate:
      on-profile:
        active:prod
            
management:
  security:
    enabled:false
  

ConfigClient.class

    package com.ramgovindhare.configserverclientapp.contorller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class MessageController {
    
    @Value("${msg:Config Server is not working. Please check..}")
    private String msg;
    
    @GetMapping("/msg")
    public String getMsg() {
        return this.msg;
    }
}
java spring spring-boot microservices spring-cloud-config
4个回答
5
投票

尝试在

pom.xml
文件中添加依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

2
投票

如果没有什么对你有用,那么我希望你尝试从配置服务器中的 pom.xml 中删除以下 xml 标签。

<packaging>pom</packaging>

另外,不要忘记您只需要一个依赖项即可开始使用 Spring Cloud Config Server。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

1
投票

如记录这里

如果使用引导标志,配置服务器需要有它的 bootstrap.yml 中配置的名称和存储库 URI。

您没有发布引导程序文件,因此不清楚您是否已定义它们。如果没有,这可能是问题的根源,因为您已将 spring.cloud.config.server.bootstrap 设置为 true 。在配置服务器资源文件夹内的 bootstrap.properties 文件中,您需要定义以下属性:

配置服务器bootstrap.properties

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/TEK-Leads/Config-Properties.git
          clone-on-start: true
        bootstrap: true
      enabled: true
   
  application:
    name: Config-Server

配置客户端bootstrap.properties

spring:
  application:
    name: client-config
    
  cloud:
    config:
      uri: http://localhost:9090
  config:
    activate:
      on-profile:
        active:prod

还值得注意的是,自 Spring Boot 2.4 起,引导上下文初始化已被弃用,因此此设置不再需要使用引导。有关更多信息,请访问 https://docs.spring.io/spring-cloud-config/docs/current/reference/html/


0
投票

我现在也遇到同样的问题。我的 POM.xml 中有 bootstrap 依赖项,并且在配置服务器的 bootstrap.yml 文件中设置了名称和存储库 URI。有趣的是,我只得到这个

如果您使用 git 配置文件,则需要在配置中设置 Git URI。如果设置了 spring.cloud.config.server.bootstrap=true,则需要使用复合配置。

当我的 Docker 容器尝试启动我的配置服务器时出现错误。在我的 IDE 中运行时似乎工作正常。遗憾的是,我需要对其进行 Docker 化。看来我们仍然需要解决这个问题哈哈。

© www.soinside.com 2019 - 2024. All rights reserved.