Spring Cloud Consul /refresh endpoint missing - java

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;
}

Related

How to integrate config server in spring boot applicaton? getting error

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?

spring boot - springdoc open api: No api definiation provided error

I need to document my spring boot application's rest apis with SpringDoc OpenApi. So, I added this dependency in my pom.xml:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>
And here is my config class:
#Configuration
public class SwaggerConfig {
#Bean
public GroupedOpenApi apis() {
return GroupedOpenApi.builder()
.group("my-application")
.packagesToScan("com.myapp.base")
.pathsToMatch("/**")
.build();
}
}
When I want to use swagger-ui in /base-url/swagger-ui/index.html, the result is as below image:
What's wrong in my configuration?
I use spring boot 2.6.6.
Note: I don't see any problem to load swagger resources in browser console(401, 404). Also I have disabled spring security in app, there is not any problem related to security or loading swagger resources.
You bean looks okay to me but you're probably not hitting the right endpoint for the Swagger-UI.
The URL /base-url/swagger-ui/index.html points to the Swagger-UI provided by the Swagger Core library and there's not a lot you can do about it.
Springdoc has a transitive dependency on Swagger Core and as such the page is available. But the URL at which Springdoc exposes it's Swagger-UI is /base-url/swagger-ui.html.
Refer to the answer here for an explanation on why is it so and the ways to update it.

How to activate JMX monitoring in spring boot standalone app

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

Apply /refresh on multiple instances annotated with #refreshScope

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.

How to register spring MVC application to spring boot admin

I have spring MVC web application. I have used spring boot actuator in it by adding dependancies as below.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
In my configuration file, I have imported classes as below.
#Configuration
#ComponentScan({"com.test.*"})
#Import({EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, EndpointAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class,PublicMetricsAutoConfiguration.class})
#EnableWebMvc
#PropertySource("classpath:appconfig.properties")
public class SpringWebConfig extends WebMvcConfigurerAdapter {
}
Now when I hit url "http://localhost:8080/health", I am getting response
{"status":"UP","diskSpace":{"status":"UP","total":493767094272,"free":417100754944,"threshold":10485760}}
So, this works perfect. Now my question is how to register this spring MVC web application to spring-boot-admin server.
Can anyone help me ?
If you don't want to use Spring Boots autoconfiguration (#EnableAutoConfiguration), have a look at the SpringBootAdminClientAutoConfiguration form the spring-boot-admin-starter-client library there are all components configured used for registering at the admin server.
Primarily the ApplicationRegistrator and some taskScheduler for issuing the frequent registration.
If you don't want to use the client-lib at all you could also register via a simple http post request to the admin-server's /api/applications endpoint.
$ curl -H"Content-Type: application/json" --data '{ "serviecUrl":"http://localhost:8080/", "healthUrl":"http://localhost:8080/health", "managementUrl":"http://localhost:8080/","name":"my-app"}' http://<adminserver-host>:<port>/api/applications

Categories

Resources