简体   繁体   English

在Spring Cloud中使用@RefreshScope重新加载Spring属性时出错

[英]error while reloading spring properties with @RefreshScope in spring cloud

I'm testing one prototype for spring-cloud using @RefreshScope annotation. 我正在使用@RefreshScope注释测试spring-cloud的一个原型。 I'm using one external property file for that. 我为此使用了一个外部属性文件。 Which I have provided and configured in runtime argument. 我已经在运行时参数中提供并配置了它。

pom.xml dependency 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      http://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.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.proto.reload</groupId>
<artifactId>CachingService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CachingService</name>
<description>Demo project for cache</description>

<properties>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-context</artifactId>
        <version>1.1.8.RELEASE</version>
    </dependency>

    <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> 
        <version>1.1.3.RELEASE</version> </dependency> -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

This is main class 这是主班

package com.proto.reload;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.ApplicationPidFileWriter;

@SpringBootApplication
public class CachingServiceApplication {

public static void main(String[] args) {
    if (args.length == 0) {
        System.out.println("Invalid Commandline argument. Please provide Consumer Properties file.");
        System.exit(0);
    }
    new SpringApplicationBuilder(CachingServiceApplication.class)
            .listeners(new ApplicationPidFileWriter())
            .properties("spring.config.name:" + args[0].replaceAll(".properties", ""))
            .build()
            .run(args);

    while (true) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ConfigClass.getName());
    }
}

}

Config class having @RefreshScope annotation 具有@RefreshScope批注的配置类

package com.proto.reload;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

@Configuration
@RefreshScope
public class ConfigClass {

    private static String name;

    public static String getName() {
        return name;
    }

    @Value("${app.prop.name}")
    public void setName(String name) {
        this.name = name;
    }

}

And this is property file I'm passing through argument 这是我要传递参数的属性文件

app.prop.name=FirstOldValue

spring.pid.file=TestOne.pid

When I run this code, I get the following error. 运行此代码时,出现以下错误。

   12:38:41.051 [main] DEBUG org.springframework.boot.context.logging.ClasspathLoggingApplicationListener - Application failed to start with classpath: [file:/C:/Program%20Files/Java/jre1.8.0_191/lib/resources.jar, file:/C:/Program%20Files/Java/jre1.8.0_191/lib/rt.jar, file:/C:/Program%20Files/Java/jre1.8.0_191/lib/jsse.jar, file:/C:/Program%20Files/Java/jre1.8.0_191/lib/jce.jar, file:/C:/Program%20Files/Java/jre1.8.0_191/lib/charsets.jar, file:/C:/Program%20Files/Java/jre1.8.0_191/lib/jfr.jar, file:/C:/Program%20Files/Java/jre1.8.0_191/lib/ext/access-bridge-64.jar, file:/C:/Program%20Files/Java/jre1.8.0_191/lib/ext/cldrdata.jar, ...]
12:38:41.105 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:120)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:84)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:62)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:347)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
    at com.proto.reload.CachingServiceApplication.main(CachingServiceApplication.java:19)

Am I using @RefreshScope in wrong way ? 我以错误的方式使用@RefreshScope吗? This code works fine when I remove dependency and @RefreshScope, but its not updating property value at runtime. 当我删除依赖项和@RefreshScope时,此代码可以正常工作,但在运行时不会更新属性值。 My goal is to load modified property value at runtime. 我的目标是在运行时加载修改后的属性值。

1.1.8.RELEASE for Spring Cloud is part of the Camden Release train which is for Spring Boot 1.4.x releases. 1.1.8.RELEASE Cloud的发布是Camden Release系列的一部分,该系列适用于Spring Boot 1.4.x版本。

Please see the Release Trains section for what release train to use for your Spring Boot version 请参阅“ Release Trains部分,了解用于您的Spring Boot版本的发行说明。

https://spring.io/projects/spring-cloud https://spring.io/projects/spring-cloud

Currently they are, 目前他们是

Release Train Boot Version 发行火车启动版本

Greenwich 2.1.x 格林威治2.1.x

Finchley 2.0.x 芬奇利2.0.x

Edgware 1.5.x Edgware 1.5.x

Dalston 1.5.x 达斯顿1.5.x

As your using spring boot 2.1.1 your dependency management should look like the below, note as Greenwich doesn't have a RELEASE yet you need to add the milestone repository. 当您使用Spring Boot 2.1.1时,您的依赖项管理应如下所示,请注意,因为Greenwich没有RELEASE但您需要添加里程碑存储库。

 <repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RC2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

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

</dependencies>

Add cloud context depedency and try again 添加云上下文权限,然后重试

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-context</artifactId>
        <version>1.1.8.RELEASE</version>
    </dependency>

Update : it is just not a valid use case to have @Configuration and @RefreshScope on the same element. 更新:在同一元素上具有@Configuration和@RefreshScope只是一个无效的用例。 As per this URL thread : https://github.com/spring-cloud/spring-cloud-config/issues/43 按照这个URL线程: https : //github.com/spring-cloud/spring-cloud-config/issues/43

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM