spring集成dubbo

1.dubbo的架构

Registry:使用zookeeper

Provider:接口提供方

Consumer:接口调用方

1.2.启动zookeeper

1.3.查看服务器ip

2.新建providerweb工程 

2.1,导入pom依赖

<!-- dubbo相关 -->
< dependency >
   < groupId >com.alibaba</ groupId >
   < artifactId >dubbo</ artifactId >
   < version >2.5.3</ version >
   < exclusions >
     < exclusion >
       < groupId >org.springframework</ groupId >
       < artifactId >spring</ artifactId >
     </ exclusion >
     < exclusion >
       < groupId >org.jboss.netty</ groupId >
       < artifactId >netty</ artifactId >
     </ exclusion >
   </ exclusions >
</ dependency >
< dependency >
   < groupId >org.apache.zookeeper</ groupId >
   < artifactId >zookeeper</ artifactId >
   < version >3.4.7</ version >
</ dependency >
< dependency >
   < groupId >com.github.sgroschupf</ groupId >
   < artifactId >zkclient</ artifactId >
   < version >0.1</ version >
</ dependency >
<!--4:spring依赖-->
<!--1)spring核心依赖-->
< dependency >
   < groupId >org.springframework</ groupId >
   < artifactId >spring-core</ artifactId >
   < version >4.1.7.RELEASE</ version >
</ dependency >
< dependency >
   < groupId >org.springframework</ groupId >
   < artifactId >spring-beans</ artifactId >
   < version >4.1.7.RELEASE</ version >
</ dependency >
< dependency >
   < groupId >org.springframework</ groupId >
   < artifactId >spring-context</ artifactId >
   < version >4.1.7.RELEASE</ version >
</ dependency >
<!--2)spring dao层依赖-->
< dependency >
   < groupId >org.springframework</ groupId >
   < artifactId >spring-jdbc</ artifactId >
   < version >4.1.7.RELEASE</ version >
</ dependency >
< dependency >
   < groupId >org.springframework</ groupId >
   < artifactId >spring-tx</ artifactId >
   < version >4.1.7.RELEASE</ version >
</ dependency >

2,2 提供一个接口

2.3 spring 配置文件中配置声明发布的接口以及应用名称,端口zookeeper地址

<? xml  version = "1.0"  encoding = "UTF-8" ?>
< beans  xmlns = "<a href="http://www.springframework.org/schema/beans" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/beans"
        xmlns:context = "<a href="http://www.springframework.org/schema/context" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/context"  xmlns:p = "<a href="http://www.springframework.org/schema/p" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/p"
        xmlns:aop = "<a href="http://www.springframework.org/schema/aop" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/aop"  xmlns:tx = "<a href="http://www.springframework.org/schema/tx" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/tx"
        xmlns:dubbo = "<a href="http://code.alibabatech.com/schema/dubbo" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://code.alibabatech.com/schema/dubbo"  xmlns:xsi = "<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.w3.org/2001/XMLSchema-instance"
    http://www.springframework.org/schema/util <a href="http://www.springframework.org/schema/util/spring-util-4.2.xsd" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 0, 0) !important;">http://www.springframework.org/schema/util/spring-util-4.2.xsd">
 
         < context:component-scan  base-package = "com.niezheng.service.provider" />
 
         <!-- 使用dubbo发布服务 -->
         <!-- 提供方应用信息,用于计算依赖关系 -->
         < dubbo:application  name = "dubbo-provider"  />
         < dubbo:registry  protocol = "zookeeper"
                         address = "192.168.56.100:2181"  />
         <!-- 用dubbo协议在20880端口暴露服务 -->
         < dubbo:protocol  name = "dubbo"  port = "20880"  />
         <!-- 声明需要暴露的服务接口 -->
         < dubbo:service  interface = "com.niezheng.service.provider.IDubboProvider"  ref = "dubboProviderImpl"  />
</ beans >

2.4.启动工程

3.新建consumer工程

3.1导入pom依赖

<!-- dubbo相关 -->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>dubbo</artifactId>
   <version> 2.5 . 3 </version>
   <exclusions>
     <exclusion>
       <groupId>org.springframework</groupId>
       <artifactId>spring</artifactId>
     </exclusion>
     <exclusion>
       <groupId>org.jboss.netty</groupId>
       <artifactId>netty</artifactId>
     </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.apache.zookeeper</groupId>
   <artifactId>zookeeper</artifactId>
   <version> 3.4 . 7 </version>
</dependency>
<dependency>
   <groupId>com.github.sgroschupf</groupId>
   <artifactId>zkclient</artifactId>
   <version> 0.1 </version>
</dependency>
<!-- 4 :spring依赖-->
<!-- 1 )spring核心依赖-->
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version> 4.1 . 7 .RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version> 4.1 . 7 .RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version> 4.1 . 7 .RELEASE</version>
</dependency>
<!-- 2 )spring dao层依赖-->
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version> 4.1 . 7 .RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version> 4.1 . 7 .RELEASE</version>
</dependency>

3.2 spring配置文件

<?xml version= "1.0"  encoding= "UTF-8" ?>
<beans xmlns= "<a href="http://www.springframework.org/schema/beans" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/beans"
        xmlns:xsi= "<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.w3.org/2001/XMLSchema-instance"  xmlns:p= "<a href="http://www.springframework.org/schema/p" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/p"
        xmlns:context= "<a href="http://www.springframework.org/schema/context" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/context"
        xmlns:dubbo= "<a href="http://code.alibabatech.com/schema/dubbo" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://code.alibabatech.com/schema/dubbo"
        xmlns:mvc= "<a href="http://www.springframework.org/schema/mvc" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
         http: //www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
         http: //code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
         http: //www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-4.2.xsd" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 130, 0) !important;">http://www.springframework.org/schema/context/spring-context-4.2.xsd">
     <!-- 引用dubbo服务 -->
     <dubbo:application name= "dubbo-consumer" />
     <dubbo:registry protocol= "zookeeper"  address= "192.168.56.100:2181" />
     <dubbo:reference  interface = "com.niezheng.service.provider.IDubboProvider"  id= "dubboProviderImpl" ></dubbo:reference>
 
</beans>

注:

 <dubbo:reference interface="com.niezheng.service.provider.IDubboProvider" id="dubboProviderImpl"></dubbo:reference>

这里的接口名称要和dubbo提供方的要一致 ,消费方要有对应的接口如下

3.3 调用dubbo接口

3.4.访问调用接口 

http://localhost:8080/test/dubbo

4.通过dubbo-admin查看提供者和调用者

4.1下载dbuuo-admin :http://dubbo.io/

4.2 将admin打包成war包

进入到dubbo-admin目录下 打开DOS窗口执行:mvn package -Dmaven.skip.test=true

4.3 使用xftp将war包部署到服务器

4.4.注意修改dubbo.properties  

4.5 如果dubbo和zookeeper在同一台服务器上则不用修改 不在同一台机器上要修改zookeeper地址

4.6 启动tomcat

4.6  访问dubbo-admin

可以看见dubbo的消费者和提供者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一组用于构建分布式系统的开源框架,它为我们提供了一种轻量级的微服务架构的解决方案。而Dubbo是阿里巴巴开源的高性能RPC框架,用于构建分布式服务框架。而Feign是Spring Cloud提供的一种声明式的Web服务客户端,它可以与Dubbo进行集成,提供更加便捷的服务调用方式。 在集成Dubbo和Feign之前,我们需要先将Dubbo注册到Spring Cloud的注册中心,使得Dubbo服务可以被Spring Cloud所管理。这样,我们就可以使用Feign作为Spring Cloud中的服务消费者来调用Dubbo提供的服务。通过Feign,我们可以避免编写繁琐的服务调用代码,只需要简单的声明式接口,并使用注解来定义具体的服务调用方法,Feign会动态生成代理实现类,自动处理服务调用的细节。 在使用Feign集成Dubbo时,我们需要在Spring Boot配置文件中进行相应的配置,声明需要调用的Dubbo服务接口。然后,通过在代码中定义Feign的接口,使用注解来标识调用的Dubbo服务地址、服务方法等。当我们调用该接口时,Feign会自动发起对Dubbo服务的远程调用。 通过将Feign与Dubbo集成,我们可以通过Spring Cloud的整体架构来统一管理和调用Dubbo的服务,简化了代码编写和服务调用的过程。同时,Feign还具有负载均衡、容错等一系列特性,可以进一步提高系统的稳定性和可靠性。 综上所述,通过集成Dubbo和Feign,我们可以更加方便地在Spring Cloud中调用和管理Dubbo的服务,提高系统的可维护性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值