无法从spring云配置服务器获取值到我的配置客户端(限制服务)

zc0qhyus  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(242)

经过几天的网上搜索和多次尝试(没有任何结果),我写在这里
我有一个project-spring云配置服务器,它有以下文件(完整的项目可以从https://github.com/ashishbharadwaj94/spring-cloud-config-server.git)
springcloudconfigserverapplication.java
application.properties(文件夹-资源下)
链接源文件夹-git config repo,它限制了服务的属性文件
pom.xml文件
和客户端配置项目-限制具有以下文件的服务(可以从https://github.com/ashishbharadwaj94/limits-service.git)
limitsserviceapplication.java文件
有限配置.java
配置.java
LimitsConfiguration控制器.java
bootstrap.properties(在文件夹-资源下)
pom.xml文件
同时项目-https://github.com/ashishbharadwaj94/git-config-repo.git
project-springcloud配置服务器工作正常,有以下输出

// 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"
      }
    }
  ]
}

但是当运行project-limits服务时,我有以下输出。它无法从spring云配置服务器获取数据

// http://localhost:8080/limits

{
  "minimum": null,
  "maximum": null
}

以下是项目方面的文件
spring云配置服务器
springcloudconfigserverapplication.java

@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication {

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

}

应用程序属性

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云配置服务器/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.minimum=11
limits-service.maximum=99

限制服务
limitsserviceapplication.java文件

@SpringBootApplication
public class LimitsServiceApplication {

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

}

有限配置.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 + "]";
    }

}

配置.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;
    }

}

LimitsConfiguration控制器.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

限制服务/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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题