Springboot2模塊系列:SpringbootAdmin(服務監控)

1 Admin服務端

1.0 pom.xml

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.14.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.personal</groupId>
	<artifactId>microspersonal-admin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>microspersonal-admin</name>
	<description>Demo project for Micros Service Admin</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

1.2 application.xml

server: 
  port: 8008
spring: 
  application: 
    name: spring-admin 
  security: 
    user: 
      name: admin 
      password: admin
序號 參數 描述
1 端口 Admin服務端端口
2 username 登錄Admin服務端的用戶名
3 password 登錄Admin服務端的密碼

1.3 Security配置

配置Admin服務端登錄權限.

package com.personal.microspersonaladmin.config;



import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@EnableWebSecurity
public class SpringAdminConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

    public SpringAdminConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                .antMatchers(
                        adminContextPath + "/assets/**",
                        adminContextPath + "/login"
                ).permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                .httpBasic().and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        "/instances",
                        "/actuator/**",
                        adminContextPath + "/logout"
                );
    }
}

1.4 啓動文件

package com.personal.microspersonaladmin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import de.codecentric.boot.admin.server.config.EnableAdminServer;

@SpringBootApplication
@EnableAdminServer
public class MicrospersonalAdminApplication {

	public static void main(String[] args) {
		SpringApplication.run(MicrospersonalAdminApplication.class, args);
	}

}

2 Admin客戶端

2.1 pom.xml

<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company.web</groupId>
  <artifactId>base-framework</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>base-framework Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!-- <version>1.5.10.RELEASE</version> -->
    <!-- <version>2.0.4.RELEASE</version> -->
    <version>2.3.0.RELEASE</version>
    <relativePath></relativePath>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>

    <dependency>
      <groupId>de.codecentric</groupId>
      <artifactId>spring-boot-admin-starter-client</artifactId>
      <version>2.1.0</version>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>base-framework</finalName>
    <defaultGoal>compile</defaultGoal>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
          <mainClass>com.company.web.InitiApplication</mainClass>
          <includeSystemScope>true</includeSystemScope>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.*</include>
        </includes>
      </resource>
    </resources>
  </build>
</project>

2.2 application.xml

spring:
  profiles:
    active: dev
  boot: 
    admin: 
      client: 
        url: "http://localhost:8008"
        username: admin 
        password: admin

management: 
  endpoints: 
    web: 
      exposure: 
        include: "*"
序號 參數 描述
1 url Admin服務端地址
2 username 登錄Admin服務端的用戶名
3 password 登錄Admin服務端的密碼

2.3 啓動文件

package com.company.web;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
// import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2 
@EnableCaching 
public class InitiApplication {
    private static Logger logger = LoggerFactory.getLogger(InitiApplication.class);
    
    public static void main(String[] args){
        SpringApplication.run(InitiApplication.class, args);
        logger.info("web服務啓動");
    }
}

3 應用

3.1 啓動順序

Admin服務端->Admin客戶端(Springboot單體服務)

3.2 登錄Admin服務端

http://localhost:8008
在這裏插入圖片描述

Admin登錄

3.2 服務列表

在這裏插入圖片描述

服務列表

3.3 服務面板牆

在這裏插入圖片描述

服務面板牆

3.4 服務詳細信息

在這裏插入圖片描述

服務詳細信息

3.5 URI列表

在這裏插入圖片描述

URI列表

參考文獻
[1]https://www.jianshu.com/p/25d5a85ce8dd
[2]https://www.jianshu.com/p/921387db847e
[3]https://www.cnblogs.com/ityouknow/p/8440455.html
[4]https://www.jianshu.com/p/e20a5f42a395

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章