Nisarg Bhagavantanavar
Nisarg Bhagavantanavar

Reputation: 353

Refresh Scope without config Server

i want to refresh bean when database is updated in runtime, so want to use /refresh endpoint to manually trigger it and refresh bean with new value.

but currently we are not using config server - can anyone guide how i can only make use of refresh scope

I just want to make use of refresh scope without needing config server.

My bean get value from the database for example

@Configuration
public class AppConfiguration {
    @Autowired
    MyRepository myRepo;

    @Bean
    @RefreshScope
    DemoWebClient getDemoWebClient() {
        String currentTime = new Date().getTime() + "";
        return new DemoWebClient(myRepo.getTenantId());
    }

}

tried to use spring cloud starter

2022-01-16 13:25:10.733  INFO 17884 --- [-144.54.177.213] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2022-01-16 13:25:10.733  WARN 17884 --- [-144.54.177.213] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/application/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
2022-01-16 13:25:13.836  INFO 17884 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
tried to disable config server lookup - didn't work

spring.cloud.config.enabled=false
spring.cloud.config.import-check.enabled=false

below is my pom

<?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.2.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>RefreshScopeDemoApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>RefreshScopeDemoApp</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Hoxton.SR8</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-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>
    </repositories>

</project>

Upvotes: 0

Views: 2146

Answers (1)

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 18979

You need to have the actuator enabled on your application. Then you can just call the endpoint /actuator/refresh of your application, to refresh the beans that need to be refreshed.

The important thing you must understand is that, the actuator must be enabled on the client application of the config server, meaning the application that connects to the config server and not the actual config server.

Upvotes: 0

Related Questions