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

1 微服務監控流程

在這裏插入圖片描述

圖1 微服監控流程

2 Eureka服務端

2.1 pox.xml

<parent>
	  <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.2.8.RELEASE</version>
      <relativePath/>
</parent>
<dependencies>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
	<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-dependencies</artifactId>
	<version>Hoxton.SR5</version>
	<type>pom</type>
	<scope>import</scope>
	</dependency>
	</dependencies>
</dependencyManagement>

2.2 application.yml

eureka:
  client: 
    fetch-registry: false # 從註冊中心獲取服務(註冊中心server爲自身,不需要開啓)
    register-with-eureka: false # 註冊到註冊中心(註冊中心server自身,無需註冊到註冊中心)
    service-url: 
      defaultZone: http://localhost:8090/eureka/eureka # 與Eureka server(註冊中心)交互地址,查詢Eureka服務的地址
  instance: 
    hostname: localhost # 指定主機地址

2.3 啓動類

package com.personal.microspersonaleureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SpringBootApplication
@EnableEurekaServer
public class MicrospersonalEurekaApplication {
	static Logger logger = LoggerFactory.getLogger(MicrospersonalEurekaApplication.class);
	public static void main(String[] args) {
		SpringApplication.run(MicrospersonalEurekaApplication.class, args);
		logger.info("註冊中心Eureka啓動");
	}
}

3 Admin服務端

3.1 pom.xml

	<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.2.3</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>
	</dependencies>

3.2 application.yml

Admin自身監控自身,由於Admin配置了Security,因此在元數據metadata-map中添加賬密.
Admin監控的所有服務來自與Eureka註冊中心,而Admin自身也會註冊到Eureka,所以,會出現自身監控自身的情況.

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8090/eureka/eureka
  instance:
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
序號 參數 描述
1 端口 Admin服務端端口
2 username 登錄Admin服務端的用戶名
3 password 登錄Admin服務端的密碼

3.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"
                );
    }
}

3.4 啓動文件

package com.personal.microspersonaladmin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
// @EnableEurekaClient
@EnableAdminServer
// @EnableDiscoveryClient
public class MicrospersonalAdminApplication {
	static Logger logger = LoggerFactory.getLogger(MicrospersonalAdminApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(MicrospersonalAdminApplication.class, args);
		logger.info("ADMIN啓動");
	}
}

4 應用

4.1 啓動順序

Eureka註冊中心->Admin服務端->客戶端(Springboot單體服務)

4.2 登錄Eureka註冊中心

  • 地址
http://localhost:8090/eureka/
  • 管理的服務

在這裏插入圖片描述

圖4.1 註冊中心服務

4.3 登錄Admin服務端

  • 地址
 http://localhost:8008
  • 登錄界面

在這裏插入圖片描述

圖4.2 Admin登錄

4.3 服務列表

在這裏插入圖片描述

圖4.3 服務列表

4.4 服務面板牆

在這裏插入圖片描述

圖4.4 服務面板牆

4.5 服務日誌信息

在這裏插入圖片描述

圖4.5 服務日誌信息

4.6 URI列表

在這裏插入圖片描述

圖4.6![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200528190158501.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpbl8xMDE=,size_16,color_FFFFFF,t_70#pic_center)![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200528190158501.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpbl8xMDE=,size_16,color_FFFFFF,t_70#pic_center) 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
[5]https://www.cnblogs.com/shihaiming/p/8488939.html
[6]https://www.cnblogs.com/heyongboke/p/9806396.html
[7]https://blog.csdn.net/A_Story_Donkey/article/details/81483781
[8]https://blog.csdn.net/Xin_101/article/details/106299514
[9]https://segmentfault.com/a/1190000017816452
[10]https://www.jianshu.com/p/4891de932764
[11]https://www.jianshu.com/p/f6db3117864f

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