首页 > 编程知识 正文

dubbo菜鸟教程,dubbo实现

时间:2023-05-03 08:49:56 阅读:179865 作者:1421

摘要Dubbo是阿里巴巴SOA服务化管理方案的核心框架,每天为2,000个服务提供3,000,000,000,000次访问支持,广泛应用于阿里巴巴集团各成员网站。

自开源以来,已经有很多非阿里系公司在使用Dubbo。 请参阅:已知用户

那么,什么是Dubbo? dubbo|db|是一个分布式服务框架,致力于提供高性能和透明的RPC远程服务调用和SOA服务治理方法。 其核心部分包括:

远程通信:提供了基于长连接的各种NIO框架的抽象封装,包括线程模型、序列化和请求-响应模式。 集群容错:提供基于接口的透明远程过程调用,例如多协议支持、软负载均衡、故障容错、地址路由和动态配置服务消费者动态搜索服务提供商,使地址透明,使服务提供商能够顺利增减机器。 Dubbo能做什么? 透明的远程方法调用只需调用和配置远程方法,就像调用本地方法一样,没有API入侵。 通过软负载均衡和容错机制,可以在内部网代替F5等硬件负载分散机,从而降低成本,减少单点。 服务将自动注册并被发现。 不需要写服务提供商的地址。 注册中心可以根据接口名称查询服务提供商的IP地址,从而顺利添加或删除服务提供商。

示例DEMO从代码级通过简单的DEMO介绍了如何使用dubbo开发远程通信服务。 要快速开始下载源代码,请参见Github。 更详细、更复杂的使用方法请参考用户指南。 更多详情请参考官方用户指南。 http://dubbo.io/user-guide/preface/background.htm,在本例中,您将学习以下内容:

定义远程服务接口提供程序并将远程服务发布到注册中心consumer自动检测远程服务并完成服务调用

开始前请注意:

例如,使用Dubbo推荐的Spring配置方法。 要使用API配置,请参阅API配置示例,以使用多播模式自动注册和发现服务。 在生产环境中,通常要部署单独的注册中心,首先部署dubbo的jar包。 由于dubbo由maven管理,因此pom.xml的导入部分如下。 ----dubbo---- dependencygroupidcom.Alibaba/groupidartifactiddubbo/artifactidversion2.0. 13/version/dependenden artifactidversion3.3.6/versionexclusionsexclusiongroupidlog 4j/g

roupId><artifactId>log4j</artifactId></exclusion></exclusions></dependency>

如果采用zookeeper来进行注册服务,就需要引入该jar,否则不需要。下文用的是multicast多播方式,所以不需要。 定义接口

定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
DemoService.java

package com.alibaba.dubbo.demo;public interface DemoService {    String sayHello(String name);}
Provider实现

服务提供方实现接口(对服务消费方隐藏实现):
DemoServiceImpl.java

package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.demo.DemoService;public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; }}

用Spring配置声明暴露服务:
dubbo-demo-provider.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="demo-provider" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" /></beans>

加载Spring配置:
Provider.java

import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/dubbo-demo-provider.xml"}); context.start(); System.in.read(); // 按任意键退出 }}
Consumer实现

通过Spring配置引用远程服务:
dubbo-demo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="demo-consumer" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /></beans>

加载Spring配置,并调用远程服务:(也可以使用IoC注入)
Consumer.java

import org.springframework.context.support.ClassPathXmlApplicationContext;import com.alibaba.dubbo.demo.DemoService;public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/dubbo-demo-consumer.xml"}); context.start(); DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理 String hello = demoService.sayHello("world"); // 执行远程方法 System.out.println( hello ); // 显示调用结果 }}


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。