Skip to content

Commit

Permalink
dubbo调用访问日志
Browse files Browse the repository at this point in the history
  • Loading branch information
yunnasheng committed Jul 11, 2023
1 parent fcff0df commit 26f11d2
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
```text
.
├── README.md
├── dubbo-accesslog //dubbo调用日志
├── dubbo-async //异步调用
├── dubbo-attachment //链路调用传递隐式参数
├── dubbo-cluster-fault-tolerance//集群容错
Expand All @@ -21,7 +22,7 @@
├── dubbo-notify//远程调用触发事件通知
├── dubbo-quickstart//快速入门(DubboAPI,注解,springboot,springXML)
├── dubbo-service-downgrade//服务降级
├── dubbo-specify-pi//指定IP访问, 一致性哈希选址
├── dubbo-specify-ip//指定IP访问, 一致性哈希选址
├── dubbo-thread-pool-isolation//线程池隔离
├── dubbo-triple-reactor//响应式编程
├── dubbo-validation//参数校验
Expand Down
38 changes: 38 additions & 0 deletions dubbo-accesslog/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
41 changes: 41 additions & 0 deletions dubbo-accesslog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# dubbo调用日志
在 dubbo3 日志分为日志适配和访问日志,如果想记录每一次请求信息,可开启访问日志,类似于 apache 的访问日志。
> - 此日志量比较大,请注意磁盘容量。
## 使用场景
类似 nginx accesslog 输出等。

## 使用方式
将访问日志输出到当前应用的 log4j 日志
```xml
<dubbo:protocol accesslog="true" />
```

将访问日志输出到指定文件
```xml
<dubbo:protocol accesslog="http://10.20.160.198/wiki/display/dubbo/foo/bar.log" />
```

## 引入sl4j
```xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
</dependency>
<!--sl4j实现, 绑定System.err日志,仅打印INFO级别的日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.7</version>
<scope>compile</scope>
</dependency>
```

## 日志形式
```text
[Dubbo-framework-shared-scheduler-thread-1] INFO dubbo.accesslog.com.lb.dubbo.service.GreetingService - [DUBBO] [2023-07-11 09:09:43.99100] -> [2023-07-11 09:09:44.99500] 172.19.166.121:60804 -> 172.19.166.121:20880 - com.lb.dubbo.service.GreetingService sayHi(java.lang.String) ["testing"], dubbo version: 3.2.2, current host: 172.19.166.121
[Dubbo-framework-shared-scheduler-thread-1] INFO dubbo.accesslog.com.lb.dubbo.service.GreetingService - [DUBBO] [2023-07-11 09:09:47.00700] -> [2023-07-11 09:09:48.01100] 172.19.166.121:60804 -> 172.19.166.121:20880 - com.lb.dubbo.service.GreetingService sayHi(java.lang.String) ["testing"], dubbo version: 3.2.2, current host: 172.19.166.121
```

## 官方手册
https://cn.dubbo.apache.org/en/docs3-v2/java-sdk/advanced-features-and-usage/service/accesslog/
96 changes: 96 additions & 0 deletions dubbo-accesslog/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lb.dubbo</groupId>
<artifactId>dubbo-examples</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>dubbo-accesslog</artifactId>
<packaging>jar</packaging>

<name>Dubbo :: AccessLog</name>
<description>Dubbo 访问日志</description>

<properties>
<java.version>1.8</java.version>
<dubbo.version>3.2.2</dubbo.version>
<spring.version>5.3.25</spring.version>
<junit.version>4.13.1</junit.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<type>pom</type>
</dependency>
<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!--log-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
</dependency>
<!--简单实现的绑定/提供程序,它将所有事件输出到System.err。只打印INFO及以上级别的消息。-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
21 changes: 21 additions & 0 deletions dubbo-accesslog/src/main/java/com/lb/dubbo/AccesslogClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lb.dubbo;

import com.lb.dubbo.service.GreetingService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.locks.LockSupport;

public class AccesslogClient {

public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
context.start();

GreetingService greetingService = context.getBean(GreetingService.class);
//远程调用
greetingService.sayHi("testing");
//阻塞主线程
LockSupport.park();
}
}
17 changes: 17 additions & 0 deletions dubbo-accesslog/src/main/java/com/lb/dubbo/AccesslogServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.lb.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.locks.LockSupport;

public class AccesslogServer {

public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");
context.start();

//阻塞主线程
LockSupport.park();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lb.dubbo.service;

/**
* 功能描述: <br/>
*
* @author yunnasheng
* @date: 2020-12-30 17:06<br/>
* @since JDK 1.8
*/
public interface GreetingService {

String sayHi(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.lb.dubbo.service.impl;

import com.lb.dubbo.service.GreetingService;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

public class GreetingServiceImpl implements GreetingService {
@Override
public String sayHi(String name) {
System.out.println(LocalDateTime.now() + " Hi ===> " + name);

try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return String.format("Hi, %s ! Power by Service Downgrade Server", name);
}
}
17 changes: 17 additions & 0 deletions dubbo-accesslog/src/main/resources/dubbo-consumer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>

<!-- 应用名 -->
<dubbo:application name="echo-consumer"/>

<!-- 注册中心地址 check="false" 代表关闭注册中心启动时检查-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>

<!-- 定义订阅信息,Dubbo 会在 Spring Context 中创建对应的 bean -->
<dubbo:reference id="greetingService" check="false" interface="com.lb.dubbo.service.GreetingService" timeout="5000"/>
</beans>
27 changes: 27 additions & 0 deletions dubbo-accesslog/src/main/resources/dubbo-provider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>


<!-- 应用名 -->
<dubbo:application name="echo-provider"/>

<!-- 注册中心地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

<!--需要认证-->
<dubbo:provider token="true"/>

<!--协议 accesslog="true"访问日志-->
<dubbo:protocol name="dubbo" port="-1" accesslog="true"/>

<!-- 定义实现类对应的 bean -->
<bean id="greetingsService" class="com.lb.dubbo.service.impl.GreetingServiceImpl"/>
<!-- 定义服务信息,引用上面的 bean -->
<dubbo:service interface="com.lb.dubbo.service.GreetingService" ref="greetingsService"/>

</beans>
78 changes: 78 additions & 0 deletions dubbo-accesslog/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true
scanPeriod: 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。
-->
<configuration scan="true" scanPeriod="30 seconds" debug="false" >
<!-- 每个logger都关联到logger上下文,默认上下文名称为default。但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。 -->
<contextName>springboot-quickstart-002</contextName>
<!-- 通过<property>定义的值会被插入到logger上下文中。定义变量后,可以使"${}"来使用变量 -->
<property name="logback.path" value="/Users/yunnasheng/Desktop/logs" />
<property name="logback.level" value="INFO" />
<property name="logback.additivity" value="true" />
<!--格式化输出:%d表示日期,%t表示线程名,%-5p:级别从左显示5个字符宽度 %m:日志消息,%n是换行符-->
<property name="logback.pattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n" />
<!-- %d [%t] %-5p %c.%M[%L] - %m%n -->
<!-- %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n -->

<!--
<appender>是<configuration>的子节点,是负责写日志的组件
<appender>有两个必要属性name和class。name指定appender名称,class指定appender的全限定名。
ConsoleAppender:把日志添加到控制台,有以下子节点:
<encoder>:对日志进行格式化。
<target>:字符串 System.out 或者 System.err 默认 System.out
-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>${logback.pattern}</pattern>
</layout>
</encoder>
</appender>

<!--
FileAppender:把日志添加到文件,有以下子节点
<file>:被写入的文件名,可以是相对目录,也可以是绝对目录,如果上级目录不存在会自动创建,没有默认值。
<append>:如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true。
<encoder>:对记录事件进行格式化。
<prudent>:如果是 true,日志会被安全的写入文件,即使其他的FileAppender也在向此文件做写入操作,效率低,默认是 false。
-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名-->
<FileNamePattern>${logback.path}/springboot-mybatis.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>${logback.pattern}</pattern>
</layout>
</encoder>
</appender>

<appender name ="ANSY_FILE" class= "ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold >0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>256</queueSize>
<appender-ref ref ="FILE"/>
</appender>

<logger name="org.springframework" level="INFO"/>
<logger name="org.apache" level="ERROR"/>
<logger name="java.sql" level="ERROR"/>
<logger name="com.lb" level="DEBUG"/>

<!--
<root> 也是<loger>元素,但是它是根loger。只有一个level属性,应为已经被命名为"root"
level: 用来设置打印级别,大小写无关:TRACE < DEBUG < INFO < WARN < ERROR, ALL 和 OFF,不能设置为INHERITED或者同义词NULL。 默认是DEBUG
<loger>和<root>可以包含零个或多个<appender-ref>元素,标识这个引用的appender将会被添加到这个loger中。
-->
<root level="${logback.level}">
<appender-ref ref="STDOUT" />
<appender-ref ref="ANSY_FILE" />
</root>
</configuration>
Loading

0 comments on commit 26f11d2

Please sign in to comment.