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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
简介: 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




相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
目录
相关文章
|
7月前
|
XML Dubbo Java
【Dubbo3高级特性】「框架与服务」服务的异步调用实践以及开发模式
【Dubbo3高级特性】「框架与服务」服务的异步调用实践以及开发模式
163 0
|
7月前
|
Dubbo Java 应用服务中间件
Dubbo服务暴露机制解密:深入探讨服务提供者的奥秘【九】
Dubbo服务暴露机制解密:深入探讨服务提供者的奥秘【九】
103 0
|
7月前
|
SpringCloudAlibaba Dubbo Java
SpringCloud Alibaba集成Dubbo实现远程服务间调用
SpringCloud Alibaba集成Dubbo实现远程服务间调用
|
2月前
|
监控 Dubbo Java
dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。
这篇文章详细介绍了如何将Spring Boot与Dubbo和Zookeeper整合,并通过Dubbo管理界面监控服务注册情况。
132 0
dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。
|
4月前
|
JSON Dubbo Java
【Dubbo协议指南】揭秘高性能服务通信,选择最佳协议的终极攻略!
【8月更文挑战第24天】在分布式服务架构中,Apache Dubbo作为一款高性能的Java RPC框架,支持多种通信协议,包括Dubbo协议、HTTP协议及Hessian协议等。Dubbo协议是默认选择,采用NIO异步通讯,适用于高要求的内部服务通信。HTTP协议通用性强,利于跨语言调用;Hessian协议则在数据传输效率上有优势。选择合适协议需综合考虑性能需求、序列化方式、网络环境及安全性等因素。通过合理配置,可实现服务性能最优化及系统可靠性提升。
62 3
|
4月前
|
缓存 Dubbo Java
Dubbo服务消费者启动与订阅原理
该文章主要介绍了Dubbo服务消费者启动与订阅的原理,包括服务消费者的启动时机、启动过程以及订阅和感知最新提供者信息的方式。
Dubbo服务消费者启动与订阅原理
|
4月前
|
Dubbo 网络协议 Java
深入掌握Dubbo服务提供者发布与注册原理
该文章主要介绍了Dubbo服务提供者发布与注册的原理,包括服务发布的流程、多协议发布、构建Invoker、注册到注册中心等过程。
深入掌握Dubbo服务提供者发布与注册原理
|
4月前
|
负载均衡 Dubbo Java
Dubbo服务Spi机制和原理
该文章主要介绍了Dubbo中的SPI(Service Provider Interface)机制和原理,包括SPI的基本概念、Dubbo中的SPI分类以及SPI机制的实现细节。
Dubbo服务Spi机制和原理
|
4月前
|
C# 开发者 Windows
勇敢迈出第一步:手把手教你如何在WPF开源项目中贡献你的第一行代码,从选择项目到提交PR的全过程解析与实战技巧分享
【8月更文挑战第31天】本文指导您如何在Windows Presentation Foundation(WPF)相关的开源项目中贡献代码。无论您是初学者还是有经验的开发者,参与这类项目都能加深对WPF框架的理解并拓展职业履历。文章推荐了一些适合入门的项目如MvvmLight和MahApps.Metro,并详细介绍了从选择项目、设置开发环境到提交代码的全过程。通过具体示例,如添加按钮点击事件处理程序,帮助您迈出第一步。此外,还强调了提交Pull Request时保持专业沟通的重要性。参与开源不仅能提升技能,还能促进社区交流。
48 0
|
6月前
|
Dubbo 前端开发 Java
Dubbo3 服务原生支持 http 访问,兼具高性能与易用性
本文展示了 Dubbo3 triple 协议是如何简化从协议规范与实现上简化开发测试、入口流量接入成本的,同时提供高性能通信、面向接口的易用性编码。
16645 13