diff --git a/ReadMe.md b/ReadMe.md index 6aabce3..84572da 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -30,6 +30,8 @@

+

+ # Java基础 ## 基础知识 @@ -394,6 +396,25 @@ todo # 消æ�¯é˜Ÿåˆ— ## Kafka +* [消æ�¯é˜Ÿåˆ—kafka详解:Kafka快速上手(Java版)](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:Kafka快速上手(Java版).md) +* [消æ�¯é˜Ÿåˆ—kafka详解:Kafka一æ�¡æ¶ˆæ�¯å­˜åˆ°broker的过程](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:Kafka一æ�¡æ¶ˆæ�¯å­˜åˆ°broker的过程.md) +* [消æ�¯é˜Ÿåˆ—kafka详解:消æ�¯é˜Ÿåˆ—kafka详解:Kafka介ç»�](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:Kafka介ç»�.md) +* [消æ�¯é˜Ÿåˆ—kafka详解:Kafka原ç�†åˆ†æž�总结篇](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:Kafka原ç�†åˆ†æž�总结篇.md) +* [消æ�¯é˜Ÿåˆ—kafka详解:Kafka常è§�命令å�Šé…�置总结](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:Kafka常è§�命令å�Šé…�置总结.md) +* [消æ�¯é˜Ÿåˆ—kafka详解:Kafka架构介ç»�](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:Kafka架构介ç»�.md) +* [消æ�¯é˜Ÿåˆ—kafka详解:Kafka的集群工作原ç�†](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:Kafka的集群工作原ç�†.md) +* [消æ�¯é˜Ÿåˆ—kafka详解:Kafkaé‡�è¦�知识点+é�¢è¯•题大全](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:Kafkaé‡�è¦�知识点+é�¢è¯•题大全.md) +* [消æ�¯é˜Ÿåˆ—kafka详解:如何实现延迟队列](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:如何实现延迟队列.md) +* [消æ�¯é˜Ÿåˆ—kafka详解:如何实现死信队列](docs/mq/kafka/消æ�¯é˜Ÿåˆ—kafka详解:如何实现死信队列.md) + +## RocketMQ +* [RocketMQ系列:事务消æ�¯ï¼ˆæœ€ç»ˆä¸€è‡´æ€§ï¼‰](docs/mq/RocketMQ/RocketMQ系列:事务消æ�¯ï¼ˆæœ€ç»ˆä¸€è‡´æ€§ï¼‰.md) +* [RocketMQ系列:基本概念](docs/mq/RocketMQ/RocketMQ系列:基本概念.md) +* [RocketMQ系列:广播与延迟消æ�¯](docs/mq/RocketMQ/RocketMQ系列:广播与延迟消æ�¯.md) +* [RocketMQ系列:批é‡�å�‘é€�与过滤](docs/mq/RocketMQ/RocketMQ系列:批é‡�å�‘é€�与过滤.md) +* [RocketMQ系列:消æ�¯çš„生产与消费](docs/mq/RocketMQ/RocketMQ系列:消æ�¯çš„生产与消费.md) +* [RocketMQ系列:环境æ�­å»º](docs/mq/RocketMQ/RocketMQ系列:环境æ�­å»º.md) +* [RocketMQ系列:顺åº�消费](docs/mq/RocketMQ/RocketMQ系列:顺åº�消费.md) # 大å�Žç«¯ * [å�Žç«¯æŠ€æœ¯æ�‚谈开篇:云计算,大数æ�®ä¸ŽAI的故事](docs/backend/å�Žç«¯æŠ€æœ¯æ�‚谈开篇:云计算,大数æ�®ä¸ŽAI的故事.md) diff --git a/docs/monitor/Spring Actuator.md b/docs/monitor/Spring Actuator.md new file mode 100644 index 0000000..a1ee5ef --- /dev/null +++ b/docs/monitor/Spring Actuator.md @@ -0,0 +1,578 @@ +## ǰÑÔ + +ÔÚ΢·þÎñϵͳ¼Ü¹¹ÖУ¬·þÎñµÄ¼à¿ØÊDZز»¿ÉÉٵġ£Ä¿Ç°´ó¶àÊý΢·þÎñÓ¦ÓÃÓÖÊÇ»ùÓÚSpring CloudϵÁУ¬Ò²¿ÉÒÔ˵ÊÇ»ùÓÚSpring BootϵÁеġ£´ËʱʹÓÃSpring Boot ActuatorÀ´½øÐÐ΢·þÎñµÄ¼à¿Ø£¬²»½ö¹¦ÄÜÈ«Ãæ£¬¶øÇҷdz£·½±ã¡£ + +ÔÚÉÏÆªÎÄÕ¡¶[Spring Boot Actuator¼¯³É£¬ÄѵÄÊÇÁé»îÔËÓã¡](https://link.juejin.cn?target=https%3A%2F%2Fmp.weixin.qq.com%2Fs%2FBaNQWygQb8UXxktrXetOcw "https://mp.weixin.qq.com/s/BaNQWygQb8UXxktrXetOcw")¡·ÖÐÎÒÃÇÒѾ­½éÉÜÁËÈçºÎ½«Actuator¼¯³Éµ½Spring BootÏîÄ¿ÖУ¬²¢ÇÒ½éÉÜÁËÈçºÎ×Ô¶¨ÒåEndpoint£¨¶Ëµã£©¡£ÓÐÅóÓÑÁôÑÔ˵²»¹»ÉîÈ룬ÄÇô£¬±¾ÆªÎÄÕÂÄØ£¬ÎÒÃǽ«½éÉÜActuatorÔ­Éú¶ËµãµÄ¹¦Äܼ°»ù±¾Ê¹Óó¡¾°¡£ + +## Endpoints ½éÉÜ + +ActuatorÖÐËùνµÄ Endpoints £¨·­ÒëΪ¶Ëµã£©ÌṩÁËÍⲿÀ´ÓëÓ¦ÓóÌÐò½øÐзÃÎʺͽ»»¥µÄ¹¦ÄÜ¡£ ±ÈÈç˵/health¶ËµãÌṩÁËÓ¦Óý¡¿µÇé¿öµÄÐÅÏ¢£¬metrics ¶ËµãÌṩÁËÓ¦ÓóÌÐòµÄÖ¸±ê£¨JVM ÄÚ´æÊ¹Óá¢ÏµÍ³CPUʹÓõȣ©ÐÅÏ¢¡£ + +ActuatorÔ­ÉúµÄ¶Ëµã¿É·ÖΪÈý´óÀࣺ + +* Ó¦ÓÃÅäÖÃÀࣺ»ñȡӦÓóÌÐòÖмÓÔØµÄÓ¦ÓÃÅäÖᢻ·¾³±äÁ¿¡¢×Ô¶¯»¯ÅäÖñ¨¸æµÈÓëSpring BootÓ¦ÓÃÃÜÇÐÏà¹ØµÄÅäÖÃÀàÐÅÏ¢¡£ +* ¶ÈÁ¿Ö¸±êÀࣺ»ñȡӦÓóÌÐòÔËÐйý³ÌÖÐÓÃÓÚ¼à¿ØµÄ¶ÈÁ¿Ö¸±ê£¬±ÈÈ磺ÄÚ´æÐÅÏ¢¡¢Ï̳߳ØÐÅÏ¢¡¢HTTPÇëÇóͳ¼ÆµÈ¡£ +* ²Ù×÷¿ØÖÆÀࣺÌṩÁ˶ÔÓ¦ÓõĹرյȲÙ×÷À๦ÄÜ¡£ + +²»Í¬°æ±¾µÄActuatorÌṩµÄÔ­Éú¶ËµãÓÐËù³öÈ룬ÔÚʹÓõĹý³ÌÖÐ×îºÃÒÔËùʹÓð汾µÄ¹Ù·½ÎĵµÎª×¼¡£Í¬Ê±£¬Ã¿¸öÔ­ÉúµÄ¶Ëµã¶¼¿ÉÒÔͨ¹ýÅäÖÃÀ´µ¥¶ÀµÄ½ûÓûòÆôÓᣠ+ +¶øÔÚActuator 2.x ÖÐĬÈ϶˵ãÔö¼ÓÁË/actuatorǰ׺£¬Í¬Ê±Ä¬ÈÏÖ»±©Â¶µÄÁ½¸ö¶ËµãΪ/actuator/healthºÍ /actuator/info¡£¹ØÓڶ˵㱩¶µÄÅäÖ㬿ɲο¼Ç°Ò»ÆªÎÄÕ¡£ÏÂÃæ»ùÓÚSpring Boot 2.2.2.RELEASE°æ±¾À´Öص㽲½âÿ¸ö¶ËµãµÄ¹¦ÄܺÍÓ¦Óó¡¾°¡£ + +## actuator¶Ëµã + +Actuator 2.xÐÂÔöµÄĬÈ϶˵㣬ÓÃÓÚչʾĿǰӦÓÃÖб©Â¶³öÀ´µÄ¶Ëµã»ã×Ü£¬Äã¿ÉÒÔÀí½âΪ¿ÉÓö˵ãµÄĿ¼¡£ + +·ÃÎÊURL£º[http://localhost:8080/actuator](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator "http://localhost:8080/actuator") £¬¶ÔӦչʾ½á¹ûÈçÏÂͼ£º + +![image-20230530233537559](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233537559.png) + +ÉÏÊöֻչʾÁËÒ»²¿·ÖµÄ¶Ëµã£¬·µ»Ø½á¹ûΪ£¬ÕâÀï²ÉÓÃÁËä¯ÀÀÆ÷µÄ²å¼þ-Handler½øÐÐÁ˸ñʽÃÀ»¯¡£Í¨¹ýactuator¿ÉÒÔÖ±¹ÛµÄ¿´³öĿǰ¿ª·ÅÁËÄÄЩ¶Ëµã£¬ÒÔ¼°ÕâЩ¶ËµãµÄÃû³ÆºÍÇëÇó·¾¶¡£ + +ÏÂÃæÎÒÃǾͰ´ÕÕÏÔʾactuator¶ËµãչʾµÄÁбíÖðÒ»½éÉÜ¡£ + +## auditevents¶Ëµã + +auditevents¶ËµãÓÃÓÚÏÔʾӦÓñ©Â¶µÄÉó¼ÆÊ¼þ (±ÈÈçÈÏÖ¤½øÈë¡¢¶©µ¥Ê§°Ü)£¬¼´Ê¹ÎÒÃÇ´ò¿ªÁËËùÓж˵㣬ĬÈÏÇé¿öÏÂÒ²ÊÇ¿´²»µ½Õâ¸ö¶ËµãµÄ¡£ÒòΪʹÓÃËüµÄǰÌáÊÇÐèÒªÔÚSpringÈÝÆ÷ÖдæÔÚÒ»¸öÀàÐÍΪAuditEventRepositoryµÄBeanµÄ¡£ + +²é¿´ÁËÍøÂçÉÏ´ó¶àÊý½Ì³Ì£¬»ù±¾É϶¼ÊǽéÉÜÁËauditevents¶Ëµã¹¦ÄÜ£¬¶øÎ´Õ¹Ê¾¾ßÌåʵÀý¡£±ÊÕß¾­¹ý¶à·½³¢ÊÔ£¬ÖÕÓÚ¸ø´ó¼ÒдÁËÒ»¸ö°¸Àý³öÀ´¡£ + +Ê×ÏÈÉæ¼°µ½È¨ÏÞÈÏÖ¤£¬ÐèÒªÏÈÒýÈëspring-boot-starter-securityÒÀÀµ£º + +```` + + org.springframework.boot + spring-boot-starter-security +` +```` + +µ¥´¿Ìí¼ÓÕâ¸öÒÀÀµ»¹ÊDz»¹»µÄ£¬»¹ÐèÒª¼ÓÈësecurityµÄÅäÖ㬲»È»AuthorizationAuditListener,AuthenticationAuditListener ¼àÌýʲôʼþÄØ? Òò´Ë,ÎÒÃǼÓÈëÈçÏ´úÂ룺 + + +```` +@Configuration +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + + auth.inMemoryAuthentication() + .withUser("admin") + .password(bcryptPasswordEncoder().encode("admin")) + .roles("admin"); + } +```` + +```` + @Bean + public PasswordEncoder bcryptPasswordEncoder() { + return new BCryptPasswordEncoder(); + } +} +```` + +ÕâÀï²ÉÓÃÁËsecurityĬÈϵĵǼ½çÃæºÍȨÏÞ¿ØÖÆ£¬Ò²¾ÍÊÇ˵ËùÓеķÃÎʶ¼ÐèÒª½øÐеǼ¡£¶øµÇ¼µÄÓû§ÃûºÍÃÜÂë¾ùΪadmin¡£ + +ÁíÍâ£¬Ç°ÃæÌáµ½ÐèÒªÓõ½AuditEventRepositoryµÄBean£¬ÕâÀï³õʼ»¯Ò»¸ö¶ÔÓ¦µÄBean£º + + + + +```` +@Configuration +public class AuditEventConfig { + + @Bean + public InMemoryAuditEventRepository repository(){ + return new InMemoryAuditEventRepository(); + } +} +```` + +InMemoryAuditEventRepositoryÊÇAuditEventRepository½Ó¿ÚµÄΨһʵÏÖÀà¡£ + +ÖØÆôÏîÄ¿£¬auditevents¶Ëµã±ã¿ÉÓÃÁË¡£·ÃÎÊ[http://localhost:8080/actuator](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator "http://localhost:8080/actuator") ,´Ëʱ»áÌø×ªµ½SecurityÌṩµÄµÇÂ¼Ò³Ãæ£º + +![image-20230530233604253](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233604253.png) + +ÊäÈë´úÂëÖÐÖ¸¶¨µÄÓû§ÃûºÍÃÜÂ룬µÇ¼³É¹¦£¬Ìø×ªµ½/actuatorÒ³Ãæ£º + +![image-20230530233625068](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233625068.png) + +¿ÉÒÔ¿´µ½auditevents¶ËµãÒѾ­³É¹¦ÏÔʾ³öÀ´ÁË¡£Ð¿ªÒ³Ãæ·ÃÎÊ[http://localhost:8080/actuator/auditevents](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fauditevents "http://localhost:8080/actuator/auditevents") £¬Õ¹Ê¾ÄÚÈÝÈçÏ£º + +![image-20230530233716752](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233716752.png) + +¿ÉÒÔ¿´µ½ÒѾ­¼Ç¼ÏÂÁËȨÏÞÏà¹ØµÄʼþ£¬ÆäÖеÚÒ»´ÎʼþÊÇÎÒÃÇÖ±½Ó·ÃÎÊactuator¶Ëµãʱ£¬ÓÉÓÚ֮ǰΪ×öȨÏÞÈÏÕæ£¬ËùÒÔʼþÀàÐÍΪ"AUTHORIZATION_FAILURE"£¬Ò²¾ÍÊÇÈÏ֤ʧ°Ü¡£´ËÊ±Ìø×ªµ½µÇÂ¼Ò³Ãæ£¬È»ºóÔÚµÇÂ¼Ò³ÃæÊäÈëÓû§ÃûºÍÃÜÂ룬µÇ¼³É¹¦£¬¶ÔÓ¦µÄʼþΪ"AUTHENTICATION_SUCCESS"¡£ + +Ò²¾ÍÊÇ˵auditevents¼Ç¼ÁËÓû§ÈÏÖ¤µÇ¼ϵͳÏà¹ØµÄʼþÐÅÏ¢£¬°üÀ¨Ê±¼ä´Á¡¢ÈÏÖ¤Óû§¡¢Ê¼þÀàÐÍ¡¢·ÃÎʵØÖ·¡¢sessionIdµÈ¡£ + +ʾÀýÔ´ÂëµØÖ·£º[github.com/secbr/sprin¡­](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fsecbr%2Fspringboot-all%2Ftree%2Fmaster%2Fspringboot-actuator-auditevents "https://github.com/secbr/springboot-all/tree/master/springboot-actuator-auditevents") ¡£ + +## beans¶Ëµã + +/beans¶Ëµã»á·µ»ØSpringÈÝÆ÷ÖÐËùÓÐbeanµÄ±ðÃû¡¢ÀàÐÍ¡¢ÊÇ·ñµ¥Àý¡¢ÒÀÀµµÈÐÅÏ¢¡£ + +·ÃÎÊ·¾¶Îª[http://localhost:8080/actuator/beans](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fbeans "http://localhost:8080/actuator/beans") £¬·¶Î§½á¹ûÈçÏ£º + +![image-20230530233748286](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233748286.png) + +Õâ¸ö¶Ëµã»áչʾĿǰSpringÈÝÆ÷Öгõʼ»¯µÄËùÓÐBean£¬ÊÔÏëһϣ¬Èç¹ûÄãÅäÖÃÁËÒ»¸öBean£¬µ«²»È·¶¨ÊÇ·ñ³É¹¦ÊµÀý»¯£¬ÊDz»ÊǾͿÉÒÔͨ¹ýÕâ¸ö¶Ë¿Ú²éѯһÏÂÄØ£¿ + +ÎÒÃÇÔÚÏîÄ¿Öж¨ÒåÒ»¸öTestController£¬²¢×¢ÈëÒ»¸öUserService£º + + +```` +@Controller +public class TestController { + + @Resource + private UserService userService; +} +```` + +ÖØÐÂÆô¶¯²¢·ÃÎʸö˵㣬»á¿´µ½ÈçÏÂÐÅÏ¢£º + +![image-20230530233805161](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233805161.png) + +¿ÉÒÔ¿´µ½TestController±»ÊµÀý»¯ÁË£¬¶øÇÒÒÀÀµÓÚUserService¡£ + +## caches¶Ëµã + +caches¶ËµãÖ÷ÒªÓÃÓÚ±©Â¶Ó¦ÓóÌÐòÖеĻº³å¡£ÕâÀïÒÔSpring BootÌṩµÄCache×é¼þÀ´Õ¹Ê¾Ò»ÏÂʵÀý¡£ + +ÔÚÏîÄ¿Öм¯³Éspring-boot-starter-cache£¬ÒýÈëÒÀÀµ£º + + +```` + + org.springframework.boot + spring-boot-starter-cache + +```` + +È»ºóÔÚÆô¶¯ÀàÉÏÌí¼Ó@EnableCaching£¬¿ªÆô»º´æ¹¦ÄÜ¡£ + +¶¨ÒåÒ»¸öCacheController£¬¶ÔÆä·½·¨queryAllʹÓûº´æ»úÖÆ£º + + +```` +@RestController +public class CacheController { + + @RequestMapping("/queryAll") + @Cacheable(value = "queryAll") + public Map queryAll() { + Map map = new HashMap<>(); + map.put("1", "Tom"); + map.put("2", "Steven"); + return map; + } +} +```` + +ÕâÀïʹÓÃ@Cacheable×¢½âÀ´ÊµÏÖ»º´æ¹¦ÄÜ£¬»º´æµÄkeyΪqueryAll¡£´Ëʱ£¬·ÃÎÊ[http://localhost:8080/actuator/caches](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fcaches "http://localhost:8080/actuator/caches") £¬»áչʾ»º´æµÄ¸ùÄÚÈÝ£¬µ«ÀïÃæ²¢Ã»Óлº´æ¡£ + +·ÃÎÊÒ»ÏÂ[http://localhost:8080/queryAll](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2FqueryAll "http://localhost:8080/queryAll") £¬Ò²¾ÍÊÇ´¥·¢Ò»Ï»º´æÄÚÈݵÄÉú³É¡£´ËʱÔÙ·ÃÎÊÉÏÃæµÄÁ´½Ó£¬±ã¿ÉÒÔ¿´µ½Ó¦ÓóÌÐòÖеĻº´æÐÅÏ¢ÁË£º + +![image-20230530233852486](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233852486.png) + +¿ÉÒÔ¿´µ½£¬·µ»ØµÄÄÚÈݲ»½öչʾÁËÓ¦ÓóÌÐòµÄ»º´æ¹ÜÀíÆ÷£¬Í¬Ê±Ò²Õ¹Ê¾ÁË»º´æµÄKeyºÍ»º´æÊý¾Ý´æ´¢ÀàÐÍÐÅÏ¢¡£ + +## caches-cache¶Ëµã + +caches-cache¶ËµãÊǶÔÉÏÃæcaches¶ËµãµÄÀ©Õ¹£¬caches¶ËµãչʾµÄËùÓеĻº´æÐÅÏ¢¡£Èç¹ûÏëÖ±½Ó¿´³ö¾ßÌåµÄÒ»¸ö»º´æÐÅÏ¢£¬Ôò¿ÉÒÔʹÓÃcaches-cache¶Ëµã¡£ + +·ÃÎʵÄURLΪ£º[http://localhost:8080/actuator/caches/{cache}](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fcaches%2F%257Bcache%257D "http://localhost:8080/actuator/caches/%7Bcache%7D") £¬ÆäÖдóÀ¨ºÅÄÚµÄÖµ¿ÉÒÔÌæ»»Îª»º´æµÄkey¡£ + + + + +`http://localhost:8080/actuator/caches/queryAll` + +½«ÉÏÃæµÄռλ·û»»³ÉqueryAll£¨»º´æµÄkey£©£¬Ö´Ðнá¹ûÈçÏ£º + +![image-20230530233906164](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233906164.png) + +¿ÉÒÔ¿´³ö£¬Ö»²éѯ³ýÁËÖ¸¶¨µÄ»º´æÐÅÏ¢£¬°üÀ¨»º´æ¹ÜÀíÆ÷¡¢»º´æÃû³Æ£¨key£©£¬»º´æµÄ´æ´¢ÀàÐÍ¡£ + +## health¶Ëµã + +health¶ËµãÓÃÀ´¼ì²éÓ¦ÓõÄÔËÐÐ״̬£¬×î¸ßƵʹÓõÄÒ»¸ö¶Ëµã¡£¼ì²éÓ¦ÓÃʵÀýµÄÔËÐÐ״̬ÒÔ¼°Ó¦Óò»¡±½¡¿µ¡°µÄÔ­Òò£¬±ÈÈçÊý¾Ý¿âÁ¬½Ó¡¢´ÅÅ̿ռ䲻¹»µÈ¡£ + +·ÃÎʵØÖ·£º[http://localhost:8080/actuator/health](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fhealth "http://localhost:8080/actuator/health") + +չʾ½á¹û£º + +`{ +"status": "UP" +}` + +ÉÏÃæµÄʵÀý¹ýÓÚ¼òµ¥£¬ÔÚÏîÄ¿ÖаÑÊý¾Ý¿â¸ø¼¯³É½øÈ¥£º + +` + +```` + + org.springframework.boot + spring-boot-starter-jdbc + + + mysql + mysql-connector-java +` +```` + +È»ºóÔÚapplicationÅäÖÃÎļþÖнøÐÐÅäÖ㺠+ + + +``` +spring: + datasource: + url: jdbc:mysql://xxx:3333/xxx?characterEncoding=utf8&serverTimezone=Asia/Shanghai + username: root + password: root + driver-class-name: com.mysql.cj.jdbc.Driver +``` + +ͬʱ£¬ÎÒÃÇÒªÔÚapplicationÅäÖÃÎļþÖÐÅäÖÃÒ»ÏÂmanagement.endpoint.health.show-detailsµÄÖµ¡£¸ÃÊôÐÔÓÐÈý¸ö¿ÉÑ¡Ï + +* never £º²»Õ¹Ê¾ÏêϸÐÅÏ¢£¬up »òÕß down µÄ״̬£¬Ä¬ÈÏÅäÖã» +* when-authorized£ºÏêϸÐÅÏ¢½«»áչʾ¸øÍ¨¹ýÈÏÖ¤µÄÓû§¡£ÊÚȨµÄ½ÇÉ«¿ÉÒÔͨ¹ýmanagement.endpoint.health.roles ÅäÖã» +* always£º¶ÔËùÓÐÓû§±©Â¶ÏêϸÐÅÏ¢¡£ + +ĬÈÏÖµÊÇnever£¬ËùÒÔÎÒÃÇÖ±½Ó·ÃÎÊ¿´µ½µÄÖ»ÓÐUP»òDOWN¡£ÏÖÔÚ¼¯³ÉÁËÊý¾Ý¿â£¬Í¬Ê±°Ñ¸ÃÏîÖµÅäÖÃΪalways£¬¿´Ò»ÏÂÏêÇ飺 + +![image-20230530233934501](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233934501.png) + +¿ÉÒÔ¿´µ½ÕûÌå״̬ΪUP£¬ÆäÖÐÏÂÃæµÄÈý¸ö×é¼þ¾ùΪUP£¬¶øÊý¾Ý¿âÊÇMYSQL£¬¼ì²éÊý¾Ý¿âµÄÓï¾äΪ¡°SELECT 1¡±¡£Í¬Ê±£¬»¹Õ¹Ê¾ÁË´ÅÅÌÐÅÏ¢ºÍpingµÄ״̬¡£ + +ÏÖÔÚÎÒÃǰÑÊý¾Ý¿âµÄÓû§ÃûºÍÃÜÂë¹ÊÒâ¸Ä´í£¬ÖØÆô·ÃÎʿɵ㺠+ +![image-20230530233951145](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530233951145.png) + +״̬ΪDOWN£¬×é¼þÖÐdb³öÏÖÁËÎÊÌ⣬״̬ΪDOWN¡£ÎÊÌâµÄÏêÇéÔÚerrorÖÐչʾ£¬¿ÉÒÔ¿´³öÊǽ¨Á¢Á¬½Óʱ³ö´íÁË¡£ÔÚʵ¼ùÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýhealth¶Ë¿Ú¼à¿ØÊý¾Ý¿â¡¢Redis¡¢MongoDB¡¢´ÅÅ̵Ƚ¡¿µÇé¿ö¡£ActuatorÔ¤¶¨ÒåµÄ´¦ÀíÀàΪ£ºDataSourceHealthIndicator, DiskSpaceHealthIndicator, MongoHealthIndicator, RedisHealthIndicatorµÈ¡£ + +¶øÇÒÿ¸öÖ¸±ê¶¼¿ÉÒÔµ¥¶ÀµÄ½øÐпªÆôºÍ¹Ø±Õ£¬ÒÔÊý¾Ý¿âµÄΪÀý£º + + +```` +management: + health: + db: + enabled: true` +```` + +## info¶Ëµã + +/info ¶ËµãÓÃÀ´²é¿´ÅäÖÃÎļþ applicationÖÐÒÔinfo¿ªÍ·µÄÅäÖÃÐÅÏ¢£¬Ä¬ÈÏÇé¿öÏ applicationÖв¢Ã»ÓÐ info ½ÚµãÅäÖã¬ËùÒÔĬÈÏΪ¿Õ¡£ + +applicationÖÐÌí¼ÓÈçÏÂÅäÖ㺠+ + + +```` +info: + user: + type: ¹«ÖںŠ+ name: ³ÌÐòÐÂÊÓ½ç + wechat: zhuan2quan +```` + +·ÃÎÊ[http://localhost:8080/actuator/info](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Finfo "http://localhost:8080/actuator/info") £¬Õ¹Ê¾½á¹ûÈçÏ£º + +![image-20230530234019487](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234019487.png) + +## conditions¶Ëµã + +Spring BootÌṩÁË×Ô¶¯ÅäÖù¦ÄÜ£¬Ê¹ÓÃÆðÀ´·Ç³£·½±ã¡£µ«ÕâЩ×Ô¶¯ÅäÖÃÀàÊÇʲôÇé¿öÏÂÉúЧµÄ£¬ÊÇ·ñÉúЧÊDZȽÏÄÑÅŲéµÄ¡£´Ëʱ£¬¿ÉÒÔʹÓà conditions ÔÚÓ¦ÓÃÔËÐÐʱ²é¿´Ä³¸öÅäÖÃÀàÔÚʲôÌõ¼þÏÂÉúЧ£¬»òΪʲôûÓÐÉúЧ¡£ + +·ÃÎÊURL£º[http://localhost:8080/actuator/conditions](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fconditions "http://localhost:8080/actuator/conditions") £¬²¿·Ö·µ»ØÐÅÏ¢ÈçÏ£º + +![image-20230530234053134](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234053134.png) + +¿ÉÒÔ¿´µ½Ä³¸ö×Ô¶¯ÅäÖÃÀà¶ÔÓ¦µÄÉúЧÌõ¼þºÍÌáʾÐÅÏ¢¡£ + +## shutdown¶Ëµã + +shutdown¶ËµãÊôÓÚ²Ù×÷¿ØÖÆÀà¶Ëµã£¬¿ÉÒÔÓÅÑÅ¹Ø±Õ Spring Boot Ó¦Óá£ÐèÒªÔÚÅäÖÃÎļþÖпªÆô£º + + +```` +management: + endpoint: + shutdown: + enabled: true +```` + +¸Ã¶ËµãÖ»Ö§³ÖPOSTÇëÇó£¬Ö´ÐÐÃüÁî¼°·µ»Ø½á¹ûÈçÏ£º + + +``` +curl -X POST "http://localhost:8080/actuator/shutdown" +{ + "message": "Shutting down, bye..." +} +``` + +Ö´ÐÐÖ®ºó£¬»á·¢ÏÖÓ¦ÓóÌÐòÒѾ­±»¹Ø±ÕÁË¡£ÓÉÓڸö˵ã»á¹Ø±ÕÓ¦ÓóÌÐò£¬Òò´ËʹÓÃÊÇÐèҪСÐÄ¡£ + +## configprops¶Ëµã + +ÔÚSpring BootÏîÄ¿ÖУ¬ÎÒÃǾ­³£»áÓõ½@ConfigurationProperties×¢½âÀ´ÅúÁ¿×¢ÈëһЩÊôÐÔ£¬¶øconfigprops¶Ëµã¾ÍÊÇÓÃÀ´ÏÔʾÕâЩ±»¸Ã×¢½â±ê×¢µÄÅäÖÃÀà¡£ + +±ÈÈçÇ°ÃæµÄinfoÅäÖã¬ÎÒÃǾͿÉÒÔ¶¨ÒåÒ»¸öÀàInfoProperties£º + + +```` +@Component +@ConfigurationProperties(prefix = "info") +public class InfoProperties { + + private String type; + + private String name; + + private String wechat; + + // Ê¡ÂÔgetter/setter +} +```` + +·ÃÎÊURL£º[http://localhost:8080/actuator/configprops](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fconfigprops "http://localhost:8080/actuator/configprops") £¬²¿·ÖÐÅÏ¢ÈçÏ£º + +![image-20230530234110515](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234110515.png) + +²»½ö¿ÉÒÔ¿´µ½ÏµÍ³ÖÐĬÈϼ¯³ÉµÄÅäÖÃÀàÐÅÏ¢£¬»¹¿ÉÒÔ¿´µ½ÎÒÃÇ×Ô¶¨ÒåµÄÅäÖÃÀàÐÅÏ¢¡£ÕâÀïÐèҪעÒâµÄÊǶÔÓ¦µÄÀàÐèÒª½øÐÐʵÀý»¯£¨@Component£©ÕâÀï²ÅÄܹ»¿´µ½¡£ + +ÎÒÃÇ×Ô¶¨ÒåµÄÀàÖзµ»ØÁËBeanµÄÃû³Æ¡¢ÅäÖÃǰ׺¡£ÉÏÃæµÄProjectInfoProperties»¹·µ»ØÁËÊôÐÔÐÅÏ¢¡£ + +## env¶Ëµã + +env¶ËµãÓÃÓÚ»ñȡȫ²¿»·¾³ÊôÐÔ£¬°üÀ¨applicationÅäÖÃÎļþÖеÄÄÚÈÝ¡¢ÏµÍ³±äÁ¿µÈ¡£ + +·ÃÎÊURL£º[http://localhost:8080/actuator/env£¬·µ»Ø²¿·ÖÐÅÏ¢£º](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fenv%25EF%25BC%258C%25E8%25BF%2594%25E5%259B%259E%25E9%2583%25A8%25E5%2588%2586%25E4%25BF%25A1%25E6%2581%25AF%25EF%25BC%259A "http://localhost:8080/actuator/env%EF%BC%8C%E8%BF%94%E5%9B%9E%E9%83%A8%E5%88%86%E4%BF%A1%E6%81%AF%EF%BC%9A") + +![image-20230530234200949](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234200949.png) + +## env-toMatch¶Ëµã + +env-toMatch¶ËµãÓëcachesºÍcaches-cacheÀàËÆ£¬Ò»¸öÊÇ»ñÈ¡ËùÓеģ¬Ò»¸öÊÇ»ñȡָ¶¨µÄ¡£ÕâÀïµÄenv-toMatch¶ËµãÊÇ»ñȡָ¶¨keyµÄ»·¾³±äÁ¿ÊôÐÔ¡£ + +»ù±¾¸ñʽΪ£º[http://localhost:8080/actuator/env/{toMatch}¡£](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fenv%2F%257BtoMatch%257D%25E3%2580%2582 "http://localhost:8080/actuator/env/%7BtoMatch%7D%E3%80%82") ʵÀýURL£º[http://localhost:8080/actuator/env/info.user.name](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fenv%2Finfo.user.name "http://localhost:8080/actuator/env/info.user.name") £¬·µ»Ø½á¹ûÈçÏÂͼ£º + +![image-20230530234238073](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234238073.png) + +·µ»ØÊý¾ÝÐÅÏ¢°üÀ¨¸ÃÊôÐÔµÄÀ´Ô´¡¢valueÖµµÈÐÅÏ¢¡£ + +## loggers¶Ëµã + +/loggers ¶Ëµã±©Â¶Á˳ÌÐòÄÚ²¿ÅäÖõÄËùÓÐ logger µÄÐÅÏ¢£¬°üÀ¨²»Í¬µÄpackage¡¢²»Í¬µÄÀàµÄÈÕÖ¾¼¶±ðÐÅÏ¢¡£ + +·ÃÎÊURL£º[http://localhost:8080/actuator/loggers](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Floggers "http://localhost:8080/actuator/loggers") £¬²¿·Ö·µ»Ø½á¹û£º + +![image-20230530234301625](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234301625.png) + +## loggers-name¶Ëµã + +loggers-name¶ËµãÒ²ÊÇlogger¶ËµãµÄϸ·Ö£¬¿ÉÒÔͨ¹ýname·ÃÎÊijһ¸ölogger¡£ + +»ù±¾ÇëÇó¸ñʽ£º[http://localhost:8080/actuator/loggers/{name}](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Floggers%2F%257Bname%257D "http://localhost:8080/actuator/loggers/%7Bname%7D") ʾÀýÇëÇóURL£º[http://localhost:8080/actuator/loggers/com.secbro2.SpringbootActuatorApplication](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Floggers%2Fcom.secbro2.SpringbootActuatorApplication "http://localhost:8080/actuator/loggers/com.secbro2.SpringbootActuatorApplication") £¬·µ»Ø½á¹ûÈçÏ£º + + + +`{ +"configuredLevel": null, +"effectiveLevel": "INFO" +}` + +¿ÉÒÔ¿´³ö£¬Æô¶¯ÀàµÄÈÕÖ¾¼¶±ðΪINFO¡£ + +## heapdump¶Ëµã + +heapdump¶Ëµã»á·µ»ØÒ»¸öJVM ¶Ñdump£¬Í¨¹ýJVM×Ô´øµÄ¼à¿Ø¹¤¾ßVisualVM¿É´ò¿ª´ËÎļþ²é¿´ÄÚ´æ¿ìÕÕ¡£ÕâÊÇÄÚ´æÓÅ»¯£¬»ùÓÚ¶ÑÕ»²ãÃæ½øÐÐÅŲéµÄÀûÆ÷¡£ + +·ÃÎÊURL£º[http://localhost:8080/actuator/heapdump](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fheapdump "http://localhost:8080/actuator/heapdump") ¡£Mac²Ù×÷ϵͳÏÂä¯ÀÀÆ÷·ÃÎÊ»áÏÂÔØÒ»¸öÃû×ÖΪheapdumpµÄÎļþ£¬ÎÞºó׺£¬30M¡£ + +ÃüÁîÐÐÖ´ÐÐjvisualvmÃüÁ´ò¿ªVisualVM£¬ÒÀ´Îµã»÷¡°Îļþ¡±¡¢¡°×°È롱£¬¼ÇµÃÎļþÀàÐÍҪѡÔñ¡°¶ÑDump(_.hprof,_.*)¡±£¬È»ºóÑ¡Ôñheapdump¡£ + +![image-20230530234346098](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234346098.png) + +´Ëʱ±ã¿ÉÒÔͨ¹ý¹¤¾ßÀ´½øÐжÑÕ»ÐÅÏ¢µÄ·ÖÎöÁË¡£¶ÔÓÚÏßÉÏÎÊÌâµÄ·ÖÎöÌṩÁ˼«Îª±ãÀûµÄ·½Ê½¡£ + +## threaddump¶Ëµã + +/threaddump ¶Ëµã»áÉú³Éµ±Ç°Ï̻߳µÄ¿ìÕÕ¡£ÔÚÈÕ³£¶¨Î»ÎÊÌâµÄʱºò²é¿´Ï̵߳ÄÇé¿ö·Ç³£ÓÐÓã¬Ö÷ҪչʾÁËÏß³ÌÃû¡¢Ïß³ÌID¡¢Ï̵߳Ä״̬¡¢ÊÇ·ñµÈ´ýËø×ÊÔ´µÈÐÅÏ¢¡£ + +·ÃÎÊURL£º[http://localhost:8080/actuator/threaddump](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fthreaddump "http://localhost:8080/actuator/threaddump") £¬²¿·Ö·µ»Ø½á¹û£º + +![image-20230530234405331](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234405331.png) + +ÎÒÃÇ¿Éͨ¹ýÏ߳̿ìÕÕÀ´ÅŲéÉú²ú»·¾³µÄÎÊÌâ¡£ + +## metrics¶Ëµã + +/metrics ¶ËµãÓÃÀ´±©Â¶µ±Ç°Ó¦Óõĸ÷ÀàÖØÒª¶ÈÁ¿Ö¸±ê£¬±ÈÈ磺ÄÚ´æÐÅÏ¢¡¢Ïß³ÌÐÅÏ¢¡¢À¬»ø»ØÊÕÐÅÏ¢¡¢tomcat¡¢Êý¾Ý¿âÁ¬½Ó³ØµÈ¡£2.x°æ±¾ÕâÀïÖ»ÏÔʾÁËÒ»¸öÖ¸±êµÄÁÐ±í¡£ + +·ÃÎÊURL£º[http://localhost:8080/actuator/metrics](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fmetrics "http://localhost:8080/actuator/metrics") ¡£ + +```` +{ + "names": [ + "jvm.memory.max", + "jvm.threads.states", + "jvm.gc.pause", + "http.server.requests", + "process.files.max", + "jvm.gc.memory.promoted", + "system.load.average.1m", + "jvm.memory.used", + "jvm.gc.max.data.size", + "jvm.memory.committed", + "system.cpu.count", + "logback.events", + "jvm.buffer.memory.used", + "tomcat.sessions.created", + "jvm.threads.daemon", + "system.cpu.usage", + "jvm.gc.memory.allocated", + "tomcat.sessions.expired", + "jvm.threads.live", + "jvm.threads.peak", + "process.uptime", + "tomcat.sessions.rejected", + "process.cpu.usage", + "jvm.classes.loaded", + "jvm.classes.unloaded", + "tomcat.sessions.active.current", + "tomcat.sessions.alive.max", + "jvm.gc.live.data.size", + "process.files.open", + "jvm.buffer.count", + "jvm.buffer.total.capacity", + "tomcat.sessions.active.max", + "process.start.time" + ] +} +```` + +/metrics¶Ëµã¿ÉÒÔÌṩӦÓÃÔËÐÐ״̬µÄÍêÕû¶ÈÁ¿Ö¸±ê±¨¸æ£¬ÕâÏÄܷdz£µÄʵÓ㬵«ÊǶÔÓÚ¼à¿ØÏµÍ³Öеĸ÷Ïî¼à¿Ø¹¦ÄÜ£¬ËüÃÇµÄ¼à¿ØÄÚÈÝ¡¢Êý¾ÝÊÕ¼¯ÆµÂʶ¼ÓÐËù²»Í¬£¬Èç¹ûÎÒÃÇÿ´Î¶¼Í¨¹ýÈ«Á¿»ñÈ¡±¨¸æµÄ·½Ê½À´ÊÕ¼¯£¬ÂÔÏÔ´Ö±©¡£¹Ù·½Ò²ÊÇ¿ÉÄÜÊÇ´¦ÓÚ´Ë·½ÃæµÄ¿¼ÂÇ£¬ÔÚSpring Boot 2.xÖ®ºó£¬/metrics¶ËµãÖ»ÏÔʾÁËÖ¸±êµÄÁÐ±í¡£ + +Èç¹ûÐèÒª²é¿´¾ßÌåµÄijÏîÖ¸±ê£¬Ôò¿Éͨ¹ý/metrics-requiredMetricName¶ËµãÀ´ÊµÏÖ¡£ + +## metrics-requiredMetricName¶Ëµã + +metrics-requiredMetricName¶Ëµã£¬ÓÃÓÚ·ÃÎÊÖ¸¶¨Ö¸±êµÄ±¨¸æ£¬Ò»°ã»áÅäºÏ/metrics¶ËµãÏȲé³öÖ¸±êÁÐ±í£¬È»ºóÔÙ²éѯ¾ßÌåµÄij¸öÖ¸±ê¡£ + +»ù±¾¸ñʽ£º[http://localhost:8080/actuator/metrics/{requiredMetricName}¡£](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fmetrics%2F%257BrequiredMetricName%257D%25E3%2580%2582 "http://localhost:8080/actuator/metrics/%7BrequiredMetricName%7D%E3%80%82") ʵÀýURL£º[http://localhost:8080/actuator/metrics/jvm.memory.max](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fmetrics%2Fjvm.memory.max "http://localhost:8080/actuator/metrics/jvm.memory.max") £¬·µ»Ø½á¹ûÈçÏ£º + + +```` +{ + "name": "jvm.memory.max", + "description": "The maximum amount of memory in bytes that can be used for memory management", + "baseUnit": "bytes", + "measurements": [ + { + "statistic": "VALUE", + "value": 5606211583 + } + ], + "availableTags": [ + { + "tag": "area", + "values": [ + "heap", + "nonheap" + ] + }, + { + "tag": "id", + "values": [ + "Compressed Class Space", + "PS Survivor Space", + "PS Old Gen", + "Metaspace", + "PS Eden Space", + "Code Cache" + ] + } + ] +} +```` + +ÉÏÊö½á¹ûչʾÁË×î´ó¿ÉÓÃÄÚ´æµÄÇé¿ö¡£ÆäËûÏà¹ØÖ¸±êµÄÕ¹Ê¾Ìæ»»¶ÔÓ¦µÄÃû×Ö½øÐв鿴¼´¿É¡£ + +## scheduledtasks¶Ëµã + +/scheduledtasks¶ËµãÓÃÓÚչʾӦÓÃÖеĶ¨Ê±ÈÎÎñÐÅÏ¢¡£ + +ÏÈÔÚÏîÄ¿Öй¹½¨Á½¸ö¶¨Ê±ÈÎÎñ£¬Ê×ÏÈÔÚÆô¶¯ÀàÉÏÌí¼Ó@EnableScheduling¿ªÆô¶¨Ê±ÈÎÎñ¹¦ÄÜ¡£È»ºó´´½¨¶¨Ê±ÈÎÎñÀࣺ + +```` +@Component +public class MyTask { + + @Scheduled(cron = "0/10 * * * * *") + public void work() { + System.out.println("I am a cron job."); + } + + @Scheduled(fixedDelay = 10000) + public void work1() { + System.out.println("I am a fixedDelay job."); + } +} +```` + +ÆäÖж¨ÒåÁËÁ½ÖÖÀàÐ͵Ķ¨Ê±ÈÎÎñ£¬workÊÇ»ùÓÚcronʵÏֵĶ¨Ê±ÈÎÎñ£¬work1ÊÇ»ùÓÚfixedDelayʵÏֵĶ¨Ê±ÈÎÎñ¡£ + +·ÃÎÊURL£º[http://localhost:8080/actuator/scheduledtasks](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fscheduledtasks "http://localhost:8080/actuator/scheduledtasks") £¬·µ»Ø½á¹ûÐÅÏ¢ÈçÏ£º + +``` +{ + "cron": [ + { + "runnable": { + "target": "com.secbro2.job.MyTask.work" + }, + "expression": "0/10 * * * * *" + } + ], + "fixedDelay": [ + { + "runnable": { + "target": "com.secbro2.job.MyTask.work1" + }, + "initialDelay": 0, + "interval": 10000 + } + ], + "fixedRate": [], + "custom": [] +} +``` + +¿ÉÒÔ¿´µ½£¬Í¨¹ý¸Ã¶Ëµã¿ÉÒÔÃ÷È·µÄÖªµÀµ±Ç°Ó¦ÓÃÖж¨ÒåµÄ¶¨Ê±ÈÎÎñ£¬ÒÔ¼°Ö´ÐÐģʽºÍƵ´Î¡£ + +## mappings¶Ëµã + +/mappings¶ËµãÓÃÓÚÃèÊöÈ«²¿µÄ URI ·¾¶£¬ÒÔ¼°ºÍ¿ØÖÆÆ÷µÄÓ³Éä¹ØÏµ¡£Õâ¸ö¹¦ÄÜËãÊDZȽϳ£ÓõÄÁË£¬Èç¹ûÏëϵͳµÄ²é¿´URL¶ÔÓ¦µÄController¼°·½·¨£¬¿ÉÒÔʹÓô˶˵㡣 + +·ÃÎÊURL£º[http://localhost:8080/actuator/mappings](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8080%2Factuator%2Fmappings "http://localhost:8080/actuator/mappings") £¬²¿·Ö·µ»Ø½á¹ûÈçÏ£º + +![image-20230530234501440](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230530234501440.png) + +ÖÁ´Ë¹ØÓÚSpring Boot ActuatorÌṩµÄËùÓж˵ã½éÉÜÍê±Ï¡£ + +## С½á + +±¾ÎÄͨ¹ý¶ÔSpring Boot ActuatorÌṩËùÓж˵㹹½¨ÊµÀý²¢ÑÝʾ½á¹û£¬±¾ÎÄ´ó¶àÊýÄÚÈݺÍʵÀý¶¼ÊÇÈ«ÍøµÚÒ»ÊÖ×ÊÁÏ¡£¸Ã¿ò¼Ü¶ÔÅŲéÏßÉÏÎÊÌ⣬ÐÔÄÜÓÅ»¯µÈ¶¼Óм«´óµÄ°ïÖú¡£¶øÔÚд±¾ÎĵĹý³ÌÖÐÒ²Ô½À´Ô½¾ªÌ¾ActuatorµÄ¹¦ÄÜ֮ǿ´ó£¬Ç¿ÁÒÍÆ¼öÓÃÆðÀ´¡£ + +## ²Î¿¼Á´½Ó + +×÷Õߣº³ÌÐòÐÂÊÓ½ç +Á´½Ó£ºhttps://juejin.cn/post/6984550846876876814 +À´Ô´£ºÏ¡ÍÁ¾ò½ð +Öø×÷Ȩ¹é×÷ÕßËùÓС£ÉÌÒµ×ªÔØÇëÁªÏµ×÷Õß»ñµÃÊÚȨ£¬·ÇÉÌÒµ×ªÔØÇë×¢Ã÷³ö´¦¡£ + diff --git a/docs/monitor/SpringBoot Admin.md b/docs/monitor/SpringBoot Admin.md new file mode 100644 index 0000000..7b0c8ab --- /dev/null +++ b/docs/monitor/SpringBoot Admin.md @@ -0,0 +1,495 @@ +## ÕªÒª + +Spring Boot Admin ¿ÉÒÔ¶ÔSpringBootÓ¦Óõĸ÷ÏîÖ¸±ê½øÐÐ¼à¿Ø£¬¿ÉÒÔ×÷Ϊ΢·þÎñ¼Ü¹¹ÖÐµÄ¼à¿ØÖÐÐÄÀ´Ê¹Ó㬱¾ÎĽ«¶ÔÆäÓ÷¨½øÐÐÏêϸ½éÉÜ¡£ + +## Spring Boot Admin ¼ò½é + +SpringBootÓ¦ÓÿÉÒÔͨ¹ýActuatorÀ´±©Â¶Ó¦ÓÃÔËÐйý³ÌÖеĸ÷ÏîÖ¸±ê£¬Spring Boot Adminͨ¹ýÕâЩָ±êÀ´¼à¿ØSpringBootÓ¦Óã¬È»ºóͨ¹ýͼÐλ¯½çÃæ³ÊÏÖ³öÀ´¡£Spring Boot Admin²»½ö¿ÉÒÔ¼à¿Øµ¥ÌåÓ¦Ó㬻¹¿ÉÒÔºÍSpring CloudµÄ×¢²áÖÐÐÄÏà½áºÏÀ´¼à¿ØÎ¢·þÎñÓ¦Óᣠ+ +Spring Boot Admin ¿ÉÒÔÌṩӦÓõÄÒÔÏÂ¼à¿ØÐÅÏ¢£º + +* ¼à¿ØÓ¦ÓÃÔËÐйý³ÌÖеĸÅÀÀÐÅÏ¢£» +* ¶ÈÁ¿Ö¸±êÐÅÏ¢£¬±ÈÈçJVM¡¢Tomcat¼°½ø³ÌÐÅÏ¢£» +* »·¾³±äÁ¿ÐÅÏ¢£¬±ÈÈçϵͳÊôÐÔ¡¢ÏµÍ³»·¾³±äÁ¿ÒÔ¼°Ó¦ÓÃÅäÖÃÐÅÏ¢£» +* ²é¿´ËùÓд´½¨µÄBeanÐÅÏ¢£» +* ²é¿´Ó¦ÓÃÖеÄËùÓÐÅäÖÃÐÅÏ¢£» +* ²é¿´Ó¦ÓÃÔËÐÐÈÕÖ¾ÐÅÏ¢£» +* ²é¿´JVMÐÅÏ¢£» +* ²é¿´¿ÉÒÔ·ÃÎʵÄWeb¶Ëµã£» +* ²é¿´HTTP¸ú×ÙÐÅÏ¢¡£ + +## ´´½¨admin-serverÄ£¿é + +> ÕâÀïÎÒÃÇ´´½¨Ò»¸öadmin-serverÄ£¿éÀ´×÷Ϊ¼à¿ØÖÐÐÄÑÝʾÆä¹¦ÄÜ¡£ + +* ÔÚpom.xmlÖÐÌí¼ÓÏà¹ØÒÀÀµ£º + + + + + +```` + + org.springframework.boot + spring-boot-starter-web + + + de.codecentric + spring-boot-admin-starter-server + + +```` + +* ÔÚapplication.ymlÖнøÐÐÅäÖ㺠+ + + + + +````spring: + application: + name: admin-server +server: + port: 9301 +```` + +* ÔÚÆô¶¯ÀàÉÏÌí¼Ó@EnableAdminServerÀ´ÆôÓÃadmin-server¹¦ÄÜ£º + + + + +```` +@EnableAdminServer +@SpringBootApplication +public class AdminServerApplication { + + public static void main(String[] args) { + SpringApplication.run(AdminServerApplication.class, args); + } + +} +```` + +## ´´½¨admin-clientÄ£¿é + +> ÕâÀïÎÒÃÇ´´½¨Ò»¸öadmin-clientÄ£¿é×÷Ϊ¿Í»§¶Ë×¢²áµ½admin-server¡£ + +* ÔÚpom.xmlÖÐÌí¼ÓÏà¹ØÒÀÀµ£º + + + +```` + + org.springframework.boot + spring-boot-starter-web + + + de.codecentric + spring-boot-admin-starter-client + +```` + +* ÔÚapplication.ymlÖнøÐÐÅäÖ㺠+ + + + + + + + +``` +spring: + application: + name: admin-client + boot: + admin: + client: + url: http://localhost:9301 #ÅäÖÃadmin-serverµØÖ· +server: + port: 9305 +management: + endpoints: + web: + exposure: + include: '*' + endpoint: + health: + show-details: always +logging: + file: admin-client.log #Ìí¼Ó¿ªÆôadminµÄÈÕÖ¾¼à¿Ø +``` + +* Æô¶¯admin-serverºÍadmin-client·þÎñ¡£ + +## ¼à¿ØÐÅÏ¢ÑÝʾ + +* ·ÃÎÊÈçϵØÖ·´ò¿ªSpring Boot AdminµÄÖ÷Ò³£º[http://localhost:9301](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A9301 "http://localhost:9301") + +![image-20230531001002163](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001002163.png) + + + + + +* µã»÷wallboard°´Å¥£¬Ñ¡Ôñadmin-client²é¿´¼à¿ØÐÅÏ¢£» + +* ¼à¿ØÐÅÏ¢¸ÅÀÀ£» + +![image-20230531001023644](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001023644.png) + + + + + +* ¶ÈÁ¿Ö¸±êÐÅÏ¢£¬±ÈÈçJVM¡¢Tomcat¼°½ø³ÌÐÅÏ¢£» + +![image-20230531001053279](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001053279.png) + + + + + +* »·¾³±äÁ¿ÐÅÏ¢£¬±ÈÈçϵͳÊôÐÔ¡¢ÏµÍ³»·¾³±äÁ¿ÒÔ¼°Ó¦ÓÃÅäÖÃÐÅÏ¢£» + +![image-20230531001103093](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001103093.png) + + + + + +* ²é¿´ËùÓд´½¨µÄBeanÐÅÏ¢£» + +![image-20230531001111221](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001111221.png) + + + + + +* ²é¿´Ó¦ÓÃÖеÄËùÓÐÅäÖÃÐÅÏ¢£» + +![image-20230531001124678](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001124678.png) + + + + + +* ²é¿´ÈÕÖ¾ÐÅÏ¢£¬ÐèÒªÌí¼ÓÒÔÏÂÅäÖòÅÄÜ¿ªÆô£» + + + +`logging: +file: admin-client.log #Ìí¼Ó¿ªÆôadminµÄÈÕÖ¾¼à¿Ø` + +![image-20230531001136184](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001136184.png) + + + + +* ²é¿´JVMÐÅÏ¢£» + +![image-20230531001144614](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001144614.png) + + + + + +* ²é¿´¿ÉÒÔ·ÃÎʵÄWeb¶Ëµã£» + +![image-20230531001156191](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001156191.png) + + + + + +* ²é¿´HTTP¸ú×ÙÐÅÏ¢£» + +![image-20230531001206364](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001206364.png) + + + + + +## ½áºÏ×¢²áÖÐÐÄʹÓà + +> Spring Boot Admin½áºÏSpring Cloud ×¢²áÖÐÐÄʹÓã¬Ö»Ð轫admin-serverºÍ×¢²áÖÐÐÄÕûºÏ¼´¿É£¬admin-server »á×Ô¶¯´Ó×¢²áÖÐÐÄ»ñÈ¡·þÎñÁÐ±í£¬È»ºó°¤¸ö»ñÈ¡¼à¿ØÐÅÏ¢¡£ÕâÀïÒÔEureka×¢²áÖÐÐÄΪÀýÀ´½éÉÜϸù¦ÄÜ¡£ + +### ÐÞ¸Äadmin-server + +* ÔÚpom.xmlÖÐÌí¼ÓÏà¹ØÒÀÀµ£º + + + + +```` + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + +```` + +* ÔÚapplication-eureka.ymlÖнøÐÐÅäÖã¬Ö»ÐèÌí¼Ó×¢²áÖÐÐÄÅäÖü´¿É£º + + + + +```` +spring: + application: + name: admin-server +server: + port: 9301 +eureka: + client: + register-with-eureka: true + fetch-registry: true + service-url: + defaultZone: http://localhost:8001/eureka/ +```` + +* ÔÚÆô¶¯ÀàÉÏÌí¼Ó@EnableDiscoveryClientÀ´ÆôÓ÷þÎñ×¢²á¹¦ÄÜ£º + + + + + +```` +`@EnableDiscoveryClient +@EnableAdminServer +@SpringBootApplication +public class AdminServerApplication { + + public static void main(String[] args) { + SpringApplication.run(AdminServerApplication.class, args); + } + +} +```` + +### ÐÞ¸Äadmin-client + +* ÔÚpom.xmlÖÐÌí¼ÓÏà¹ØÒÀÀµ£º + + + +```` + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + +```` + +* ÔÚapplication-eureka.ymlÖнøÐÐÅäÖã¬É¾³ýÔ­À´µÄadmin-serverµØÖ·ÅäÖã¬Ìí¼Ó×¢²áÖÐÐÄÅäÖü´¿É£º + + + + + +``` +spring: + application: + name: admin-client +server: + port: 9305 +management: + endpoints: + web: + exposure: + include: '*' + endpoint: + health: + show-details: always +logging: + file: admin-client.log #Ìí¼Ó¿ªÆôadminµÄÈÕÖ¾¼à¿Ø +eureka: + client: + register-with-eureka: true + fetch-registry: true + service-url: + defaultZone: http://localhost:8001/eureka/ +``` + +* ÔÚÆô¶¯ÀàÉÏÌí¼Ó@EnableDiscoveryClientÀ´ÆôÓ÷þÎñ×¢²á¹¦ÄÜ£º + + + + + +``` +@EnableDiscoveryClient +@SpringBootApplication +public class AdminClientApplication { + + public static void main(String[] args) { + SpringApplication.run(AdminClientApplication.class, args); + } + +} +``` + +### ¹¦ÄÜÑÝʾ + +* Æô¶¯eureka-server£¬Ê¹ÓÃapplication-eureka.ymlÅäÖÃÆô¶¯admin-server£¬admin-client£» + +* ²é¿´×¢²áÖÐÐÄ·¢ÏÖ·þÎñ¾ùÒÑ×¢²á£º[http://localhost:8001/](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A8001%2F "http://localhost:8001/") + +![image-20230531001221519](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001221519.png) + + +* ²é¿´Spring Boot Admin Ö÷Ò³·¢ÏÖ¿ÉÒÔ¿´µ½·þÎñÐÅÏ¢£º[http://localhost:9301](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A9301 "http://localhost:9301") + +![image-20230531001232048](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001232048.png) + + +## Ìí¼ÓµÇ¼ÈÏÖ¤ + +> ÎÒÃÇ¿ÉÒÔͨ¹ý¸øadmin-serverÌí¼ÓSpring SecurityÖ§³ÖÀ´»ñµÃµÇ¼ÈÏÖ¤¹¦ÄÜ¡£ + +### ´´½¨admin-security-serverÄ£¿é + +* ÔÚpom.xmlÖÐÌí¼ÓÏà¹ØÒÀÀµ£º + + + +``` + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + de.codecentric + spring-boot-admin-starter-server + 2.1.5 + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + +``` + +* ÔÚapplication.ymlÖнøÐÐÅäÖã¬ÅäÖõǼÓû§ÃûºÍÃÜÂ룬ºöÂÔadmin-security-serverµÄ¼à¿ØÐÅÏ¢£º + + + + + +``` +spring: + application: + name: admin-security-server + security: # ÅäÖõǼÓû§ÃûºÍÃÜÂë + user: + name: macro + password: 123456 + boot: # ²»ÏÔʾadmin-security-serverµÄ¼à¿ØÐÅÏ¢ + admin: + discovery: + ignored-services: ${spring.application.name} +server: + port: 9301 +eureka: + client: + register-with-eureka: true + fetch-registry: true + service-url: + defaultZone: http://localhost:8001/eureka/ +``` + +* ¶ÔSpringSecurity½øÐÐÅäÖã¬ÒÔ±ãadmin-client¿ÉÒÔ×¢²á£º + + + + + +scss + +¸´ÖÆ´úÂë + + + + + +``` +/** + * Created by macro on 2019/9/30. + */ +@Configuration +public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { + private final String adminContextPath; + + public SecuritySecureConfig(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() + //1.ÅäÖÃËùÓо²Ì¬×ÊÔ´ºÍµÇ¼ҳ¿ÉÒÔ¹«¿ª·ÃÎÊ + .antMatchers(adminContextPath + "/assets/**").permitAll() + .antMatchers(adminContextPath + "/login").permitAll() + .anyRequest().authenticated() + .and() + //2.ÅäÖõǼºÍµÇ³ö·¾¶ + .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and() + .logout().logoutUrl(adminContextPath + "/logout").and() + //3.¿ªÆôhttp basicÖ§³Ö£¬admin-client×¢²áʱÐèҪʹÓà + .httpBasic().and() + .csrf() + //4.¿ªÆô»ùÓÚcookieµÄcsrf±£»¤ + .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) + //5.ºöÂÔÕâЩ·¾¶µÄcsrf±£»¤ÒÔ±ãadmin-client×¢²á + .ignoringAntMatchers( + adminContextPath + "/instances", + adminContextPath + "/actuator/**" + ); + } +} +``` + +* ÐÞ¸ÄÆô¶¯À࣬¿ªÆôAdminServer¼°×¢²á·¢ÏÖ¹¦ÄÜ£º + + + + + +``` +@EnableDiscoveryClient +@EnableAdminServer +@SpringBootApplication +public class AdminSecurityServerApplication { + + public static void main(String[] args) { + SpringApplication.run(AdminSecurityServerApplication.class, args); + } +} +``` + +* Æô¶¯eureka-server£¬admin-security-server£¬·ÃÎÊSpring Boot Admin Ö÷Ò³·¢ÏÖÐèÒªµÇ¼²ÅÄÜ·ÃÎÊ£º[http://localhost:9301](https://link.juejin.cn?target=http%3A%2F%2Flocalhost%3A9301 "http://localhost:9301") + +![image-20230531001242361](https://java-tutorial.oss-cn-shanghai.aliyuncs.com/image-20230531001242361.png) + + + + + +## ʹÓõ½µÄÄ£¿é + + + + + +``` +springcloud-learning +©À©¤©¤ eureka-server -- eureka×¢²áÖÐÐÄ +©À©¤©¤ admin-server -- admin¼à¿ØÖÐÐÄ·þÎñ +©À©¤©¤ admin-client -- admin¼à¿ØÖÐÐÄ¼à¿ØµÄÓ¦Ó÷þÎñ +©¸©¤©¤ admin-security-server -- ´øµÇ¼ÈÏÖ¤µÄadmin¼à¿ØÖÐÐÄ·þÎñ` +``` \ No newline at end of file diff --git "a/docs/mq/RocketMQ/RocketMQ\347\263\273\345\210\227\357\274\232\346\266\210\346\201\257\347\232\204\347\224\237\344\272\247\344\270\216\346\266\210\350\264\271 .md" "b/docs/mq/RocketMQ/RocketMQ\347\263\273\345\210\227\357\274\232\346\266\210\346\201\257\347\232\204\347\224\237\344\272\247\344\270\216\346\266\210\350\264\271.md" similarity index 100% rename from "docs/mq/RocketMQ/RocketMQ\347\263\273\345\210\227\357\274\232\346\266\210\346\201\257\347\232\204\347\224\237\344\272\247\344\270\216\346\266\210\350\264\271 .md" rename to "docs/mq/RocketMQ/RocketMQ\347\263\273\345\210\227\357\274\232\346\266\210\346\201\257\347\232\204\347\224\237\344\272\247\344\270\216\346\266\210\350\264\271.md" diff --git "a/docs/mq/RocketMQ/RocketMQ\347\263\273\345\210\227\357\274\232\351\241\272\345\272\217\346\266\210\350\264\271 .md" "b/docs/mq/RocketMQ/RocketMQ\347\263\273\345\210\227\357\274\232\351\241\272\345\272\217\346\266\210\350\264\271.md" similarity index 100% rename from "docs/mq/RocketMQ/RocketMQ\347\263\273\345\210\227\357\274\232\351\241\272\345\272\217\346\266\210\350\264\271 .md" rename to "docs/mq/RocketMQ/RocketMQ\347\263\273\345\210\227\357\274\232\351\241\272\345\272\217\346\266\210\350\264\271.md"