[fix]:修复日志打印
Showing
5 changed files
with
31 additions
and
3 deletions
| ... | @@ -48,6 +48,13 @@ | ... | @@ -48,6 +48,13 @@ |
| 48 | <groupId>org.springframework.boot</groupId> | 48 | <groupId>org.springframework.boot</groupId> |
| 49 | <artifactId>spring-boot-starter-actuator</artifactId> | 49 | <artifactId>spring-boot-starter-actuator</artifactId> |
| 50 | </dependency> | 50 | </dependency> |
| 51 | |||
| 52 | <!-- 日志组件 --> | ||
| 53 | <dependency> | ||
| 54 | <groupId>org.springframework.boot</groupId> | ||
| 55 | <artifactId>spring-boot-starter-logging</artifactId> | ||
| 56 | </dependency> | ||
| 57 | |||
| 51 | <dependency> | 58 | <dependency> |
| 52 | <groupId>org.springframework.boot</groupId> | 59 | <groupId>org.springframework.boot</groupId> |
| 53 | <artifactId>spring-boot-configuration-processor</artifactId> | 60 | <artifactId>spring-boot-configuration-processor</artifactId> | ... | ... |
| 1 | package com.seektruth.demo.spring.boot.resilience4j; | 1 | package com.seektruth.demo.spring.boot.resilience4j; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | import com.seektruth.demo.spring.boot.resilience4j.config.RegistryEventBeanConfig; | ||
| 5 | import org.slf4j.Logger; | ||
| 6 | import org.slf4j.LoggerFactory; | ||
| 4 | import org.springframework.boot.SpringApplication; | 7 | import org.springframework.boot.SpringApplication; |
| 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; | 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 6 | import org.springframework.context.ApplicationEvent; | 9 | import org.springframework.context.ApplicationEvent; |
| ... | @@ -13,12 +16,13 @@ import org.springframework.context.event.ContextClosedEvent; | ... | @@ -13,12 +16,13 @@ import org.springframework.context.event.ContextClosedEvent; |
| 13 | */ | 16 | */ |
| 14 | @SpringBootApplication | 17 | @SpringBootApplication |
| 15 | public class BootStrap { | 18 | public class BootStrap { |
| 19 | private static final Logger LOG = LoggerFactory.getLogger(BootStrap.class); | ||
| 16 | 20 | ||
| 17 | public static void main(String[] args) { | 21 | public static void main(String[] args) { |
| 18 | ConfigurableApplicationContext ctx = SpringApplication.run(BootStrap.class,args); | 22 | ConfigurableApplicationContext ctx = SpringApplication.run(BootStrap.class,args); |
| 19 | boolean running = ctx.isRunning(); | 23 | boolean running = ctx.isRunning(); |
| 20 | if(running){ | 24 | if(running){ |
| 21 | System.out.println("启动已启动"); | 25 | LOG.info("启动已启动"); |
| 22 | } | 26 | } |
| 23 | } | 27 | } |
| 24 | } | 28 | } | ... | ... |
| ... | @@ -7,10 +7,12 @@ import org.aspectj.lang.annotation.Pointcut; | ... | @@ -7,10 +7,12 @@ import org.aspectj.lang.annotation.Pointcut; |
| 7 | import org.slf4j.Logger; | 7 | import org.slf4j.Logger; |
| 8 | import org.slf4j.LoggerFactory; | 8 | import org.slf4j.LoggerFactory; |
| 9 | import org.springframework.core.Ordered; | 9 | import org.springframework.core.Ordered; |
| 10 | import org.springframework.stereotype.Component; | ||
| 10 | 11 | ||
| 11 | /** | 12 | /** |
| 12 | * 操作日志切面 | 13 | * 操作日志切面 |
| 13 | */ | 14 | */ |
| 15 | @Component | ||
| 14 | @Aspect | 16 | @Aspect |
| 15 | public class OperateLogAspect implements Ordered { | 17 | public class OperateLogAspect implements Ordered { |
| 16 | private static final Logger LOG = LoggerFactory.getLogger(OperateLogAspect.class); | 18 | private static final Logger LOG = LoggerFactory.getLogger(OperateLogAspect.class); |
| ... | @@ -18,7 +20,7 @@ public class OperateLogAspect implements Ordered { | ... | @@ -18,7 +20,7 @@ public class OperateLogAspect implements Ordered { |
| 18 | /** | 20 | /** |
| 19 | * 设置切入点:横切所有 controller 类的所有方法 | 21 | * 设置切入点:横切所有 controller 类的所有方法 |
| 20 | */ | 22 | */ |
| 21 | @Pointcut("execution(* com.seektruth.demo.spring.boot.resillience4j.controller.*.*(..))") | 23 | @Pointcut("execution(* com.seektruth.demo.spring.boot.resilience4j.controller.*.*(..))") |
| 22 | public void operateLogPointcut() { | 24 | public void operateLogPointcut() { |
| 23 | // 该方法无方法体,主要为了让同类中其他方法使用此切入点 | 25 | // 该方法无方法体,主要为了让同类中其他方法使用此切入点 |
| 24 | } | 26 | } |
| ... | @@ -35,8 +37,10 @@ public class OperateLogAspect implements Ordered { | ... | @@ -35,8 +37,10 @@ public class OperateLogAspect implements Ordered { |
| 35 | Object[] args = joinPoint.getArgs(); | 37 | Object[] args = joinPoint.getArgs(); |
| 36 | LOG.info("========================================"); | 38 | LOG.info("========================================"); |
| 37 | LOG.info(" 操作日志打印:调用 {} {} 方法,请求参数:{}",className,methodName,args); | 39 | LOG.info(" 操作日志打印:调用 {} {} 方法,请求参数:{}",className,methodName,args); |
| 40 | Object result = joinPoint.proceed(args); | ||
| 41 | LOG.info(" 操作日志打印:{} {} 方法,返回结果:{}",result); | ||
| 38 | LOG.info("========================================"); | 42 | LOG.info("========================================"); |
| 39 | return joinPoint.proceed(args); | 43 | return result; |
| 40 | } | 44 | } |
| 41 | 45 | ||
| 42 | @Override | 46 | @Override | ... | ... |
| 1 | package com.seektruth.demo.spring.boot.resilience4j.controller; | 1 | package com.seektruth.demo.spring.boot.resilience4j.controller; |
| 2 | 2 | ||
| 3 | import com.seektruth.demo.spring.boot.resilience4j.BootStrap; | ||
| 3 | import io.github.resilience4j.bulkhead.BulkheadFullException; | 4 | import io.github.resilience4j.bulkhead.BulkheadFullException; |
| 4 | import io.github.resilience4j.circuitbreaker.CallNotPermittedException; | 5 | import io.github.resilience4j.circuitbreaker.CallNotPermittedException; |
| 5 | import io.github.resilience4j.ratelimiter.RequestNotPermitted; | 6 | import io.github.resilience4j.ratelimiter.RequestNotPermitted; |
| 6 | import io.github.resilience4j.retry.MaxRetriesExceededException; | 7 | import io.github.resilience4j.retry.MaxRetriesExceededException; |
| 8 | import org.slf4j.Logger; | ||
| 9 | import org.slf4j.LoggerFactory; | ||
| 7 | 10 | ||
| 8 | /** | 11 | /** |
| 9 | * 降级方法 | 12 | * 降级方法 |
| 10 | */ | 13 | */ |
| 11 | public class BaseController { | 14 | public class BaseController { |
| 15 | private static final Logger LOG = LoggerFactory.getLogger(BaseController.class); | ||
| 12 | 16 | ||
| 13 | public String fallback(CallNotPermittedException ex){ | 17 | public String fallback(CallNotPermittedException ex){ |
| 18 | LOG.info("服务已熔断。"); | ||
| 14 | return "服务已熔断。"; | 19 | return "服务已熔断。"; |
| 15 | } | 20 | } |
| 16 | 21 | ||
| 17 | public String fallback(BulkheadFullException ex){ | 22 | public String fallback(BulkheadFullException ex){ |
| 23 | LOG.info("服务并发限制,不允许访问。"); | ||
| 18 | return "服务并发限制,不允许访问。"; | 24 | return "服务并发限制,不允许访问。"; |
| 19 | } | 25 | } |
| 20 | 26 | ||
| 21 | public String fallback(RequestNotPermitted ex){ | 27 | public String fallback(RequestNotPermitted ex){ |
| 28 | LOG.info("限速了不允许访问了。"); | ||
| 22 | return "限速了不允许访问了。"; | 29 | return "限速了不允许访问了。"; |
| 23 | } | 30 | } |
| 24 | 31 | ||
| 25 | public String fallback(MaxRetriesExceededException ex){ | 32 | public String fallback(MaxRetriesExceededException ex){ |
| 33 | LOG.info("重试过多,不允许访问。"); | ||
| 26 | return "重试过多,不允许访问。"; | 34 | return "重试过多,不允许访问。"; |
| 27 | } | 35 | } |
| 28 | 36 | ||
| 29 | public String fallback(Throwable ex){ | 37 | public String fallback(Throwable ex){ |
| 38 | LOG.info("服务降级了。"); | ||
| 30 | return "服务降级了。"; | 39 | return "服务降级了。"; |
| 31 | } | 40 | } |
| 32 | } | 41 | } | ... | ... |
-
Please register or sign in to post a comment