Refresh server.port of a Spring Cloud Config Client dynamically via /actuator/refresh

1.2k views Asked by At

Is it possible to make a Spring Cloud Config Client application refresh its configuration dynamically in a way, that it would restart its web server on a different "server.port" that has been read from updated configuration via Spring Cloud Config Server?

With the simplest example project (just one RestController using spring-boot-starter-web) with the "application.properties" below, "/actuator/refresh" works fine for "normal" properties.

Having "server.port: 12345" in the config (config-client-default.yml) provided by the config server, the config client properly starts on port 12345. When I change the configuration file served by the config server (e.g. to "server.port: 8080") and call the "/actuator/refresh" endpoint of the client (e.g. via "curl -sX POST http://localhost:12345/actuator/refresh", I can see that the configuration had been re-read from the config server, but the client does not restart its web server on the new port (8080).

Is it possible to dynamically refresh (reload) "server.port" using "/actuator/refresh", preferably with as little additional code as possible? Thanks in advance.

### Spring Cloud Config Server application.properties :
server.port=8888
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.server.git.uri=file:///D:/DEVEL/local_git
spring.cloud.config.server.git.default-label=master
spring.cloud.config.server.health.enabled=false
### Spring Cloud Config Client application.properties :
management.endpoints.web.exposure.include=*
spring.application.name=config-client
spring.config.import=configserver:http://localhost:8888
### Config client startup :
$ java.exe -jar config-client-0.0.1-SNAPSHOT.jar
:: Spring Boot ::                (v2.6.5)
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : Starting ConfigClientApplication using Java 11.0.14 on PC with PID 17028 (config-client-0.0.1-SNAPSHOT.jar started by Me in D:\DEVEL\config-client)
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : No active profile set, falling back to 1 default profile: "default"
INFO 17028 --- [main] o.s.b.context.config.ConfigDataLoader    : Fetching config from server at : http://localhost:8888
INFO 17028 --- [main] o.s.b.context.config.ConfigDataLoader    : Located environment: name=config-client, profiles=[default], label=null, version=2262dfe9b9ffa387cdb5b3253a0951a084d35a1c, state=null
INFO 17028 --- [main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6cab5e7d-ad01-347c-8b2d-01847b263bb8
INFO 17028 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 12345 (http)
INFO 17028 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
INFO 17028 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]
INFO 17028 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
INFO 17028 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 735 ms
INFO 17028 --- [main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 15 endpoint(s) beneath base path '/actuator'
INFO 17028 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 12345 (http) with context path ''
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : Started ConfigClientApplication in 1.866 seconds (JVM running for 2.153)
### Changed the client configuration served by the config server :
$ curl -sS http://localhost:8888/config-client/default | json_pp | fgrep 'server.port'
            "server.port" : 8080
### Calling the refresh endpoint :

$ curl -sX POST http://localhost:12345/actuator/refresh
["config.client.version","server.port"]

### Config client log (continuation):
INFO 17028 --- [io-12345-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 17028 --- [io-12345-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
INFO 17028 --- [io-12345-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
INFO 17028 --- [io-12345-exec-1] o.s.b.context.config.ConfigDataLoader    : Fetching config from server at : http://localhost:8888
INFO 17028 --- [io-12345-exec-1] o.s.b.context.config.ConfigDataLoader    : Located environment: name=config-client, profiles=[default], label=null, version=c92ba03724cb0b06b88a57d19fe79048d830b481, state=null

### (Config client still running on port 12345 - not 8080)
$ curl -sX POST http://localhost:12345/actuator/refresh
[]
0

There are 0 answers