Dubbo:消费端直连服务提供者

在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候可能需要点对点直连;

点对点直连方式,将以服务接口为单位,忽略注册中心的提供者列表;

A 接口配置点对点,不影响 B 接口从注册中心获取列表。

 

通过 XML 配置

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

需要dubbo1.0.6 及以上版本支持

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

完整示例:

dubbo-demo-provider.xml

提供者需要将服务暴露在某个端口上

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider"/>

    <!-- use multicast registry center to export service -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
    <dubbo:registry protocol="zookeeper" address="192.168.60.207:2181" />


    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>

</beans>
  • 启动日志
[11/01/19 02:40:11:011 CST] main  INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/dubbo-demo-provider.xml]
[11/01/19 02:40:12:012 CST] main  INFO logger.LoggerFactory: using logger: org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter
[11/01/19 02:40:13:013 CST] main  WARN extension.SpringExtensionFactory:  [DUBBO] No spring extension (bean) named:defaultCompiler, try to find an extension (bean) of type java.lang.String, dubbo version: , current host: 192.168.60.207
[11/01/19 02:40:13:013 CST] main  WARN extension.SpringExtensionFactory:  [DUBBO] No spring extension (bean) named:defaultCompiler, type:java.lang.String found, stop get bean., dubbo version: , current host: 192.168.60.207
[11/01/19 02:40:13:013 CST] main  INFO config.AbstractConfig:  [DUBBO] The service ready on spring started. service: org.apache.dubbo.demo.DemoService, dubbo version: , current host: 192.168.60.207
[11/01/19 02:40:14:014 CST] main  INFO config.AbstractConfig:  [DUBBO] Export dubbo service org.apache.dubbo.demo.DemoService to local registry, dubbo version: , current host: 192.168.60.207
[11/01/19 02:40:14:014 CST] main  INFO config.AbstractConfig:  [DUBBO] Export dubbo service org.apache.dubbo.demo.DemoService to url dubbo://192.168.60.207:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.60.207&bind.port=20880&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=11636&qos.port=22222&side=provider&timestamp=1547188813829, dubbo version: , current host: 192.168.60.207
[11/01/19 02:40:14:014 CST] main  INFO config.AbstractConfig:  [DUBBO] Register dubbo service org.apache.dubbo.demo.DemoService url dubbo://192.168.60.207:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.60.207&bind.port=20880&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=11636&qos.port=22222&side=provider&timestamp=1547188813829 to registry registry://192.168.60.207:2181/org.apache.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.2&pid=11636&qos.port=22222&registry=zookeeper&timestamp=1547188813816, dubbo version: , current host: 192.168.60.207
[11/01/19 02:40:16:016 CST] main  INFO transport.AbstractServer:  [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.60.207:20880, dubbo version: , current host: 
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\Users\ADMINI~1\AppData\Local\Temp\
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 7
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.version=6.1
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Client environment:user.name=Administrator
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Users\Administrator
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Client environment:user.dir=D:\WorkSpace\demo\dubbo\incubator-dubbo-master
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.60.207:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@6bd61f98
[11/01/19 02:40:16:016 CST] main-SendThread(192.168.60.207:2181)  INFO zookeeper.ClientCnxn: Opening socket connection to server 192.168.60.207/192.168.60.207:2181. Will not attempt to authenticate using SASL (unknown error)
[11/01/19 02:40:16:016 CST] main-SendThread(192.168.60.207:2181)  INFO zookeeper.ClientCnxn: Socket connection established to 192.168.60.207/192.168.60.207:2181, initiating session
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZookeeperRegistry:  [DUBBO] Register: dubbo://192.168.60.207:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=11636&side=provider&timestamp=1547188813829, dubbo version: , current host: 192.168.60.207
[11/01/19 02:40:16:016 CST] main-SendThread(192.168.60.207:2181)  INFO zookeeper.ClientCnxn: Session establishment complete on server 192.168.60.207/192.168.60.207:2181, sessionid = 0x1000139d01c0000, negotiated timeout = 40000
[11/01/19 02:40:16:016 CST] main-EventThread  INFO state.ConnectionStateManager: State change: CONNECTED
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZookeeperRegistry:  [DUBBO] Subscribe: provider://192.168.60.207:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=11636&side=provider&timestamp=1547188813829, dubbo version: , current host: 192.168.60.207
[11/01/19 02:40:16:016 CST] main  INFO zookeeper.ZookeeperRegistry:  [DUBBO] Notify urls for subscribe url provider://192.168.60.207:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=11636&side=provider&timestamp=1547188813829, urls: [empty://192.168.60.207:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=11636&side=provider&timestamp=1547188813829], dubbo version: , current host: 192.168.60.207
[14:44:30] Hello world, request from consumer: /192.168.60.207:62102
[14:44:32] Hello world, request from consumer: /192.168.60.207:62102
[14:44:33] Hello world, request from consumer: /192.168.60.207:62102
[14:44:34] Hello world, request from consumer: /192.168.60.207:62102
[14:44:35] Hello world, request from consumer: /192.168.60.207:62102
[14:44:36] Hello world, request from consumer: /192.168.60.207:62102
[14:44:37] Hello world, request from consumer: /192.168.60.207:62102
[14:44:38] Hello world, request from consumer: /192.168.60.207:62102
[14:44:39] Hello world, request from consumer: /192.168.60.207:62102
[14:44:40] Hello world, request from consumer: /192.168.60.207:62102
[14:44:41] Hello world, request from consumer: /192.168.60.207:62102
[14:44:42] Hello world, request from consumer: /192.168.60.207:62102
[14:44:43] Hello world, request from consumer: /192.168.60.207:62102
[14:44:44] Hello world, request from consumer: /192.168.60.207:62102
[14:44:45] Hello world, request from consumer: /192.168.60.207:62102
[14:44:46] Hello world, request from consumer: /192.168.60.207:62102
[14:44:47] Hello world, request from consumer: /192.168.60.207:62102
[14:44:48] Hello world, request from consumer: /192.168.60.207:62102

dubbo-demo-consumer.xml

去掉注册中心,在dubbo:reference配置服务者的地址和端口,注意:是dubbo协议

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer"/>

    <!-- use multicast registry center to discover service -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
    <!--<dubbo:registry protocol="zookeeper" address="192.168.60.207:2181" />-->


    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService" url="dubbo://192.168.60.207:20880"/>

</beans>
  • 启动日志
[11/01/19 02:44:29:029 CST] main  INFO transport.AbstractClient:  [DUBBO] Successed connect to server /192.168.60.207:20880 from NettyClient 192.168.60.207 using dubbo version , channel is NettyChannel [channel=[id: 0xc7e0112e, L:/192.168.60.207:62102 - R:/192.168.60.207:20880]], dubbo version: , current host: 192.168.60.207
[11/01/19 02:44:29:029 CST] main  INFO transport.AbstractClient:  [DUBBO] Start NettyClient kf-PC/192.168.60.207 connect to the server /192.168.60.207:20880, dubbo version: , current host: 192.168.60.207
[11/01/19 02:44:29:029 CST] main  INFO config.AbstractConfig:  [DUBBO] Refer dubbo service org.apache.dubbo.demo.DemoService from url dubbo://192.168.60.207:20880/org.apache.dubbo.demo.DemoService?application=demo-consumer&check=false&dubbo=2.0.2&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=11120&qos.port=33333&register.ip=192.168.60.207&side=consumer&timestamp=1547189067397, dubbo version: , current host: 192.168.60.207
Hello world, response from provider: 192.168.60.207:20880
Hello world, response from provider: 192.168.60.207:20880
Hello world, response from provider: 192.168.60.207:20880
Hello world, response from provider: 192.168.60.207:20880
Hello world, response from provider: 192.168.60.207:20880
Hello world, response from provider: 192.168.60.207:20880

通过 JVM 中-D 参数指定

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

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

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

通过文件映射

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

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

  • 查看jvm环境的user.home、file.encoding等参数,可以通过下面的命令查看:

java -XshowSettings:all -version

java -Ddubbo.resolve.file=xxx.properties

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

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

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

参考链接:http//dubbo.incubator.apache.org/zh-cn/docs/user/demos/explicit-target.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琦彦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值