Spring MVC与Dubbo的整合一

摘要:
--排除传递spring依赖--˃springorg.springframework2.序列化需要传输的java对象如图,只要implementsSerializable接口即可。--提供方应用信息,用于计算依赖关系--˃其中,将需要提供的服务全部配置在文件中。

一、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对象

Spring MVC与Dubbo的整合一第1张

如图,只要implements Serializable接口即可。

3.在resource目录下编写服务提供者的配置文件dubbo.xml

Spring MVC与Dubbo的整合一第2张

<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对象,并且代码与服务的提供者相同,如图

Spring MVC与Dubbo的整合一第3张

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

免责声明:文章转载自《Spring MVC与Dubbo的整合一》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux设置tomcat开机启动C++ STL map使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android 自己实现更新下载自动安装

1、一些公司开发完一款App之后可能并不会去上架App商店,但事后期也需要定时进行维护更新,所以会选择把打包好的apk 发布到自己的服务器,然后在数据库建一个版本号的表,然后剩下的就交给你android开发了,android自己要实现版本检测更新,由于android自带的DownloadManager 就可以实现下载功能,用起来就会很简单了,不用再写很多下...

canvas基础绘制矩形(1)

1、canvas基础知识 canvas元素是HTML5中新增的一个重要的元素,专门用来绘制图形,不过canvas本身不具备画图的能力,在页面中放置了canvas元素,就相当于在页面中放置了一块矩形的“画布”,我们可以利用js脚本在“画布”上绘制图形。 1.1canvas元素  在利用canvas绘制图形之前,我们首先需要在页面中放置一个canvas元素,...

Android中事件监听示例

监视来电状态AudioFocus是Android2.2以后才有的功能,对于比2.2低得版本,用的是另一种方法,就是监听电话的状态。最起码在电话打进来是能够暂停音乐的播放。实现这一功能的第一步是在AndroidManifest.xml中声明用于接收PHONE_STATE通知的receiver<receiver android:name=".PhoneS...

Opentracing 链路追踪

在微服务架构的系统中,请求在各服务之间流转,调用链错综复杂,一旦出现了问题和异常,很难追查定位,这个时候就需要链路追踪来帮忙了。链路追踪系统能追踪并记录请求在系统中的调用顺序,调用时间等一系列关键信息,从而帮助我们定位异常服务和发现性能瓶颈。 Opentracing Opentracing是分布式链路追踪的一种规范标准,是CNCF(云原生计算基金会)下的项...

普通Java类获取Spring的bean traceofsun的专栏 博客频道 CSDN.NET

普通Java类获取Spring的bean - traceofsun的专栏 - 博客频道 - CSDN.NET 普通Java类获取Spring的bean 分类:Extjs开发2010-08-13 09:47451人阅读评论(0)收藏举报 在SSH集成的前提下。某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象...

IdentitiServser4 + Mysql实现Authorization Server

 Identity Server 4官方文档:https://identityserver4.readthedocs.io/en/latest/ 新建2个asp.net core 项目使用空模板 Auth 身份认证服务 Client客户端 Auth项目打开安装相关依赖  IdentityServer4 和 EF 实体框架 Mysql EF Provid...