Spring boot Config client : Refresh not working
Asked Answered
M

5

5

Unable to refresh the Config files by using 'http://localhost:9001/refresh'. If I restart the Client application, the updated config's are loading fine. The following is the simple rest controller I am using to test the same. The refresh is run using the curl command 'curl -d {} localhost:9001/refresh/',which is giving 404 error.

@RestController
@RefreshScope
class ExampleController {

    @Value("${Message2}")
    private String message2 = "Hello World";

    @RequestMapping
    public String sayValue() {
        return message2;
    }
}

The following is the pom.xml which I am using

<groupId></groupId>
<artifactId>MyConfigurationClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>MyConfigurationServer</name>
<description>Demo project for Spring Boot</description>

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.M8</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-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>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

Merril answered 19/3, 2018 at 14:2 Comment(2)
Should'nt the http method be POST? 'curl -X POST localhost:9001/refresh'Complicacy
Also /refresh endpoint only refreshes those properties annotated with @ConfigurationProperties means it does not refresh those properties which are initialized during app initialization.Chlorella
W
9

Actuator dependency should be added in pom.xml to use.

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

Include refresh endpoint by adding below entry in application.properties or bootstrap.properties.

management.endpoints.web.exposure.include=refresh

Call refresh endpoint to reload properties without restarting the application. http://localhost:8080/actuator/refresh (Use http post method not get)

@ConfigurationProperties - Will reload respective properties with actuator refresh call itself.

@Value - Will load properties at startup and will not reload with refresh call. - To reload properties annotated with @Value you need to,

  • Restart the application.
  • It can also be done without restart just by annotating the class with @RefreshScope (spring cloud config annotation) that uses @Value.
Winch answered 7/10, 2019 at 11:4 Comment(1)
the configuration management.endpoints.web.exposure.include=refresh was what was missing. worked fine calling the end point, now is create a way to the server config call a post on http://localhost:8080/actuator/refresh on the clients.Plat
I
1

Just check, you config-client has set the property

server.port: 8080
management.endpoints.web.exposure.include=*

And then run the POST refresh request (Not GET)

curl --location --request POST 'http://localhost:8080/actuator/refresh'

Good luck!

Indole answered 16/11, 2022 at 17:55 Comment(0)
S
0

I found the answer in another stack overflow question.

In my case I had to set the property

management.endpoints.web.exposure.include=*
# management.endpoints.web.exposure.include=xyz

which enables the "/actuator/refresh" url (note the actuator part!), and add a class

package here.org.your.put;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 *
 */
@Component
@ConfigurationProperties(prefix = "encrypted")
public class PropertyConfiguration {

  private String property;

  public String getProperty() {
    return property;
  }

  public void setProperty(String property) {
    this.property = property;
  }
}

which has a method setProperty that is invoked by "refresh". It takes my encrypted property (encrypted.password in application.properties of my client) from a git repository and decrypts using the spring config server I am also running. It then sets the value in this class.

Scantling answered 12/11, 2018 at 2:56 Comment(0)
O
0

Solution That Worked For Me!

In the Client Configuration, properties change:

spring.cloud.config.uri=http://localhost:8888/ to spring.config.import=configserver: (where configserver is the name of my Spring config server).


application.properties:

spring.application.name=myclient1
spring.config.import=configserver:
management.endpoints.web.exposure.include=*

Configuration class looks like:

@ConfigurationProperties("greetings")
@RefreshScope
@Data
public class Config{
    String say;
}

Controller class looks like:

@RestController
@AllArgsConstructor
public class MyController {
  private final Config config;

  @GetMapping
  String returnValue() {
    return config.getSay();
  }
}

In Dependency management system include below 3 dependencies from Spring Initializer.

Spring Cloud Config dependency


IMPORTANT:

Lastly, call your actuator refresh endpoint post making changes to your properties file: http://localhost:8081/actuator/refresh

Orvas answered 5/4, 2024 at 5:39 Comment(0)
H
-1

Refresh endpoint is included in actuator. Please add actuator dependency and try this with "http://localhost:9001/actuator/refresh" endpoint.

Hyde answered 31/7, 2018 at 9:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.