服务的调用方式多种多样,从一开始的webservice(基于SOAP)提供wsdl的方式, 再好比EJB,RMI,restful等,每种服务在当时都有其特定的使用价值,可是随着架构体系的升级,技术的发展单单是实现远程通讯是远远不够的。这个时候可能就须要使用服务治理。 服务治理可能要求:
注册中心
、链路跟踪
、通讯异常处理
、负载均衡
等java
为何使用dubbo,由于他可以知足服务治理的要求 。web
dubbo是一种RPC框架。 那么RPC框架所须要具有的基本功能:网络通讯(服务调用)、序列化/反序列化、动态代理(serviceA->serviceB的方式改成了serviceA直接经过RPC调用ServiceB那么确定会存在代理)、负载均衡、容错等spring
一个服务serviceA->调用服务serviceB 那么经过这个RPC框架已经把须要作的这一系列例如: 动态代理->序列化/反序列化->网络通讯等操做给封装好, 至关于在本地调用服务那样直接使用。
<!-- 服务端的XML --> <?xml version="1.0" encoding="UTF-8"?> <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.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 应用名,惟一 --> <dubbo:application name="pay-provider" /> <!--注册中心地址--> <dubbo:registry address="N/A"/> <!--服务提供的方式和端口,(可选),由于会默认提供地址--> <dubbo:protocol name="dubbo" port="20880"/> <!--服务提供的接口--> <dubbo:service interface="xxx.IPayService" ref="payService"/> <!--接口实现--> <bean class="xxx.provider.PayServiceImpl" id="payService"/> </beans>
// 启动 ClassPathXmlApplicationContext loader = new ClassPathXmlApplicationContext(new String[]{"application.xml"}); loader.start(); System.in.read();
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.3</version> </dependency>
dubbo://0.0.0.0:20880/xxx.api.IPayService?anyhost=true&application=provider&bean.name=xxx.IPayService&bind.ip=0.0.0.0&bind.port=20880&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=xxx.IPayService&methods=doPay&pid=5676®ister=true&release=2.7.3&side=provider×tamp=1570123868318, dubbo version: 2.7.3, current host: 0.0.0.0
同理客户端的配置:apache
<?xml version="1.0" encoding="UTF-8"?> <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.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 应用名,惟一 --> <dubbo:application name="pay-client" /> <!--注册中心地址--> <dubbo:registry address="N/A"/> <!--服务提供的接口--> <dubbo:reference id="payService" interface="xxx.IPayService" url="dubbo://0.0.0.0:20880/xxx.IPayService"/> </beans>
ClassPathXmlApplicationContext loader = new ClassPathXmlApplicationContext(new String[]{"application.xml"}); loader.start(); IPayService payService = (IPayService)loader.getBean("payService"); payService.doPay();