增加集成Dubbo的demo
This commit is contained in:
+3
-11
@@ -1,27 +1,21 @@
|
|||||||
package com.central.log.trace;
|
package com.central.log.trace;
|
||||||
|
|
||||||
import com.central.log.properties.TraceProperties;
|
|
||||||
import org.apache.dubbo.common.constants.CommonConstants;
|
import org.apache.dubbo.common.constants.CommonConstants;
|
||||||
import org.apache.dubbo.common.extension.Activate;
|
import org.apache.dubbo.common.extension.Activate;
|
||||||
import org.apache.dubbo.rpc.*;
|
import org.apache.dubbo.rpc.*;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* dubbo过滤器,传递traceId
|
* dubbo过滤器,传递traceId
|
||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2020/10/14
|
* @date 2021/1/30
|
||||||
* <p>
|
* <p>
|
||||||
* Blog: https://zlt2000.gitee.io
|
* Blog: https://zlt2000.gitee.io
|
||||||
* Github: https://github.com/zlt2000
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER}, order = MDCTraceUtils.FILTER_ORDER)
|
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER}, order = MDCTraceUtils.FILTER_ORDER)
|
||||||
public class DubboTraceFilter implements Filter {
|
public class DubboTraceFilter implements Filter {
|
||||||
@Resource
|
|
||||||
private TraceProperties traceProperties;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务消费者:传递traceId给下游服务
|
* 服务消费者:传递traceId给下游服务
|
||||||
* 服务提供者:获取traceId并赋值给MDC
|
* 服务提供者:获取traceId并赋值给MDC
|
||||||
@@ -29,10 +23,9 @@ public class DubboTraceFilter implements Filter {
|
|||||||
@Override
|
@Override
|
||||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||||
boolean isProviderSide = RpcContext.getContext().isProviderSide();
|
boolean isProviderSide = RpcContext.getContext().isProviderSide();
|
||||||
if (traceProperties.getEnable()) {
|
|
||||||
if (isProviderSide) { //服务提供者逻辑
|
if (isProviderSide) { //服务提供者逻辑
|
||||||
String traceId = invocation.getAttachment(MDCTraceUtils.KEY_TRACE_ID);
|
String traceId = invocation.getAttachment(MDCTraceUtils.KEY_TRACE_ID);
|
||||||
if (!StringUtils.isEmpty(traceId)) {
|
if (StringUtils.isEmpty(traceId)) {
|
||||||
MDCTraceUtils.addTraceId();
|
MDCTraceUtils.addTraceId();
|
||||||
} else {
|
} else {
|
||||||
MDCTraceUtils.putTraceId(traceId);
|
MDCTraceUtils.putTraceId(traceId);
|
||||||
@@ -43,11 +36,10 @@ public class DubboTraceFilter implements Filter {
|
|||||||
invocation.setAttachment(MDCTraceUtils.KEY_TRACE_ID, traceId);
|
invocation.setAttachment(MDCTraceUtils.KEY_TRACE_ID, traceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return invoker.invoke(invocation);
|
return invoker.invoke(invocation);
|
||||||
} finally {
|
} finally {
|
||||||
if (isProviderSide && traceProperties.getEnable()) {
|
if (isProviderSide) {
|
||||||
MDCTraceUtils.removeTraceId();
|
MDCTraceUtils.removeTraceId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
## Demo说明
|
||||||
|
|
||||||
|
Spring Cloud 集成 Dubbo 的 demo
|
||||||
|
|
||||||
|
启动后访问:http://127.0.0.1:8091/test/abc
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 集成 Spring Cloud Gateway
|
||||||
|
[Dubbo想要个网关怎么办?试试整合Spring Cloud Gateway](https://mp.weixin.qq.com/s/_idYl39i1eQejLLLlCW2sg)
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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.zlt</groupId>
|
||||||
|
<artifactId>zlt-demo</artifactId>
|
||||||
|
<version>4.2.0</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>dubbo-demo</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-log-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.zlt;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2020/1/30
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class WebDubboApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(WebDubboApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.zlt.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.dubbo.config.annotation.DubboReference;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.zlt.service.RpcService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2020/6/26
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
public class WebController {
|
||||||
|
@DubboReference
|
||||||
|
private RpcService dubboService;
|
||||||
|
|
||||||
|
@GetMapping("/test/{p}")
|
||||||
|
public String test(@PathVariable("p") String param) {
|
||||||
|
log.info("==============WebController");
|
||||||
|
return dubboService.test(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.zlt.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2020/1/30
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
public interface RpcService {
|
||||||
|
String test(String param);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package org.zlt.service.impl;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.dubbo.config.annotation.DubboService;
|
||||||
|
import org.zlt.service.RpcService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2020/1/30
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@DubboService(protocol = "dubbo")
|
||||||
|
public class RpcServiceImpl implements RpcService {
|
||||||
|
@Override
|
||||||
|
public String test(String param) {
|
||||||
|
log.info("==============RpcServiceImpl");
|
||||||
|
return "dubbo service: " + param;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
dubbo:
|
||||||
|
scan:
|
||||||
|
base-packages: org.zlt.service
|
||||||
|
protocols:
|
||||||
|
dubbo:
|
||||||
|
name: dubbo
|
||||||
|
port: -1
|
||||||
|
registry:
|
||||||
|
address: spring-cloud://localhost
|
||||||
|
consumer:
|
||||||
|
timeout: 5000
|
||||||
|
check: false
|
||||||
|
retries: 0
|
||||||
|
cloud:
|
||||||
|
subscribed-services:
|
||||||
|
|
||||||
|
zlt:
|
||||||
|
trace:
|
||||||
|
enable: true
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
server:
|
||||||
|
port: 8091
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: zlt-web-dubbo
|
||||||
|
main:
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
server-addr: 192.168.28.130:8848
|
||||||
Reference in New Issue
Block a user