Dubbo中的点对点通信(指定IP)

dubbo本身是通过负载均衡策略来控制自己的调度,但是在实际生产过程中消费者可能需要指定IP

如下是官方文档中2.7中给出的实现方法,但是dubbo3.0中已经没有Address的方法,优先选择2.7实现,3.0的方法还不确定如何实现:

// 需要依赖的class
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.cluster.router.address.Address;
    
@RestController
public class TestServiceConsumer {
    @DubboReference(interfaceClass = TestService.class,group = "dev",parameters = {"router","address"})
    private TestService testService;

   @GetMapping("/invokeByIpPortSpecified")
       public String invokeByIp(){
           try {
               // 根据provider的ip,port创建Address实例
               Address address = new Address("10.220.47.253", 20880);
               RpcContext.getContext().setObjectAttachment("address", address);
               return testService.sayHello("Tom");
           }catch (Throwable ex){
               return ex.getMessage();
           }
       }
}

 除此之外如果使用了【标签路由规则】(路由规则 | Apache Dubbo)的情况下,IP需要放在标签规则的上面且需要带上标签路由规则

Address address = new Address(ip, Integer.parseInt(port));
RpcContext.getContext().setObjectAttachment("address", address);
RpcContext.getContext().setObjectAttachment(CommonConstants.TAG_KEY, env.getTag());
DubboService.commandExecute(this.input);

你可能感兴趣的:(网络,http,网络协议,微服务)