问题
After days of search over internet and many tries(out of which nothing worked out for me), i am writing here
I have Project- spring-cloud-config-server which has following files(full project can be accessed from https://github.com/AshishBharadwaj94/spring-cloud-config-server.git)
- SpringCloudConfigServerApplication.java
- application.properties (under folder- resources)
- Link Source Folder- git-config-repo which has limits-service's properties files
- pom.xml
and Client Config Project- limits-service which has following files(full project can be accessed from https://github.com/AshishBharadwaj94/limits-service.git)
- LimitsServiceApplication.java
- LimitConfiguration.java
- Configuration.java
- LimitsConfigurationController.java
- bootstrap.properties (under folder- resources)
- pom.xml
Also project- https://github.com/AshishBharadwaj94/git-config-repo.git
Project- spring-cloud-config-server is working fine and has the following output
// http://localhost:8888/limits-service/default
{
"name": "limits-service",
"profiles": [
"default"
],
"label": null,
"version": "8dd340030a5c82a11327bf0d2e55ae3b6434240b",
"state": null,
"propertySources": [
{
"name": "file:///D:/Learn-Java/springboot-workspace/git-config-repo//limits-service.properties",
"source": {
"limits-service.minimum": "11",
"limits-service.maximum": "99"
}
}
]
}
but when running Project- limits-service, i have the following output. It is unable to fetch data from spring-cloud-config-server
// http://localhost:8080/limits
{
"minimum": null,
"maximum": null
}
Below are the files Project wise
spring-cloud-config-server
SpringCloudConfigServerApplication.java
@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
application.properties
spring.application.name=spring-cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=file:///D:/Learn-Java/springboot-workspace/git-config-repo/
spring-cloud-config-server/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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0-M4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.microservices</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0-SNAPSHOT</spring-cloud.version>
</properties>
<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>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
limits-service.properties
limits-service.minimum=11
limits-service.maximum=99
limits-service
LimitsServiceApplication.java
@SpringBootApplication
public class LimitsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(LimitsServiceApplication.class, args);
}
}
LimitConfiguration.java
@Component
public class LimitConfiguration {
private String minimun;
private String maximun;
public LimitConfiguration() {
super();
}
public LimitConfiguration(String minimun, String maximun) {
super();
this.minimun = minimun;
this.maximun = maximun;
}
public String getMinimun() {
return minimun;
}
public void setMinimun(String minimun) {
this.minimun = minimun;
}
public String getMaximun() {
return maximun;
}
public void setMaximun(String maximun) {
this.maximun = maximun;
}
@Override
public String toString() {
return "LimitConfiguration [minimun=" + minimun + ", maximun=" + maximun + "]";
}
}
Configuration.java
@Component
@ConfigurationProperties("limits-service")
public class Configuration {
private String minimum;
private String maximum;
public String getMinimum() {
return minimum;
}
public void setMinimum(String minimum) {
this.minimum = minimum;
}
public String getMaximum() {
return maximum;
}
public void setMaximum(String maximum) {
this.maximum = maximum;
}
}
LimitsConfigurationController.java
@RestController
public class LimitsConfigurationController {
@Autowired
private Configuration configuration;
@GetMapping(path="/limits")
public LimitConfiguration retrieveLimitsFromConfiguration() {
return new LimitConfiguration(configuration.getMinimum(), configuration.getMaximum());
}
}
bootstrap.properties
spring.application.name=limits-service
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.enabled=true
limits-service/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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0-M4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.microservices</groupId>
<artifactId>limits-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>limits-service</name>
<description>Build microservices with spring cloud</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0-SNAPSHOT</spring-cloud.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
来源:https://stackoverflow.com/questions/64880039/unable-to-fetch-values-from-spring-cloud-config-server-to-my-config-clientlimit