提交 30b87fdb 编写于 作者: zlt2000's avatar zlt2000

增加集成Dubbo的demo

上级 880b599b
package com.central.log.trace;
import com.central.log.properties.TraceProperties;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
/**
* dubbo过滤器,传递traceId
*
* @author zlt
* @date 2020/10/14
* @date 2021/1/30
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER}, order = MDCTraceUtils.FILTER_ORDER)
public class DubboTraceFilter implements Filter {
@Resource
private TraceProperties traceProperties;
/**
* 服务消费者:传递traceId给下游服务
* 服务提供者:获取traceId并赋值给MDC
......@@ -29,10 +23,9 @@ public class DubboTraceFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
boolean isProviderSide = RpcContext.getContext().isProviderSide();
if (traceProperties.getEnable()) {
if (isProviderSide) { //服务提供者逻辑
String traceId = invocation.getAttachment(MDCTraceUtils.KEY_TRACE_ID);
if (!StringUtils.isEmpty(traceId)) {
if (StringUtils.isEmpty(traceId)) {
MDCTraceUtils.addTraceId();
} else {
MDCTraceUtils.putTraceId(traceId);
......@@ -43,11 +36,10 @@ public class DubboTraceFilter implements Filter {
invocation.setAttachment(MDCTraceUtils.KEY_TRACE_ID, traceId);
}
}
}
try {
return invoker.invoke(invocation);
} finally {
if (isProviderSide && traceProperties.getEnable()) {
if (isProviderSide) {
MDCTraceUtils.removeTraceId();
}
}
......
## 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)
<?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>
\ No newline at end of file
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);
}
}
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);
}
}
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);
}
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;
}
}
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
\ No newline at end of file
server:
port: 8091
spring:
application:
name: zlt-web-dubbo
main:
allow-bean-definition-overriding: true
cloud:
nacos:
server-addr: 192.168.28.130:8848
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册