Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Support dubbo 2.7 #433

Merged
merged 16 commits into from
Jan 22, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void run(String... args) throws Exception {
}

protected void installBiz(String bizDir) throws Throwable {
String pathRoot = "rpc/dubbo26/";
String pathRoot = "samples/dubbo-samples/rpc/dubbo26/";
File bizFile = new File(pathRoot + bizDir);
if (bizFile.exists()) {
File tmpFile = new File(pathRoot + "target/" + bizFile.getName());
Expand Down
201 changes: 201 additions & 0 deletions samples/dubbo-samples/rpc/dubbo27/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# Dubbo 2.7.x 在模块中使用
## 基座新增依赖
base 为普通 dubbo 应用改造而成,改造内容只需在主 pom 里增加如下依赖
```xml
<!--覆盖dubbo 2.7同名类,一定要放在dubbo的依赖前面-->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-adapter-dubbo2.7</artifactId>
<version>0.5.6-SNAPSHOT</version>
</dependency>
<!--sofa serverless 依赖-->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-base-starter</artifactId>
</dependency>
<!--如果是 web 应用,并且希望后面模块部署与基座使用同一个 tomcat host,则引入如下依赖。详细查看[这里](https://www.sofastack.tech/projects/sofa-boot/sofa-ark-multi-web-component-deploy/)-->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>web-ark-plugin</artifactId>
</dependency>
<!-- 通信类-->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>dubbo27model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
```

这里基座发布了 RPC 服务
```xml
<bean id="demoService" class="com.alipay.sofa.rpc.dubbo27.base.service.MasterDemoServiceImpl"/>
<dubbo:service interface="com.alipay.sofa.rpc.dubbo27.model.DemoService" ref="demoService" group="masterBiz"/> <!-- 和本地bean一样实现服务 -->
```

## 模块中使用
用例提供了 biz 和 biz2 两个模块动态安装到基座JVM中,也是普通 dubbo 应用,进行如下改造即可变成可合并部署的 ark biz 模块

### 1.修改模块打包插件
```xml
<plugin>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-maven-plugin</artifactId>
<version>2.2.3</version>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<skipArkExecutable>true</skipArkExecutable>
<outputDirectory>./target</outputDirectory>
<!--biz和biz2必须声明不同的bizName-->
<bizName>biz</bizName>
<!--biz和biz2必须声明不同的webContextPath-->
<webContextPath>/biz</webContextPath>
<declaredMode>true</declaredMode>
</configuration>
</plugin>
```
### 2. 添加依赖
另外模块还额外将基座里有的依赖,设置为了 provided,这样可以尽可能的复用基座的spring、dubbo依赖等。通信类 dubbo27model 不用 provided,让biz类加载器自己加载
```xml
<!--提供 log4j2 适配,提供基座、模块日志隔离,不需要可以不添加-->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-adapter-log4j2</artifactId>
<version>${sofa.serverless.runtime.version}</version>
<scope>provided</scope>
</dependency>
<!--提供 dubbo2.7 适配-->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-adapter-dubbo2.7</artifactId>
<version>${sofa.serverless.runtime.version}</version>
<scope>provided</scope>
</dependency>
<!-- 通信类-->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>dubbo27model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
```
### 3. 声明dubbo服务和引用
biz1声明了三个rpc服务:
> biz1/com.alipay.sofa.rpc.dubbo27.model.DemoService
>
> biz1Second/com.alipay.sofa.rpc.dubbo27.model.DemoService
>
> biz1/com.alipay.sofa.rpc.dubbo27.model.HelloService

三个rpc引用,remote调用biz2发布的rpc服务
> biz2/com.alipay.sofa.rpc.dubbo27.model.DemoService
>
> biz2Second/com.alipay.sofa.rpc.dubbo27.model.DemoService
>
> biz2/com.alipay.sofa.rpc.dubbo27.model.HelloService

服务发布与引用配置如下:
```xml
<bean id="demoService" class="com.alipay.sofa.rpc.dubbo27.biz.service.BizDemoServiceImpl"/>
<dubbo:service interface="com.alipay.sofa.rpc.dubbo27.model.DemoService" ref="demoService" group="biz1"/>

<bean id="secondDemoService" class="com.alipay.sofa.rpc.dubbo27.biz.service.SecondDemoServiceImpl"/>
<dubbo:service interface="com.alipay.sofa.rpc.dubbo27.model.DemoService" ref="secondDemoService" group="biz1Second"/>

<bean id="helloService" class="com.alipay.sofa.rpc.dubbo27.biz.service.HelloServiceImpl"/>
<dubbo:service interface="com.alipay.sofa.rpc.dubbo27.model.HelloService" ref="helloService" group="biz1"/>

<dubbo:reference id="demoServiceRef" interface="com.alipay.sofa.rpc.dubbo27.model.DemoService" scope="remote" group="biz2" check="false"/>
<dubbo:reference id="secondDemoServiceRef" interface="com.alipay.sofa.rpc.dubbo27.model.DemoService" scope="remote" group="biz2Second" check="false"/>
<dubbo:reference id="helloServiceRef" interface="com.alipay.sofa.rpc.dubbo27.model.HelloService" scope="remote" group="biz2" check="false"/>
```

类似的,biz2声明了三个rpc服务:
> biz2/com.alipay.sofa.rpc.dubbo27.model.DemoService
>
> biz2Second/com.alipay.sofa.rpc.dubbo27.model.DemoService
>
> biz2/com.alipay.sofa.rpc.dubbo27.model.HelloService

三个rpc引用,remote调用biz1发布的rpc服务
> biz1/com.alipay.sofa.rpc.dubbo27.model.DemoService
>
> biz1Second/com.alipay.sofa.rpc.dubbo27.model.DemoService
>
> biz1/com.alipay.sofa.rpc.dubbo27.model.HelloService


### 运行代码
1. 【重要】首次运行,先编译安装下用于通信包
```shell
cd samples/dubbo-samples/rpc/dubbo27/dubbo27model
mvn clean install
```

2. 进入目录`samples/dubbo-samples/rpc/dubbo27` 编译打包模块的代码
```shell
cd ../
mvn clean package
```
3. 启动基座应用Dubbo26BaseApplication.java,为了方便本地测试用,启动基座时,默认也启动模块
```java
public static void run(String... args) throws Exception {
try {
installBiz("dubbo27biz/target/dubbo27biz-0.0.1-SNAPSHOT-ark-biz.jar");
installBiz("dubbo27biz2/target/dubbo27biz2-0.0.1-SNAPSHOT-ark-biz.jar");
} catch (Throwable e) {
LOGGER.error("Install biz failed", e);
}
}
```
也可以用curl、telnet、arkctl等部署工具手动安装,此处不再赘述。确保基座和模块启动成功。
4. 查看模块安装是否成功
```shell
curl --location --request POST 'localhost:1238/queryAllBiz'
```
可以查看到所有安装好的模块列表

5. 验证模块的RPC调用

模块biz远程调用biz2发布的dubbo服务(因为有dubbo网络调用,执行前请关闭vpn,否则可能出现调用超时)
```shell
curl localhost:8080/biz/
{"result":"bizcom.alipay.sofa.rpc.dubbo27.biz2.service.BizDemoServiceImpl"}

curl "localhost:8080/biz/?ref=second"
{"result":"biz->com.alipay.sofa.rpc.dubbo27.biz2.service.SecondDemoServiceImpl"}

curl localhost:8080/biz/hello
{"data":"null->com.alipay.sofa.rpc.dubbo27.biz2.service.HelloServiceImpl"}
```

模块biz2远程调用biz发布的dubbo服务
```shell
curl localhost:8080/biz2/
{"result":"biz2->com.alipay.sofa.rpc.dubbo27.biz.service.BizDemoServiceImpl"}

curl "localhost:8080/biz2/?ref=second"
{"result":"biz2->com.alipay.sofa.rpc.dubbo27.biz.service.SecondDemoServiceImpl"}

curl localhost:8080/biz2/hello
{"data":"null->com.alipay.sofa.rpc.dubbo27.biz.service.HelloServiceImpl"}
```

6. 验证基座的 RPC 调用自己

基座rpc调用自己发布的dubbo服务
```shell
curl localhost:8080
{"result":"base->com.alipay.sofa.rpc.dubbo27.base.service.MasterDemoServiceImpl"}
```

### 说明
因为有dubbo网络调用,执行前请关闭vpn,否则可能出现调用超时。

dubbo2.7 暂时只支持java序列化,不支持热部署能力,如有需要请提一个issue告诉我们。

15 changes: 10 additions & 5 deletions samples/dubbo-samples/rpc/dubbo27/dubbo27base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<artifactId>sofa-serverless-base-starter</artifactId>
<version>${sofa.serverless.runtime.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-adapter-dubbo2.7</artifactId>
<version>${sofa.serverless.runtime.version}</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
Expand All @@ -52,11 +57,11 @@
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-adapter-dubbo27</artifactId>
<version>0.5.1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.alipay.sofa.serverless</groupId>-->
<!-- <artifactId>sofa-serverless-adapter-dubbo27</artifactId>-->
<!-- <version>0.5.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import java.io.File;

import com.alipay.sofa.ark.api.ArkClient;
import com.alipay.sofa.rpc.dubbo27.base.controller.MasterController;
import com.alipay.sofa.rpc.dubbo27.model.DemoResponse;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;

/**
*
Expand All @@ -22,9 +27,13 @@
*/

@SpringBootApplication
public class Dubb27BaseApplication implements CommandLineRunner {
@ImportResource("classpath:provider.xml")
public class Dubb27BaseApplication {
private static Logger LOGGER = LoggerFactory.getLogger(Dubb27BaseApplication.class);

// @Autowired
// private MasterController masterController;

public static void main(String[] args) {

//Prevent to get IPV6 address,this way only work in debug mode
Expand All @@ -35,16 +44,26 @@ public static void main(String[] args) {
System.setProperty("sofa.ark.embed.enable", "true");
System.setProperty("sofa.ark.plugin.export.class.enable", "true");

SpringApplication.run(Dubb27BaseApplication.class, args);
ConfigurableApplicationContext context = SpringApplication.run(Dubb27BaseApplication.class, args);
MasterController controller = context.getBean(MasterController.class);
DemoResponse xxx = controller.handle("xxx");
System.out.println(xxx.getResult());

System.out.println("start to deploy biz");
try {
run(args);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Install biz when base started
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
// @Override
public static void run(String... args) throws Exception {
try {
installBiz("dubbo27biz/target/dubbo27biz-0.0.1-SNAPSHOT-ark-biz.jar");
installBiz("dubbo27biz2/target/dubbo27biz2-0.0.1-SNAPSHOT-ark-biz.jar");
Expand All @@ -53,7 +72,7 @@ public void run(String... args) throws Exception {
}
}

protected void installBiz(String bizDir) throws Throwable {
protected static void installBiz(String bizDir) throws Throwable {
String pathRoot = "samples/dubbo-samples/rpc/dubbo27/";
File bizFile = new File(pathRoot + bizDir);
if (bizFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alipay.sofa.rpc.dubbo27.base.controller;

import com.alipay.sofa.rpc.dubbo27.model.DemoRequest;
import com.alipay.sofa.rpc.dubbo27.model.DemoResponse;
import com.alipay.sofa.rpc.dubbo27.model.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* @author: yuanyuan
* @date: 2023/12/22 4:12 下午
*/
@RestController
public class MasterController {

@Resource
private DemoService demoServiceRef;

@Autowired
private ApplicationContext applicationContext;

@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public DemoResponse handle(String ref) {
String appName = applicationContext.getId();
DemoRequest demoRequest = new DemoRequest();
demoRequest.setBiz(appName);
return demoServiceRef.handle(demoRequest);
}

public void setDemoServiceRef(DemoService demoServiceRef) {
this.demoServiceRef = demoServiceRef;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alipay.sofa.rpc.dubbo27.base.service;

import com.alipay.sofa.rpc.dubbo27.model.DemoRequest;
import com.alipay.sofa.rpc.dubbo27.model.DemoResponse;
import com.alipay.sofa.rpc.dubbo27.model.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

/**
* @author: yuanyuan
* @date: 2023/12/22 11:39 上午
*/
public class MasterDemoServiceImpl implements DemoService {

@Autowired
private ApplicationContext applicationContext;

@Override
public DemoResponse handle(DemoRequest demoRequest) {
DemoResponse response = new DemoResponse();
response.setResult(demoRequest.getBiz() + "->" + getClass().getName());
return response;
}
}
Loading