SpringCloudConfig客户端没有从服务器自动获取配置

z31licg0  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(266)

我正在实现springcloud配置,但是客户端没有自动获取配置。。。当我启动时,我的客户机正确地从服务器获取数据,但是再也没有检索到它。
这是我的cloudconfig服务器。

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class CloudConfigApplication {

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

}
应用程序.yml

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:9091/eureka
server:
    port: 8888
spring:
    application:
        name: config-server
    cloud:
        config:
            server:
                bootstrap: true
                health:
                    enabled: true
                native:
                    searchLocations: file:${user.dir}/src/main/resources/configs/
    profiles:
        active: native

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.1.8.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>CloudConfig</groupId>
    <artifactId>CloudConfig</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CloudConfig</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>
    <dependencies>
        <!-- 3a) Dependency for spring-cloud-config-Server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!-- 3b) Dependency for testing boot projects -->
        <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-starter-actuator</artifactId>

        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </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>

</project>

/src/main/resources/config/cloudconfigclient.yaml

test: aaaaa
test2: 1111

springcloud客户端

@EnableScheduling
@EnableDiscoveryClient
@SpringBootApplication
@EnableAutoConfiguration

public class CloudConfigClient {

     @Value("${test}")
     private String myProperty;

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

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Scheduled(fixedDelay =1000)
    public void printProperty() {
        System.out.println("Value of property \"myProperty\": " + myProperty);
    }
}

引导.yaml

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:9091/eureka
server:
    port: 7004

    datasource:
        driver-class-name: org.h2.Driver
    application:
        name: CloudConfigClient
    cloud:
        config:
             uri: http://localhost:8888
             fail-fast: true
             max-attempts: 20
             max-interval: 15000
             initial-interval: 10000

pom.xml文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>

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

这是输出日志:

2020-11-20 23:33:12.747  INFO 16764 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-11-20 23:33:13.510  INFO 16764 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=CloudConfigClient, profiles=[default], label=null, version=null, state=null
2020-11-20 23:33:13.510  INFO 16764 --- [  restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='file:D:/CloudConfig/src/main/resources/configs/CloudConfigClient.yaml'}]}

...
...
Value of property "myProperty": aaaaa
Value of property "myProperty": aaaaa
6kkfgxo0

6kkfgxo01#

如果你想要你的配置被刷新,你必须使用@refreshscopespring doc,因为你调用@scheduled,所以这个配置不会被magic自己更新。
但根据您在配置组件中获取值的方式,它们可能会受到限制,@refreshscope将无法在refreshscope中工作
我建议在@configurationproperties中创建一个bean,并用@refreshscope进行注解,当您从执行器端点/refresh点击时,一切都应该更新,或者@schedule的组合可能会实现您想要的

相关问题