I'm writing spring boot application, which using spring configuration, deployed on pivotal cloud foundry and exposed by Netflix Eureka as discovery serivce / load balancer.
I have created a bean as followed:
#Component
#ConfigurationProperties("config")
#RefreshScope
#Data
public class GeneralProperties {
private boolean ignoreEvent;
}
When calling to the application route that Eureka exposed with /refresh after changing the actual property in the configuration repository, the value that annotated by #refreshScope was changed (end in the response status the field exsiting), which means it's working correctly.
The issue starts when running multiple instances of the same application on the cloud, and calling to the /refresh.
The route that beeing used is the one that exposed by Eureka, which using it's load balancer to route the call to one of the available instances.
It leads to unexpected results that not all the instances are getting updated with the latest change in the property.
Any suggestions how to apply the change on all instances?
You should use Spring Cloud Bus in such a case.
The idea behind this framework is to bind all your application instances to a topic in a message broker (RabbitMQ or Apache Kafka).
Add the following dependency to your pom.xml:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus-parent</artifactId>
<version>1.3.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
In the above example I added a dependency on amqp which is RabbitMQ. You'll also need to bind your application to the RabbitMQ, in PCF it's easy since it's built in to the platform.
When you need to refresh, you should invoke:
POST /bus/refresh
This would trigger an event to a topic that all instances of your application are listening to, and as a result - all instances would refresh their bean configuration.
Good luck.
Related
I am trying to add the Spring Boot actuator on an existing project using Jetty, and I was wondering if it's possible at all, because despite adding the library to the project
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
And enabling those endpoints in the application properties:
endpoints.health.sensitive=false
endpoints.health.enabled=true
management.security.enabled=false
management.endpoints.web.exposure.include=*
And even forcing the endpoints to one specific port:
management.server.port=9001
There's no way I can reach them.
I can definitely see that the endpoints are mapped though:
[2022-10-26 15:00:37] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping INFO : Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal) (AbstractHandlerMethodMapping.java:543)
The question is, where they have been mapped, cause I tried every port, and I can't find those endpoints.
I am trying to integrate config-server service into my spring boot application with multiple services. After adding the changes for config server I am getting below error
APPLICATION FAILED TO START
Description:
No spring.config.import property has been defined
Action:
Add a spring.config.import=configserver: property to your configuration.
If configuration is not required add spring.config.import=optional:configserver: instead.
To disable this check, set spring.cloud.config.enabled=false or
spring.cloud.config.import-check.enabled=false.
I have already tried adding dependency for bootstrap as below
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
But it did not work
Can someone tell me how to integrate config server in spring boot application?
My Spring Boot application calls other providers and I'd like to measure the time a response takes, but also would nice to measure other stuff. Is there a clean way of doing this maybe some library or package?
I'd need something which integrates seamlessly with existing apps, and I do not need to wrap the calls with System.getCurrentTimeMillis() etc.
You can use Spring Actuator,Prometheus Server/Client and Grafana Server to monitoring your application. There are 4 types of metrics available in Prometheus, you can use according to your requirements.
Prometheus Documentation
https://prometheus.io/
Grafana Documentation
http://docs.grafana.org/
Install Prometheus and Grafana servers.
You have to add the dependencies for Prometheus Client. Also Spring Actuator dependency need to add.
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.0.26</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.0.26</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.0.26</version>
</dependency>
In Configuration file you have to define bean for metrics.
#Bean
public ServletRegistrationBean servletRegistrationBean() {
DefaultExports.initialize();
return new ServletRegistrationBean(new MetricsServlet(), "/prometheus");
}
You can follow https://g00glen00b.be/monitoring-spring-prometheus-grafana/ for more detail.
Prometheus can monitor some metrics in the jvm.
But you may want to see the javamelody spring-boot-starter which will give metrics on the jvm and on the application and also on #Service Spring components and on RestTemplate when defined as bean to measure calls to some other providers.
I went via almost all docs and all but not able to get grip on this mysterious stuff.
so my question - Can I use my standalone spring boot app to monitor health and other metrics of my app via http jmx url? Do I need to configure something else for this?
I have added below dependency in boot app.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
I have also configured below properties in my config file.
management.endpoints.web.exposure.include=*
management.endpoints.jmx.unique-names=true
management.server.port=8080
management.server.ssl.enabled=false
When I try to hit URL : http://localhost:8080/actuator/jolokia/health I am not able to see any results.
Also tried adding custom end point like below but not working.
#Endpoint(id="mypoint")
#Component
public class myPointEndPoint {
#ReadOperation
public String mypoint(){
return "Hello" ;
}
with additional property
management.endpoint.mypoint.enabled=true
The problem is the url you are trying to invoke.
First, retrieve the possible mbeans with: http://localhost:8080/actuator/jolokia/list
When you take a look at the Health mbean, you must provide the unique name and the operation (op).
In my case, it looked like: http://localhost:8080/actuator/jolokia/exec/org.springframework.boot:type=Endpoint,name=Health,identity=4200098/health
Also check the Jolokia documentation: https://jolokia.org/reference/html/index.html
I'm using Spring Cloud Consul for Distributed Configuration with Consul and all goes fine. All configuration is currently and successfully read from Consul server on startup of the application. But I can't reload this configuration for my app when some data on Consul changed because there is not /refresh endpoint. But here says "Sending a HTTP POST to /refresh will cause the configuration to be reloaded." As I understand it should be like for Spring Cloud Config Client but it doesn't. What did I miss?
You need to include the spring boot actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Or add #RefreshScope on your bean for e.g.
#Component
#RefreshScope
public class MyConsulConfig {
#Value("${consul.base.url}")
private String baseUrl;
}