Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
李墨
/
spring-boot-resilience4j-demo
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
a0cfa949
authored
2022-07-29 19:53:58 +0800
by
李墨
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[fix]:修复日志打印
1 parent
48cd3c63
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
32 additions
and
4 deletions
pom.xml
src/main/java/com/seektruth/demo/spring/boot/resilience4j/BootStrap.java
src/main/java/com/seektruth/demo/spring/boot/resilience4j/aspect/OperateLogAspect.java
src/main/java/com/seektruth/demo/spring/boot/resilience4j/controller/BaseController.java
src/main/resources/application.yml
pom.xml
View file @
a0cfa94
...
...
@@ -48,6 +48,13 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
<!-- 日志组件 -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-logging
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-configuration-processor
</artifactId>
...
...
src/main/java/com/seektruth/demo/spring/boot/resilience4j/BootStrap.java
View file @
a0cfa94
package
com
.
seektruth
.
demo
.
spring
.
boot
.
resilience4j
;
import
com.seektruth.demo.spring.boot.resilience4j.config.RegistryEventBeanConfig
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.ApplicationEvent
;
...
...
@@ -13,12 +16,13 @@ import org.springframework.context.event.ContextClosedEvent;
*/
@SpringBootApplication
public
class
BootStrap
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
BootStrap
.
class
);
public
static
void
main
(
String
[]
args
)
{
ConfigurableApplicationContext
ctx
=
SpringApplication
.
run
(
BootStrap
.
class
,
args
);
boolean
running
=
ctx
.
isRunning
();
if
(
running
){
System
.
out
.
println
(
"启动已启动"
);
LOG
.
info
(
"启动已启动"
);
}
}
}
...
...
src/main/java/com/seektruth/demo/spring/boot/resilience4j/aspect/OperateLogAspect.java
View file @
a0cfa94
...
...
@@ -7,10 +7,12 @@ import org.aspectj.lang.annotation.Pointcut;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.core.Ordered
;
import
org.springframework.stereotype.Component
;
/**
* 操作日志切面
*/
@Component
@Aspect
public
class
OperateLogAspect
implements
Ordered
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
OperateLogAspect
.
class
);
...
...
@@ -18,7 +20,7 @@ public class OperateLogAspect implements Ordered {
/**
* 设置切入点:横切所有 controller 类的所有方法
*/
@Pointcut
(
"execution(* com.seektruth.demo.spring.boot.resil
l
ience4j.controller.*.*(..))"
)
@Pointcut
(
"execution(* com.seektruth.demo.spring.boot.resilience4j.controller.*.*(..))"
)
public
void
operateLogPointcut
()
{
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
}
...
...
@@ -35,8 +37,10 @@ public class OperateLogAspect implements Ordered {
Object
[]
args
=
joinPoint
.
getArgs
();
LOG
.
info
(
"========================================"
);
LOG
.
info
(
" 操作日志打印:调用 {} {} 方法,请求参数:{}"
,
className
,
methodName
,
args
);
Object
result
=
joinPoint
.
proceed
(
args
);
LOG
.
info
(
" 操作日志打印:{} {} 方法,返回结果:{}"
,
result
);
LOG
.
info
(
"========================================"
);
return
joinPoint
.
proceed
(
args
)
;
return
result
;
}
@Override
...
...
src/main/java/com/seektruth/demo/spring/boot/resilience4j/controller/BaseController.java
View file @
a0cfa94
package
com
.
seektruth
.
demo
.
spring
.
boot
.
resilience4j
.
controller
;
import
com.seektruth.demo.spring.boot.resilience4j.BootStrap
;
import
io.github.resilience4j.bulkhead.BulkheadFullException
;
import
io.github.resilience4j.circuitbreaker.CallNotPermittedException
;
import
io.github.resilience4j.ratelimiter.RequestNotPermitted
;
import
io.github.resilience4j.retry.MaxRetriesExceededException
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* 降级方法
*/
public
class
BaseController
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
BaseController
.
class
);
public
String
fallback
(
CallNotPermittedException
ex
){
LOG
.
info
(
"服务已熔断。"
);
return
"服务已熔断。"
;
}
public
String
fallback
(
BulkheadFullException
ex
){
LOG
.
info
(
"服务并发限制,不允许访问。"
);
return
"服务并发限制,不允许访问。"
;
}
public
String
fallback
(
RequestNotPermitted
ex
){
LOG
.
info
(
"限速了不允许访问了。"
);
return
"限速了不允许访问了。"
;
}
public
String
fallback
(
MaxRetriesExceededException
ex
){
LOG
.
info
(
"重试过多,不允许访问。"
);
return
"重试过多,不允许访问。"
;
}
public
String
fallback
(
Throwable
ex
){
LOG
.
info
(
"服务降级了。"
);
return
"服务降级了。"
;
}
}
...
...
src/main/resources/application.yml
View file @
a0cfa94
...
...
@@ -185,4 +185,8 @@ resilience4j.TimeLimiter:
eventConsumerBufferSize
:
10
instances
:
timelimiterInstance
:
baseConfig
:
base
\ No newline at end of file
baseConfig
:
base
# 日志信息
logger
:
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment