当前位置: 首页 > 知识库问答 >
问题:

无法使用spring-cloud-config-server从本地git存储库读取配置

司寇灵均
2023-03-14
package com.home.microservices.limitsservice.bean;


public class Limits {

  private int minimum;
  private int maximum;

  public Limits() {
    super();
  }

  public Limits(int minimum, int maximum) {
    super();
    this.minimum = minimum;
    this.maximum = maximum;
  }

  public int getMinimum() {
    return minimum;
  }

  public void setMinimum(int minimum) {
    this.minimum = minimum;
  }

  public int getMaximum() {
    return maximum;
  }

  public void setMaximum(int maximum) {
    this.maximum = maximum;
  }

  @Override
  public String toString() {
    return "Limits [minimum=" + minimum + ", maximum=" + maximum + "]";
  }
}
package com.home.microservices.limitsservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.home.microservices.limitsservice.bean.Limits;
import com.home.microservices.limitsservice.Configuration;

@RestController
public class LimitsController {

  @Autowired
  private Configuration configuration;

  @GetMapping("/limits")
  public Limits retrieveLimits() {
    return new Limits(configuration.getMinimum(), configuration.getMaximum());
  }
}
@Component
@ConfigurationProperties("limits-service")
public class Configuration {

  private int minimum;
  private int maximum;
  
  public int getMinimum() {
    return minimum;
  }

  public void setMinimum(int minimum) {
    this.minimum = minimum;
  }

  public int getMaximum() {
    return maximum;
  }

  public void setMaximum(int maximum) {
    this.maximum = maximum;
  }
}
logging.level.org.springframework = debug
spring.application.name=limits-service
spring.config.import=optional:configserver:http://localhost:8888
limits-service.minimum=3
limits-service.maximum=997
xml prettyprint-override"><dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <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.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>
class SpringCloudConfigServerApplication:

@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringCloudConfigServerApplication.class, args);
  }
}
logging.level.org.springframework = debug
spring.application.name=spring-cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=file:///D:/Learning/Microservices/git-localconfig-repo
spring.cloud.config.import-check.enabled=false

pom.xml依赖项(spring-cloud-config-server):

<dependencies>
  <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>

Git repo目录位置(我使用Windows):

D:\Learning\Microservices\git-localconfig-repo\limits-service.properties

共有1个答案

汪同
2023-03-14
 @Component
 @PropertySource("classpath:application.properties")
 @ConfigurationProperties(prefix="limits-service")
 public class Configuration {

 @Value("${minimum}")
 private int minimum;
 @Value("${maximum}")
 private int maximum;

 public int getMinimum() {
 return minimum;
 }

 public void setMinimum(int minimum) {
 this.minimum = minimum;
 }

 public int getMaximum() {
 return maximum;
 }

 public void setMaximum(int maximum) {
  this.maximum = maximum;
  }
 } 
logging.level.org.springframework = debug
spring.application.name=limits-service
spring.config.import=optional:configserver:http://localhost:8888
limits-service.minimum=3
limits-service.maximum=997
 类似资料:
  • 我尝试使用后端存储库(文件系统)设置Spring Cloud配置服务器,但是endpoint(< code > http://localhost:8888/licensing service/default )返回以下内容: 主要: 应用程序: Licensingservice.yml: 服务配置的路径: 项目:https://github . com/JD avid-Araujo/sample-

  • 我刚接触spring cloud我的目标是将本地git repo(windows machine-->创建于文件夹z://spring_boot_cloud_microservices_udemy/projects/local-git-repo/)连接到spring cloud Server,但是当我输入相应的url(http://localhost:8888/limit-services/def

  • 我无法从git中读取sping-Cloud-config-server中的属性, 我有两个应用程序配置客户端和配置服务器,我想从配置客户端读取配置服务器git存储库中的属性。 请在下面找到我的代码: 配置客户端 ConfigClientApplication.java 消息Resource.java 独自创立属性 pom。xml 配置服务器 配置服务器pplication.java 独自创立属性

  • 我想用spring cloud config服务器Git从本地文件中获取数据。 我尝试了下面的Yaml配置,但它不工作。

  • 我正在尝试读取本地git存储库中一个微服务的属性文件。它不是在PropertySource中添加文件详细信息。它是一片空白。 请求-http://localhost:8888/limit-service/default 请参考下面的日志,同时试图要求详细信息-

  • 我有一些应用程序从Spring Cloud Config Server(Brixton. RELEASE)获取其配置,并希望启用其属性的自动刷新。我已经按照Spring Cloud Config留档来配置服务器和客户端应用程序,但是在本地git文件上进行更改时不会发生任何事情。 默认配置还检测本地git存储库中的文件系统更改(在这种情况下不使用webhook,但一旦编辑配置文件,就会广播刷新)。