一、Dubbo是什么
- 一款分布式服务框架
- 高性能和透明化的RPC远程服务调用方案
- SOA服务治理方案
每天为2千多个服务提供大于30亿次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点以及别的公司的业务中。
具体dubbo的背景和简介以及框架等基础知识参考这位大神的博客
二、提供者的Dubbo配置
首先我们先配置服务的提供者
1.给作为提供者的Spring项目增加依赖
需要增加zookeeper和dubbo的依赖,如下
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <!--排除传递spring依赖 --> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency>
2.序列化需要传输的java对象
如图,只要implements Serializable接口即可。
3.在resource目录下编写服务提供者的配置文件dubbo.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-b-server" /> <!--这里使用的注册中心是zookeeper --> <dubbo:registry address="zookeeper://127.0.0.1:2181"client="zkclient"/> <!--用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo"port="20880" /> <!--将该接口暴露到dubbo中 --> <dubbo:service interface="com.liu.service.BloggerService"ref="BloggerServiceImpl" /> <!--将具体的实现类加入到Spring容器中 --> <bean id="BloggerServiceImpl"class="com.liu.service.BloggerServiceImpl" /> <!--监控的配置 --> <dubbo:monitor protocol="registry"></dubbo:monitor> </beans>
其中,将需要提供的服务全部配置在文件中。
4.配置web.xml读取dubbo.xml文件,如下 (我理解:服务端不提供Controller,所以不用配置Spring MVC)
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml,claspath:dubbo.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
注:ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
一般web.xml里面都会有<context-param>这项,在其中的<param-value>增加classpath:dubbo.xml,用逗号隔开。
做完以上4个步骤,先运行zookeeper,再运行该项目即可将服务注册到注册中心上去。
三、消费者的Dubbo配置
1.创建一个maven项目
该项目必须包含相应的Service接口以及model对象,并且代码与服务的提供者相同,如图
2.编写配置文件dubbo.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-a-consumer" /> <!--这里使用的注册中心是zookeeper --> <dubbo:registry address="zookeeper://127.0.0.1:2181"client="zkclient"/> <!--从注册中心中查找服务 --> <dubbo:reference id="BlogerService"interface="com.liu.service.BloggerService"/> </beans>
将需要用到的服务配置上。
3.同样的web.xml读取dubbo.xml配置文件即可(我理解:消费端可以就配置一个Spring MVC,可以用Controller)
4.可以先通过Junit测试一下
packagecom.liu; importorg.junit.Before; importorg.junit.Test; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; importcom.liu.model.Blogger; importcom.liu.service.BloggerService; public classBlogerServiceTest { privateBloggerService bloggerService; @Before public void setUp() throwsException { ApplicationContext applicationContext = newClassPathXmlApplicationContext( "classpath:dubbo.xml"); this.bloggerService = applicationContext.getBean(BloggerService.class); } @Test public voidtestgetByUsername() { Blogger blogger=this.bloggerService.getByUsername("admin"); System.out.println(blogger.getNickname()+" "+blogger.getSign()); } }
四、查看Dubbo的提供者和消费者
假如我们又很多服务的提供者和消费者,我们怎么来管理以及查看呢?Dubbo开源组织提供了dubbo-admin这样的一个WEBUI让我们去可视化的管理服务。dubbo-admin的安装我就不具体写了,网上说最好下载源码自己打包,我为了方便直接下载了一个打包好的,放到webapps下启动tomcat就可以了。为了省事的可以访问这个链接点击打开链接
注意,不管是提供者和消费者的开发项目中,都必须引入server的接口,(提供者引用server接口用于实现,而消费者引用server接口用于多态调用),要么就是建立一个Interface接口项目作为父级pom,提供者和消费者都引入。
a.提供者-继承接口,然后实现写相应的数据库操作。
b.消费这-继承接口,然后远程rpc调用相应实现的接口@Reference。
转 :https://blog.csdn.net/qq_37841991/article/details/80501400