温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring boot 集成Dubbox的方法示例

发布时间:2020-09-02 21:55:29 来源:脚本之家 阅读:216 作者:Raye Blog 栏目:编程语言

前言

因为工作原因,需要在项目中集成dubbo,所以去查询dubbo相关文档,发现dubbo目前已经不更新了,所以把目光投向了dubbox,dubbox是当当网基于dubbo二次开发的一个项目,dubbox,因为公司项目中一个是基于spring mvc 3.0的,一个是基于spring boot的,而spring boot相对来说文档少一点,所以此文记录下spring boot下如何继承dubbox

一、安装zookeeper

1、zookeeper简介

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户

而dubbo就是依赖zookeeper的一个分布式框架,当然二次开发的dubbox肯定也会依赖于zookeeper,所以我们需要先安装zookeeper

2、下载zookeeper

zookeeper官网地址http://zookeeper.apache.org/

下载地址http://apache.fayea.com/zookeeper/

本地下载地址,更加方便:https://www.jb51.net/softs/578345.html

3、安装zookeeper

因为我是在Windows环境配置的,所以就简单说一下windows下面的配置吧,首先解压压缩包,然后进入conf文件夹,复制一下zoosample.cfg创建副本,然后重命名为zoo.cfg,因为zookeeper只识别zoo.cfg,而默认是没有这个文件的,zoosample.cfg是默认的配置文件,但是因为文件名的原因,所以zookeeper无法识别,当然直接重命名zoo_sample.cfg也是可以的,只是看自己喜欢咯

4、启动zookeeper

Windows环境下直接运行bin目录下的zkServer.cmd就可以,如果是Linux环境,则在bin目录下运行

./zkServer.sh start

命令,就可以启动zookeeper

5、添加dubbox依赖 

  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>dubbo</artifactId>
   <version>2.8.4</version>
  </dependency>
  <dependency>
   <groupId>com.101tec</groupId>
   <artifactId>zkclient</artifactId>
   <version>0.9</version>
  </dependency>
  <dependency>
   <groupId>org.apache.zookeeper</groupId>
   <artifactId>zookeeper</artifactId>
   <version>3.4.6</version>
   <exclusions>
    <exclusion>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-log4j12</artifactId>
    </exclusion>
    <exclusion>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
    </exclusion>
   </exclusions>
  </dependency>

注意,这里的dubbo的2.8.4版本是我自己编译的,这个在maven仓库是没有的,因为dubbo已经不更新了,而2.8.4是当当网的dubbox,所以如果你要使用,要么就编译dubbox,要么就使用旧版本的dubbo

6、添加服务提供者的接口 接口类:

package wang.raye.dubbo.interfaces;
public interface DubboInterface {
 public String hello(String name);
}

接口实现类:

package wang.raye.dubbodemo1;
import org.springframework.stereotype.Service;
import wang.raye.dubbo.DubboInterface; 
@Service
public class DubboImpl implements DubboInterface {
 public String hello(String name) {
  return "hello "+name+" this is dubbodemo1";
 }

}

7、添加dubbo配置xml xml文件放在sources文件夹,名字可以随便命名,我这里是dubbo.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:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
 default-lazy-init="false" >
 <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->  
 <dubbo:application name="dubbo-provider1"></dubbo:application> 
 <!--使用注解方式暴露接口
 <dubbo:annotation package="wang.raye.dubbodemo1" /> -->
 <!-- 使用zookeeper注册中心暴露服务地址 --> 
 <dubbo:registry address="zookeeper://192.168.1.126:2181" check="false" subscribe="false" register=""></dubbo:registry>
 <!-- 要暴露的服务接口-->
 <dubbo:service interface="wang.raye.dubbo.interfaces.DubboInterface" ref="dubboImpl" /> 
</beans> 

这里每个节点都有解释了,相信不过过多解释

8、配置消费者

类中引用远程提供者

package wang.raye.dubbodemo3.controller;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody;
import wang.raye.dubbo.DubboInterface;
@Controller
public class DubboControll {

 @Autowired
 private DubboInterface interface1;
 @RequestMapping("/hello")
 @ResponseBody
 public String hello(String name){
  return interface1.hello(name);
 }
}

消费者的xml配置

<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="dubbo-custom-dsp"/> 
 <dubbo:registry address="zookeeper://192.168.1.126:2181" /> 
 <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> 
 <dubbo:reference id="dubboService" interface="wang.raye.dubbo.interfaces.DubboInterface" /> 
</beans> 

这里也是每个接点都有注释,应该不用多说,而且比较本文不是讲究dubbo配置的,主要是讲spring boot集成dubbox

9、引用dubbo配置xml 在Spring boot的Application类添加注解

@ImportResource({"classpath:dubbo.xml"})

因为我的xml名字是dubbo.xml,所以当你用的时候需要换成自己的xml名字,注意:消费者项目和服务提供者的项目的Application类都需要配置此注解

结尾

至此,spring boot集成duubox就说完了,当然这样说肯定很空洞,所以我吧我测试的项目上传到了github,大家可以参考看看,当然要测试的话需要修改dubbo.xml的dubbo:registry 节点中的zookeeper地址。

demo地址,为了显示出dubbo的优势,所以我创建了2个服务提供者,频繁调用的话,会自动选择这2个中的任何一个,连负载均衡都免了,应该是zookeeper自己做了,所以感觉还是挺不错的

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI