您当前的位置: 首页 >  spring boot

墨家巨子@俏如来

暂无认证

  • 3浏览

    0关注

    188博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

十七.SpringCloud极简入门-Spring Boot Admin服务监控

墨家巨子@俏如来 发布时间:2020-07-23 20:02:32 ,浏览量:3

一.什么是SpringBoot Admin

我们知道在微服务架构的应用中服务众多,对于各个微服务的监控是微服务架构应用中不可缺少的一个环节,比如:服务的内存使用情况,JVM信息等等都需要做数据监控以便在资源使用告急时方便运维人员做出相应的配置调整。

SpringBoot Admin 它是在 Spring Boot Actuator 的基础上用于监控 Spring Boot 的应用,且提供简洁的可视化 WEB UI界面。Spring Boot Admin 提供了很多功能,可以监控如:spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level。

二.集成

SpringBoot Admin 包括服务端可客户端组成 ,我们需要在Eureka环境中来使用SpringBootAdmin ,我们需要使用到三个应用: EurekaSever 注册中心,SpringBootAdmin 服务端 ,eureka-client 客户端 我们这里基于SpringBoot Admin 2.0 来构建 。对于EurekaServer注册中心的搭建之前已经介绍过了这里不在多说,这里我已经搭建好的EurekaServer端口为 1111

1.Spring Boot Admin 服务端搭建

搭建SpringBoot应用 (SpringBootAdmin ),pom如下

  
		UTF-8
		UTF-8
		1.8
		Finchley.SR1
	

	
			
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		
	
		
			de.codecentric
			spring-boot-admin-starter-server
			2.0.2
		
		
		
		de.codecentric
			spring-boot-admin-server-ui
			2.0.2
		
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
	   
		
			org.springframework.boot
			spring-boot-starter-security
		
	   
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

因为同时 SpringBootAdmin 也要作为Eurake的客户端,所以我这里引入了Eurake-Client的依赖 ,还引入了 security 安全依赖, actuator监控相关依赖,和spring-boot-admin 相关依赖,包括spring-boot-admin-server-ui 界面相关依赖

2.主程序配置

主程序配置类开启 SpringBootAdmin功能,同时也开启EurekaClient功能 如下:

//EnableAdminServer 开启 springboot admin
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class SpringbootadminApplication {

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

	@Profile("insecure")
	@Configuration
	public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			http.authorizeRequests().anyRequest().permitAll()//
					.and().csrf().disable();
		}
	}

	@Profile("secure")
	@Configuration
	public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

		private final String adminContextPath;

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

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			// @formatter:off
			SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
			successHandler.setTargetUrlParameter("redirectTo");

			http.authorizeRequests()
					.antMatchers(adminContextPath + "/assets/**").permitAll()
					.antMatchers(adminContextPath + "/login").permitAll()
					.anyRequest().authenticated()
					.and()
					.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler)
					.and()
					.logout().logoutUrl(adminContextPath + "/logout").and()
					.httpBasic().and()
					.csrf().disable();
			// @formatter:on
		}
	}
}

SecurityPermitAllConfig和SecuritySecureConfig的配置是 Spring Boot Admin 官方给的配置,是对 url 进行安全认证等配置,照着配置即可,官方配置

3.添加配置文件 application.yml 如下
  spring:
  application:
    name: admin-server
  profiles:
    active:
      - secure
# tag::configuration-eureka[]
eureka:   #
  instance:
    health-check-url-path: /actuator/health   #应用实例的相对运行状况检查URL路径默认为 /info/health ,2.0后actuator的地址发生了变化
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/  #注册到 EurekaServer

# 2.0开始,actuator默认不开放,所以要设置为开放
management:
  endpoints:
    web:
      exposure:
        include: "*"  #暴露所有的端点我们可以看到更多应用相关信息
  endpoint:
    health:
      show-details: ALWAYS  #health endpoint是否必须显示全部细节
server:
  port: 2222
# end::configuration-eureka[]

---
spring:
  profiles: insecure

---
# admin登录的用户名和密码
spring:
  profiles: secure
  security:
    user:
      name: "user"
      password: "password"

# 注册给eureka的时候告诉eureka自己的密码
eureka:
  instance:
    metadata-map:
      "user.name": ${spring.security.user.name}         #These two are needed so that the server
      "user.password": ${spring.security.user.password} #can access the protected client endpoints

上面官方配置提供,修改时根据情况修改 server.port 和 eureka . client. serviceUrl .defaultZone ,和serviceUrl.usesr.name 、password即可 ,到这里SpringBootAdmin应用构建完成

4. 构建EurekaClient应用

构建EurekaClient应用(也是SpringBoot Admin的客户端),pom如下


		org.springframework.boot
		spring-boot-starter-parent
		2.0.5.RELEASE
		
	

	
		UTF-8
		UTF-8
		1.8
		Finchley.SR1
	

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			de.codecentric
			spring-boot-admin-starter-client
			2.0.0
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	
5.应用主程序配置类如下
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}
}
6.添加EurekaClient配置文件 application.yml如下
eureka:
  instance:
    health-check-url-path: /actuator/health   #应用实例的相对运行状况检查URL路径默认为 /info/health
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/  #注册中心地址

management:
  endpoints:
    web:
      exposure:
        include: "*"   #暴露所有的端点我们可以看到更多的服务实例相关信息,
  endpoint:
    health:
      show-details: ALWAYS   #health endpoint是否必须显示全部细节

server:
  port: 3333

spring:
  application:
    name: provider
  boot:
    admin:
      client:
        url: "http://localhost:2222"   #指定SpringBootAdmin服务端地址
        password: password
        username: user
        instance:
          prefer-ip: true

在客户端应用的配置中,我们指定了EurekaServer注册中心的地址,和配置了SpringBootAdmin服务端地址并设置用户名和密码,和应用监控的端点的暴露规则

7.测试,

依次启动 EurekaServer ,SpringBootAdmin,EurekaClient客户端 ,访问 SpringBootAdmin 的地址 http://localhost:2222可以看到登录界面: image.png 使用配置的用户和密码登录进去:image.png 可以看到SpringBootAdmin和EurekaClient这两个应用的服务已经被挂载上去,点击 PROVIDER进去 可以看到很多应用监控相关信息 image.png image.png

关注
打赏
1651329177
查看更多评论
立即登录/注册

微信扫码登录

0.0530s