spring boot refresh the properties files without config server - spring-boot

I want to refresh the properties files in spring boot.
version - 2.1.4.RELEASE
In case of spring config server have to connect all my app to config server which i dont want as our application is in prod and we dont want a bigger change.
Is it possible in the same application i can refresh the prop file using config server if not using config server then using some spring code
can i do it.
NOT application.properties , Have a application-optional.properties outside of project want to refresh only this.

The RefreshEndpoint is provided by Spring Cloud. Adding the spring cloud starter config will get you the required beans and API to refresh.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Additionally you will need to expose the API via management.endpoints.web.exposure.include="health,info,refresh" or you can use "*" to expose every enabled actuator endpoint to the web.

Related

Spring boot actuator for commandline runner application

I want to enable actuator for health check in PCF for the non web or batch or commandLineRunner spring boot application.
Could some one please share some details to resolve this issue.
Please let me know if any other details required
I tried the below options
Adding actuator dependency
Adding http and jmx related properties
Since localhost:8081/actuator is not accessible could not view endpoints
How to enable url for non web app
add
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.properties
spring.jmx.enabled = true
management.endpoints.jmx.exposure.include=*
management.endpoints.jmx.domain=com.example.demomail
management.endpoints.jmx.unique-names=true
run jconsole find your application name then connect.
find Tab MBeans, find com.example.demomail in left tree.
As far as I understand you need to enable the Actuator endpoints.
You must add the Spring Boot Actuator dependency (as you have already done).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Then in your application.properties file enable HTTP and JMX support (as you have already done).
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.endpoint.jmx.exposure.include=*
Now you will have to push your app to PCF and create a binding to a route to create access for the Actuator endpoint/s.
The URL to access the actuator endpoint will be something like this http://<route-url>/actuator/health
In the command line, I would use something like this to return a list of routes and the URL route of the application.
cf curl /v2/apps/<app-guid>/routes

spring cloud config client set config url at property file

I have a spring cloud project. There are config server (config-srv), eureka server (eureka-srv) and some other service with business logic, let's call it main service (main-srv).
I'm packaging them into jars and run one by one. My apps get their properties from a file in the same directory with jars.
So, I'm setting properties to config-srv through "application-native.properties".
But if i want to change config-srv url (for example, i want http://localhost:9999), how can i share this url for all micro-services before they will boot ? I'm trying to share this url in "application-default.properties" but it makes no effect.
You can put the Spring Cloud Config server details in bootstrap.properties file in each microservices.
spring.application.name=microserviceName
spring.profiles.active=dev
spring.cloud.config.uri=http://localhost:9999
spring.cloud.config.username=your_username
spring.cloud.config.password=your_password
Go through the link to have more detail about spring cloud config
https://howtodoinjava.com/spring-cloud/spring-cloud-config-server-git/

Spring Cloud Bus - Refresh a specific cluster of client

I have a spring cloud config server configured over spring cloud bus over kafka.
I am using Edgware.RELEASE with boot 1.5.9.
When I send a POST request to the endpoint /bus/refresh with destination=clientId:dev:** in the body via POSTMAN to config server, all the clients get their beans refreshed even though their clientId doesn't match the value in the destination field.
Here are additional configuration details:
spring cloud config server
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
application.properties
server.port=8888
management.security.enabled=false
spring.cloud.bus.enabled=true
spring.kafka.bootstrap-servers=localhost:9092
I have two config clients with id - config-client1 and config-client2
After changing an application property in the repository for config-client2, I submit a POST request to /bus/refresh endpoint on the config server, with destination=config-client2:dev:** in the body of the request. I was hoping this would refresh/re-initialize the beans only in config-client2 application. But I noticed beans in config-client application are also refreshed/re-initialized. I also noticed config-client application receiving refresh event along with config-client2.
I was hoping only config-client2 application receives the event and its beans are the only ones that are refreshed as a result of it.
Wondering if I am missing any configuration setting to enable granular refresh at specific client level.
I did go through the document posted at - http://cloud.spring.io/spring-cloud-static/Edgware.RELEASE/single/spring-cloud.html, but the results are not as explained.
Any help with a working example is appreciated.
You can use:
curl -X POST http://localhost:8001/actuator/bus-refresh/config-client2
You also need this in your application.properties or .yml.
spring.cloud.config.uri=http://localhost:8001
spring.cloud.config.uri should point to your Spring Config Server

Does PCF Dev show Actuator endpoints and expose log level configuration for Java app?

I'm running a demo Spring Boot app with an actuator enabled in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
however the endpoints the actuator is supposed to provide (Health Checks, Traces, etc.) are not showing in the PCF Dev Apps Manager:
Is that normal behavior and they would be enabled on the public cloud deployment or am I missing something in my configuration?
Similarly, on the Logs tab I don't see a log level configuration control through which setting a log level on a particular Java package is possible. Is that the regular behavior? How to configure logging and tracing?
For logging setup , you need to add configurations in applications.properties and it is not available as separate option in the logs window. For changing your logs at run time, you need to setup a config server/environment variable and use your actuator ends point to refresh it at run time. Hope this helps.

where is port 8080 defined in spring cloud app?

I have been doing a line by line study of the code in this spring cloud app on github after downloading and installing it on my devbox. When you type http://localhost:8080 in your web browser, the content from the ui app loads, and is able to interact with the content from the authserver and resource apps.
Port 9999 is defined for the authserver app, both in the ui app's zuul route definitions, and also in the authserver app's application.properties.
Similarly, port 9000 is defined for the resource app, both in the ui app's route definitions, and also in the resource app's application.properties.
But I cannot find any reference to port 8080, even after reviewing every line of code in the three apps and also doing a Ctrl-H to do full text search of the entire eclipse workspace for the word 8080.
So how does the ui app know to serve its client content on port 8080?
The UI module has the following Spring Boot dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
That dependency has its own dependencies as seen here:
Spring Boot Starter Web POM.xml
This includes the Spring Boot's Tomcat Starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
Which brings in the actual Tomcat embedded jars
Spring Boot Starter Tomcat POM.xml
Because the UI application is a Spring Boot app (eg it uses the Spring Boot Parent POM and has annotated its main class with #SpringBootApplication) it will autoconfigure many things for us based on the Classpath.
Notice the #ConditionalOnClass annotation in this class:
EmbeddedServletContainerAutoConfiguration
That eventually leads to this class:
TomcatEmbeddedServletContainer
Which triggers the startup of the embedded Tomcat server.
The Default Port 8080 setting is actually coming from the embedded Tomcat dependency as seen here:
org.apache.catalina.startup.Tomcat
protected int port = 8080;
You can use a .properties or .yml file to store all the configuration of your application. There you can define the server port property to listen in whatever port you want.
If you use a .properties file it would be something like this :
server.port=8082
Or in a .yml file it would be something like this:
server:
port: 8082
For more about properties and configuratio in spring, check this
there is a reference of all spring boot default properties which should be checked as soon as you hit some unknown defaults :)
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

Resources