Is it necessary to add Spring Web when using Spring Actuator? - java

I'm trying to in corporate Spring Actuator to my application. I have added the dependency in my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
But I get a 404 when trying to access the /health endpoint. After looking online, I've read that I need to also have the spring-boot-starter-web dependency in my POM. I was under the assumption that I only need the actuator dependency in order to get it working

Yes web is needed if you want to access via HTTP (otherwise only JMX is available).
The documentation for actuator states
"Click Dependencies and select Spring Web and Spring Boot Actuator."

Related

Httpclient and Zipkin Java

My and project team are looking to add Zipkin logging and tracing to our current project. We are working in an microservice environment using Spring Boot (Java 17) and cloud foundery. For the communication between Microservices we are using HttpClient. From what I've gathered from the documentation Zipkin requires an RestTemplate to function. However we don't have time to change this.
We were able to implement Zipkin in every individual project. However, every call generates their own Trace ID. I think we need to configure the HttpClient to work in tandem with Zipkin, however the documentation is not very clear and I have been unable to find anything that explains how to do this.
What can I try on this? I've included the config and dependencies below.
spring:
application:
name: Application_1
zipkin:
baseUrl: http://localhost:9411
sleuth:
sampler:
probability: 1.0
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>3.1.3</version>
</dependency>

How to implement custom endpoint in Spring Boot actuator to health

How would I implement a custom endpoint in SpringBoot to achieve the following:
http://localhost:8080/actuator/health/custom
where "custom" is the endpoint I want to implement that extends health.
That url is used to expose the Health checks for the application, if that is your situation, you only need to include the dependencies for the actuator :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
and you can see the status of the app.
If you need something more specific, only configure the dependency as you can see here .
Also you can override the behavior of the healthchecks but that dependency have a lot of functionality.

How to access secure actuator endpoints without adding spring security

Actuator "sensitive" endpoints secure since 1.5.x version, is it possible to specify user in properties or yml file and access these endpoints without adding spring security into project?
I know that there's property management.security.enabled can be set to false to expose endpoints, but want to keep them secure:)
Yes you can do it.
Just add the below lines in src/main/resources/application.properties file.
management.security.enabled=true
security.user.name=admin
security.user.password=admin1
management.security.roles=SUPERUSER
After this, add below dependency in your application pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Restart your application and try to access any sensetive actuator. You will be prompted to enter username and password. Enter the username and password that you configured in application.priperties.
After entering you will be able to access the sentive actuators.
Hope this helps.
Happy coding

How do I find a list of beans for a given Spring Boot class/package?

I am currently working with Spring Boot with a number of starter packs, such as the web MVC framework, Thymeleaf templates and security.
Each of these packages have a lot of different configuration options. I have mainly been using the source from the Auto-configuration package to figure out which beans need to be wired up and how to do that.
However, Is there any easy way to find a list of expected beans/classes that are needed by a given Spring package?
To view the beans you can you spring boot actuator
To Enable actuator, you can simply add the actuator starter dependency to your project.
For Gradle
compile("'org.springframework.boot:spring-boot-starter-actuator:1.3.1.RELEASE'
")
For Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
You can skip the version if you are using spring boot parent pom
Now you can rebuild your application make get request to http://server:port/beans if local and port is 8080 then
http://localhost:8080/beans
If you want to access using CURL command line tool you can
curl http://localhost:8080/beans
For more information visit
http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

Understanding Spring Boot

I am trying to understand the difference between spring boot and spring boot web. From this boot tutorial the pom contains spring boot as the parent and spring boot web as a dependency like so:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
What are the uses for the 2 different versions? Do you always use them together? This spring boot documentation tells me if the program is production ready to use:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
But if that's the case then why isn't there one for web like so:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-actuator</artifactId>
</dependency>
</dependencies>
There are lots of these different 'starter' poms for spring boot. Each one of them tells maven to bring in the dependencies needed for that particular functionality. So spring-boot-starter-web brings in all the stuff needed for MVC and autoconfigures it with sensible defaults. The real trick to spring boot is it when it autoconfigures things it uses a whole of #ConditionalOnClass or other such annotations that look at what dependencies are on the classpath and provides configuration for those dependencies. What this means is when you have that spring boot actuator starter, when it's doing its autoconfiguration it will look at what other spring boot start poms you have in your pom.xml and it will configure different endpoints for the actuator so you can see the various metrics the actuator provides for that particular module.
Spring Boot is a framework, spring-boot-starter-web is one of the packages that comes with it, a jar file.
Just like JDK is a library, and util is one of the packages included in the JDK.
From: https://docs.spring.io
Spring Boot provides a number of “Starters” that auto-configures your application adds jars to your classpath. The spring-boot-starter-parent is a core starter that provides useful maven defaults. It also provides a dependency-management section so that if you import additional starters then you can omit version tags for “blessed” dependencies. Therefore you should only need to specify the Spring Boot version number on this dependency
Starters are a set of convenient dependency descriptors that you can include in your application considering each starter covers a specific area.The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.For example, if you want to get started using Spring and JPA for database access, just include the "spring-boot-starter-data-jpa" dependency in your project, and you are good to go.
Spring Boot
spring-boot-starter-web is starter for building web, including RESTful, applications using Spring MVC. It uses Tomcat as the default embedded container
Spring Boot has different groups of starters like
1-Spring Boot application starters: spring-boot-starter-web , spring-boot-starter-jdbc , spring-boot-starter-jpa etc
2-Spring Boot production starters : spring-boot-starter-actuator which provides production ready features to help you monitor and manage your application
3-Spring Boot technical starters: spring-boot-starter-jetty , spring-boot-starter-tomcat these starters can be used to exclude or swap specific technical facets
1.spring-boot-starter-parent deals with the auto start of main method and run methods so on..
2.and spring-boot-starter-web deals with the spring MVC things like controller, autowired so on.. Hope this helps..
Spring boot is a very cool tool of Spring Source. In many conference the team talck about of Spring Boot as one of the three DSR(Domain Specific Run-Time) of Pivotal.... Spring Boot, Spring XD and Grails(now Pivotal didn't support Groovy & Grails).
That said compare Spring boot stand alone and Spring boot web enviroment may be a cool conversation. First of all Spring boot give you many production ready istruments such as actuator(avaiable whit sprin boot in web enviroment), spring remote shall and so on. The main difference is the same of had a spring stand-alone context or a Spring web context. Of course some of the potentiality that yon could have are avaiable in a web context, Actuator is an example. but the main difference is in what kind of application you need, web or stand-alone.

Categories