Dubbo直接连接服务提供者

本文将介绍在Dubbo中怎样绕过注册中心,直接连接到服务提供者。

在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候可能需要点对点直连,点对点直联方式,将以服务接口为单位,忽略注册中心的提供者列表,A 接口配置点对点,不影响 B 接口从注册中心获取列表。直连有三种方式,下面将分别介绍:

通过 XML 配置

如果是线上需求需要点对点,可在 dubbo:reference 中配置 url 指向提供者,将绕过注册中心,多个地址用分号隔开,配置如下 :

<dubbo:reference id="xxxService" interface="com.alibaba.xxx.XxxService" 
                url="dubbo://localhost:20890" />

实例:通过在 <dubbo:reference> 元素中通过 url 属性指定服务地址。

dubbo-consumer1.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo="https://code.alibabatech.com/schema/dubbo"
      xmlns="https://www.springframework.org/schema/beans"
      xsi:schemaLocation="https://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         https://code.alibabatech.com/schema/dubbo
         https://code.alibabatech.com/schema/dubbo/dubbo.xsd">

   <!-- 应用名称 -->
   <dubbo:application name="demo-consumer"/>

   <!-- 注册中心 -->
   <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

   <!-- 通过 url 属性配置该服务直接连接到某个提供者,绕过注册中心 -->
   <dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService"
      url="dubbo://localhost:20880"/>

</beans>

Consumer1.java源码:

package com.huangx.dubbo.consumer;

import com.huangx.dubbo.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * 测试通过 XML 文件 <dubbo:reference> 元素的 url 属性指定服务直连到那个提供者
 */
public class Consumer1 {

   public static void main(String[] args) throws Exception {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer1.xml"});
      context.start();

      // 获取远程服务
      HelloService demoService = (HelloService) context.getBean("helloService");
      while(true) {
         System.out.println( demoService.sayHello("Bill") );
         Thread.sleep(1000);
      }
   }

}

通过 -D 参数指定

在 JVM 启动参数中加入 -D 参数映射服务地址 ,如:

java -Dcom.alibaba.xxx.XxxService=dubbo://localhost:20890

实例:下面演示通过-D参数为某个服务指定dubbo的暴露地址。

dubbo-consumer2.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo="https://code.alibabatech.com/schema/dubbo"
      xmlns="https://www.springframework.org/schema/beans"
      xsi:schemaLocation="https://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         https://code.alibabatech.com/schema/dubbo
         https://code.alibabatech.com/schema/dubbo/dubbo.xsd">

   <!-- 应用名称 -->
   <dubbo:application name="demo-consumer"/>

   <!-- 注册中心 -->
   <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

   <!--  -->
   <dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" />

</beans>

Consumer2.java源码:

/**
 * 通过 java -D 指定某个服务直连到服务提供者
 *
 * 假如我们这里将 com.huangx.dubbo.service.HelloService 服务直连到 dubbo://localhost:20880 提供者
 *
 * java -Dcom.huangx.dubbo.service.HelloService=dubbo://localhost:20880
 *
 */
public class Consumer2 {

   public static void main(String[] args) throws Exception {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer2.xml"});
      context.start();

      // 获取远程服务
      HelloService demoService = (HelloService) context.getBean("helloService");
      while(true) {
         System.out.println( demoService.sayHello("Bill") );
         Thread.sleep(1000);
      }
   }

}

通过文件映射

如果服务比较多,也可以用文件映射,用 -Ddubbo.resolve.file 指定映射文件路径,此配置优先级高于 dubbo:reference 中的配置 ,如:

java -Ddubbo.resolve.file=xxx.properties

然后在映射文件 xxx.properties 中加入配置,其中 key 为服务名,value 为服务提供者URL:

com.alibaba.xxx.XxxService=dubbo://localhost:20890

注意:为了避免复杂化线上环境,不要在线上使用这个功能,只应在测试阶段使用。

  • 1.0.6 及以上版本支持 

  • key 为服务名,value 为服务提供者 url,此配置优先级最高, 1.0.15 及以上版本支持 

  • 1.0.15 及以上版本支持,2.0 以上版本自动加载 ${user.home}/dubbo.resolve.properties 文件,不需要配置

实例:通过下面实例演示通过 -Ddubbo.resolve.file 指定映射属性文件(自定义和默认dubbo.resolve.properties文件)。

dubbo-consumer2.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo="https://code.alibabatech.com/schema/dubbo"
      xmlns="https://www.springframework.org/schema/beans"
      xsi:schemaLocation="https://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         https://code.alibabatech.com/schema/dubbo
         https://code.alibabatech.com/schema/dubbo/dubbo.xsd">

   <!-- 应用名称 -->
   <dubbo:application name="demo-consumer"/>

   <!-- 注册中心 -->
   <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

   <dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" />

</beans>

Consumer3.java源码:

package com.huangx.dubbo.consumer;

import com.huangx.dubbo.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * 如果你需要映射的服务太多,则可以将服务和提供者之间的映射放入到properties文件中。然后通过 java -D指定映射文件。如下:
 *
 * 方式一:使用绝对路径
 * java -Ddubbo.resolve.file=D:\learn\Dubbo\workspaces\dubbo-learn\dubbo-demo04\dubbo-consumer04\target\classes\dubbo-direct.properties
 *
 * 方式二:使用默认文件 dubbo-resolve.properties
 * 将 dubbo-resolve.properties 文件放置到当前用户主目录下面,如:C:\Users\Administrator。输出的日志信息如下:
 * [09/04/19 10:42:28:028 CST] main  WARN config.AbstractConfig:  [DUBBO] Using default dubbo resolve file C:\Users\Administrator\dubbo-resolve.properties replace com.huangx.dubbo.service.HelloServicedubbo://localhost:20880 to p2p invoke remote service., dubbo version: 2.5.3, current host: 127.0.0.1
 */
public class Consumer3 {

   public static void main(String[] args) throws Exception {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer2.xml"});
      context.start();

      // 获取远程服务
      HelloService demoService = (HelloService) context.getBean("helloService");
      while(true) {
         System.out.println( demoService.sayHello("Bill") );
         Thread.sleep(1000);
      }
   }

}
生活总会给你答案的,但不会马上把一切都告诉你。只要你肯等一等,生活的美好,总在你不经意的时候,盛装莅临。
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号