preface
We know that project monitoring is particularly important, but using jConsole and JVisualVM that come with the JDK would be tedious and not very user-friendly. We used the Spring Boot project before, but did not have a good monitoring of the project. In the Spring family, spring-boot-admin can help us to monitor microservices projects.
Spring-boot-admin is a monitoring tool that can beautify and wrap interfaces in spring Boot. It can browse the list for basic information about all monitored Spring-Boot projects, detailed Health information, memory information, JVM information, garbage collection information, various configuration information (such as data source, cache list, hit ratio), and directly modify logger levels.
Spring-boot-admin consists of a server and a client. The server side is a separate microservice used to check the operation of monitored projects, while the client side is one of our microservice projects. So in order for our project to be monitored by the server, we need to register our service with the server.
All right, let’s try it out.
admin-server
Let’s start by setting up the spring-boot-admin server, which is a separate project. So let’s create a new SpringBoot project. Once we’ve created it, let’s make some changes.
pom.xml
In the POM file, we introduce the following dependencies
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.2. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.quellanan</groupId> <artifactId>springbootadmin</artifactId> <version>0.0.1-SNAPSHOT</version> <name> Springbootadmin </name> <description> Springbootadmin project for Spring Boot < / description > < properties > < Java version > 1.8 < / Java version > < spring - the boot - admin. Version > 2.2.1 < / spring - the boot - admin. Version > < / properties > < dependencies > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-dependencies</artifactId> <version>${spring-boot-admin.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code
Above is my entire POM file, and you can see that I introduced Web, admin-starter-Server, and Security. If you consider anything else, you can simply reference admin-starter-server to achieve the effect.
Start the class
Add the @enabLeadMinServer annotation to our startup class. If not, the project will start normally but see nothing. The @enableadMinServer annotation is used to start monitoring.
@SpringBootApplication @EnableAdminServer public class SpringbootadminApplication { public static void main(String[] args) { SpringApplication.run(SpringbootadminApplication.class, args); }}Copy the code
Configure security
Once this is configured, we can start the project, but we won’t start it here because we learned about spring-boot-Security in the last video. So let’s use it here. Having introduced Security earlier, let’s add configuration to the Application
spring.security.user.name=admin
spring.security.user.password=123456Copy the code
Indicates that only this user can access. In addition we create a SecurityConfig WebSecurityConfigurerAdapter rewrite the configure class hierarchy (HttpSecurity HTTP) method. The code is as follows:
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl("/"); http.authorizeRequests() .antMatchers("/assets/**").permitAll() .antMatchers("/login").permitAll() .anyRequest().authenticated().and() .formLogin().loginPage("/login") .successHandler(successHandler).and() .logout().logoutUrl("/logout").and() .httpBasic().and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers( "/instances", "/actuator/**" ); }}Copy the code
Now let’s start the project and see. Enter after starting the project
http://localhost:8080Copy the code
It jumps to the login screen, goes to the home page and now there’s nothing there.
admin-client
Now let’s configure the client. We can either find a Springboot project or create a new one ourselves.
pom.xml
Add admin-client dependencies to the POM file. Note that the version needs to be the same as the server version.
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> The < version > 2.2.1 < / version > < / dependency >Copy the code
application.properties
Then we add the following configuration to the application.properties file.
spring.boot.admin.client.url=http://localhost:8080
management.endpoints.web.exposure.include=*
spring.application.name=sdwlzlapp-file
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456Copy the code
Spring. The boot. Admin. Client. The url path to point to our server program interface. Management. Endpoints. Web. Exposure. Include all ports will be exposed, can be monitored. Spring.application. name indicates the name of the project displayed on spring-boot-admin. Spring. The boot. Admin. Client. The username and password is set the user name and password, here it is important to note that if the admin – no integrated security server, don’t need to configure the user name and password can also be registered in, This can be detected on the server. However, if the admin-server integrates security, ensure that the user name configured on the client is the same as that configured on the server.
test
With this configured, you can register your project with admin-server. Let’s start the project.
Now there is a question, if our project itself integrated security framework, such as Security, can not access the interface without logging in, then how can such a project be monitored by admin-server? For example, we registered the security demo in the last section. Although it was monitored, it was in a failed state.As can be seen, it is not difficult to find the problem, that is, the monitoring interface is also blocked by the project itself, so it is a failure state, then how to modify it, in fact, it is easy to deal with, let go of these several interfaces. We add configure(HttpSecurity HTTP) to the project’s SecurityConfig classThe code is as follows:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/hello").permitAll()
.antMatchers( "/actuator/**").permitAll()
.antMatchers( "/instances").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
//.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}Copy the code
So we can restart the project and see that we can monitor successfully.
One day
So far, we have provided a general demonstration of spring-boot-admin’s capabilities.
Code upload github:https://github.com/QuellanAn/springbootadmin
Follow-up fuel ♡
Welcome to pay attention to personal public account “Programmers love yogurt”
Share all kinds of learning materials, including Java, Linux, big data, etc. Information includes video documents and source code, and share myself and deliver quality technical blog posts.
Be sure to follow and share if you like ❤