EE颠覆者第11章应用监控端点ssh

简介

应用监控 和 管理的功能

通过 http JMX SSH 协议操作

actuator EndPoint的列表

autoconfig 所有自动配置

beans bean的信息

configprops 所有的配置属性

dump 当前应用线程的状态信息

env 当前环境信息

health 当前应用的健康状态

info 当前应用的信息

metrics 各项指标信息

mappings 所有的映射的路径

shutdown 关闭当前应用

trace 显示追踪信息

pom

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.hateoas</groupId>
			<artifactId>spring-hateoas</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
http://localhost:8080/actuator
{
    "links": [
        {
            "rel": "self",
            "href": "http://localhost:8080/actuator"
        },
        {
            "rel": "mappings",
            "href": "http://localhost:8080/mappings" 映射信息
        },
        {
            "rel": "shutdown",
            "href": "http://localhost:8080/shutdown" 关闭(POST提交)
        },
        {
            "rel": "autoconfig",
            "href": "http://localhost:8080/autoconfig" 自动配置
        },
        {
            "rel": "info",
            "href": "http://localhost:8080/info"  当前应用信息
        },
        {
            "rel": "env",
            "href": "http://localhost:8080/env"
        },
        {
            "rel": "trace",
            "href": "http://localhost:8080/trace" 显示追踪信息
        },
        {
            "rel": "beans",
            "href": "http://localhost:8080/beans" 所有的bean
        },
        {
            "rel": "metrics",
            "href": "http://localhost:8080/metrics"  各项指标信息
        },
        {
            "rel": "dump",
            "href": "http://localhost:8080/dump" 线程的状态
        },
        {
            "rel": "status",
            "href": "http://localhost:8080/status"
        },
        {
            "rel": "health",
            "href": "http://localhost:8080/health" 健康状态
        },
        {
            "rel": "configprops",
            "href": "http://localhost:8080/configprops" 所有的配置属性
        }
    ]
}


endpoints.shutdown.enabled=true  开启访问关闭,post提交

定制端点

endpoints.beans.id=mybeans #变成了 http://localhost:8080/mybeans
endpoints.beans.enabled=false  #关闭上面的bean端点

endpoints.enabled=false #关闭所有的端点
endpoints.beans.enabled=true #开户所需的端点

management.context-path=/manage  http://localhost:8080/manage/beans
management.port=8081  #改变端口的访问地址
management.port=-1 #关闭http端点

自定义端点

@Service
public class StatusService {
	private String status;
}

//可以通过配置文件进行配置

@ConfigurationProperties(prefix = "endpoints.status", ignoreUnknownFields = false) //1
public class StatusEndPoint extends AbstractEndpoint<String> implements ApplicationContextAware{//2 继承Endpoint接口的抽象实现。实现 意识
	
	ApplicationContext context;

	public StatusEndPoint() {
		super("status");
	}

	@Override
	public String invoke() { //3 重写
		StatusService statusService = context.getBean(StatusService.class);
		
		return "The Current Status  is :"+statusService.getStatus();
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		this.context = arg0;
		
	}
}
@SpringBootApplication
@RestController
public class DemoApplication {
	@Autowired
	StatusService statusService;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    
    @Bean
    public Endpoint<String> status() {
    	Endpoint<String> status =  new StatusEndPoint();
    	return status;
    }
    @RequestMapping("/change")
    public String changeStatus(String status){
    	statusService.setStatus(status);
    	return "OK";
    }
}
http://localhost:8080/change?status=running

http://localhost:8080/status 返回:The Current Status is :running

自定义HealthIndicator

Health信息都是从 ApplicationContext中所有的 HealthIndicator的Bean中收集的。

Spring中内置了一些 HealthIndicator

检测 ElasticSearch集群是否运行

检测JMS消息代理是否运行

邮件服务器

MongoDB

RabbitMQ

Redis

@Component
public class StatusHealth implements HealthIndicator {//1
	@Autowired
	StatusService statusService;

	@Override
	public Health health() {
		String status = statusService.getStatus();
		if(status == null||!status.equals("running")){
			return Health.down().withDetail("Error", "Not Running").build(); //2
		} //status为null 或 status 不为 running 时构造失败。其他为运行成功
		return Health.up().build(); //3
	}

}
http://localhost:8080/health
{
    "status": "UP",
    "statusHealth": {
        "status": "UP"
    },
    "diskSpace": {
        "status": "UP",
        "total": 302643146752,
        "free": 217826213888,
        "threshold": 10485760
    },
    "links": [
        {
            "rel": "self",
            "href": "http://localhost:8080/health"
        }
    ]
}


当改成其他的时候,变成down
{
    "status": "DOWN",
    "statusHealth": {
        "status": "DOWN",
        "Error": "Not Running"
    }
}

JMX

是对应用进行监控和管理,装了java即可用。

控制台输入。 jconsole

在页面选择 MBean ,找到 org.springframework.boot ,Endpoint

healthEndpoint 和 status (上面自定义的)

ssh

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-remote-shell</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
shell.auth.simple.user.name=wyf #默认用户名为user,密码会默认打印
shell.auth.simple.user.password=wyf
端口默认为2000

help 获得命令的列表

metrics

NAME                                                      VALUE
-----------------------------------------------------------------------------------------
mem                                                       565248
mem.free                                                  322311
processors                                                12

endpoint list

endpoint invoke health #执行某一端点

endpoint invoke health
{status=UP, diskSpace={status=UP, total=302643146752, free=217814908928, threshold=10485760}}

使用 Groovy语言来编制命令,运行于 JVM的动态语言。Spring Boot也可用 Groovy语言开发。

自定义指令

commands/hello.groovy

package commands
import org.crsh.cli.Command
import org.crsh.cli.Usage
import org.crsh.command.InvocationContext
class hello {
	@Usage("Say Hello") #命令的用途
	@Command #当前是一个CRaSH命令
	def main(InvocationContext context) {

		def bootVersion = context.attributes['spring.boot.version']; 1.3.0.M4
		def springVersion = context.attributes['spring.version'] 4.2.0.RELEASE

		return "Hello,your Spring Boot version is "+bootVersion +",your Spring Framework version is "+springVersion

	}
}

//2020 04 19 看 
2.2.6 spring boot 版本
5.2.5 spring framework版本

Hoxton SR3  cloud 版本 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值