Commit 0c3334dd by hweeeeeei

完成Dubbo指定ip调用

parent 0a5d9918
package io.geekidea.springbootplus.test;
import com.wecloud.im.router.RouterSendService;
import com.wecloud.im.ws.model.WsResponse;
import com.wecloud.utils.JsonUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.cluster.router.address.Address;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* dubbo指定ip调用测试类
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class DubboRpcTest {
/*
* 解决log日志依赖冲突问题
*/
static {
System.setProperty("nacos.logging.default.config.enabled", "false");
}
/*
* 指定ip调用,router=address;
* injvm = false要设置成false,否则会调用到本地提供者
*/
@DubboReference(injvm = false, interfaceClass = RouterSendService.class, parameters = {"router", "address"})
private RouterSendService routerSendService;
@Test
public void test() {
int i = 0;
WsResponse wsResponse = new WsResponse();
wsResponse.setCmd(i);
wsResponse.setCode(200);
wsResponse.setMsg("i=" + i);
wsResponse.setData("hihi");
wsResponse.setReqId("123");
String msgJson = JsonUtils.encodeJson(wsResponse);
// 根据provider的ip,port创建Address实例
Address address = new Address("192.168.1.51", 20881);
RpcContext.getContext().setObjectAttachment("address", address);
routerSendService.sendMsgRemote(123L, 1, msgJson);
}
}
spring:
profiles:
active: @profileActive@
---
spring:
profiles: dev
cloud:
nacos:
discovery:
server-addr: 192.168.1.89:8848
userName: "nacos"
password: "nacos"
config:
server-addr: 192.168.1.89:8848
userName: "nacos"
password: "nacos"
file-extension: yaml
---
spring:
profiles: test
cloud:
nacos:
discovery:
server-addr: localhost:8848
userName: "nacos"
password: "nacos"
config:
server-addr: localhost:8848
userName: "nacos"
password: "nacos"
file-extension: yaml
---
spring:
profiles: prod
cloud:
nacos:
discovery:
server-addr: localhost:8848
userName: "nacos"
password: "nacos"
config:
server-addr: localhost:8848
userName: "nacos"
password: "nacos"
file-extension: yaml
\ No newline at end of file
package com.wecloud.im.ws.sender;
import com.wecloud.im.executor.SendMsgThreadPool;
import com.wecloud.utils.GetIpUtils;
import com.wecloud.im.router.RouterSendService;
import com.wecloud.im.ws.cache.UserStateCacheManager;
import com.wecloud.im.ws.manager.ChannelManager;
......@@ -9,6 +8,7 @@ import com.wecloud.im.ws.model.ClientInfo;
import com.wecloud.im.ws.model.WsResponse;
import com.wecloud.im.ws.model.redis.ClientChannelInfo;
import com.wecloud.im.ws.model.request.ReceiveVO;
import com.wecloud.utils.GetIpUtils;
import com.wecloud.utils.JsonUtils;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
......@@ -38,7 +38,11 @@ public class ChannelSender {
@Autowired
private UserStateCacheManager userStateCacheManager;
@DubboReference(parameters = {"router", "address"})
/*
* 指定ip调用,router=address;
* injvm = false要设置成false,否则会调用到本地提供者
*/
@DubboReference(injvm = false, interfaceClass = RouterSendService.class, parameters = {"router", "address"})
private RouterSendService routerSendService;
/**
......@@ -104,6 +108,8 @@ public class ChannelSender {
batchSendMsgLocal(toClientIdAndPlatforms, msgJson);
} else {
String msgJson = JsonUtils.encodeJson(responseModel);
// dubbo指定ip调用
Address address = new Address(toIp, 20881);
RpcContext.getContext().setObjectAttachment("address", address);
routerSendService.batchSendMsgRemote(toClientIdAndPlatforms, msgJson);
......@@ -139,6 +145,7 @@ public class ChannelSender {
// RpcContext.getContext().set("ip", channelInfo.getLanIp());
// 根据provider的ip,port创建Address实例
for(ClientChannelInfo clientChannelInfo : channelInfoEntry.getValue()) {
// dubbo指定ip调用
Address address = new Address(clientChannelInfo.getLanIp(), 20881);
RpcContext.getContext().setObjectAttachment("address", address);
routerSendService.sendMsgRemote(toClientId, clientChannelInfo.getPlatform(), msgJson);
......
......@@ -343,60 +343,55 @@
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<!-- 排除自带的logback依赖 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.11</version>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo</artifactId>-->
<!-- <version>${dubbo.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
<!-- <version>2.2.5.RELEASE</version>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>com.alibaba.spring</groupId>-->
<!-- <artifactId>spring-context-support</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.5.RELEASE</version>
<exclusions>
<!-- 排除自带的logback依赖 -->
<exclusion>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.5.RELEASE</version>
<exclusions>
<!-- 排除自带的logback依赖 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- rabbitmq -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-amqp</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>net.logstash.logback</groupId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment