SpringMVC与Dubbo、zookeeper的整合案例

Dubbo--zookeeper的配置安装这里就不说了网上很多,上一篇我转载的也有详情介绍
SpringMVC与 Dubbo 的整合,这边使用的 Maven 的管理项目
在pom文件中导入jar坐标
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
</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> <dependency>
    <groupId>com.zgs</groupId>
    <artifactId>zhan-service-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
一.服务及具体实现实现
@Service(" dubboServiceImpl ")
public class DubboServiceImpl implements DubboService {
@Override
public String dubboTest() {
return "dubbo执行了" ;
}
}
application-dubbo-provider.xml服务提供方配置
<? xml version ="1.0" encoding ="UTF-8" ?>
< 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: dubbo ="http://code.alibabatech.com/schema/dubbo" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi :schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd" >
<!-- 配置包扫描器,扫描所有带@Service注解的类 -->
< context :component-scan base-package ="com.zhan.*" />
<!-- 使用dubbo发布服务 4步-->
<!-- 提供方应用信息 ,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
< dubbo :application name ="dubbo-provide" />
<!-- 在zookeeper注册中心 中注册服务 protocol协议
Address:地址 多个地址,用逗号 隔开 -->
< dubbo :registry protocol ="zookeeper" address ="localhst:2181" />
<!--<dubbo:registry address="N/A" /> 可以在开发时使用下 -->
<!-- 用dubbo协议 在20880端口暴露服务 -->
< dubbo :protocol name ="dubbo" port ="20882" />
<!-- 主机端口
<dubbo:protocol host="127.0.0.1" port="20880"/>-->
<!-- 声明需要暴露的服务接口 -->
< dubbo :service interface ="com.zhan.service.DubboService" ref ="dubboService" />
</ beans >
说明:
   dubbo:registry  标签一些属性的说明:
      1 register 是否向此注册中心注册服务,如果设为 false ,将只订阅,不注册
      2 check注册中心不存在时,是否报错。
      3 subscribe 是否向此注册中心订阅服务,如果设为 false ,将只注册,不订阅
      4 timeout 注册中心请求超时时间 ( 毫秒 )。
      5 address 可以 Zookeeper 集群配置 ,地址可以多个以逗号隔开等。
  dubbo:service 标签的一些属性说明:
     1 interface服务接口的路径
     2 ref引用对应的实现类的Bean ID
     3 registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry> id 属性,多个注册中心 ID 用逗号分隔,如果不想将该服务注册到任何 registry ,可将值设为 N/A
     4 register 默认true ,该协议的服务是否注册到注册中心。

二.消费方也要在pom 导入上面的那些jar坐标
@Controller
public class DubboController {
@Autowired
private DubboService dubboService ;
@RequestMapping ( "aa" )
public void dubboTest(){
String s = dubboService .dubboTest();
System. out .print(s);
}
}
application-dubbo-consumer.xml服务消费方配置
<? xml version ="1.0" encoding ="UTF-8" ?>
< 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: dubbo ="http://code.alibabatech.com/schema/dubbo" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi :schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd" >

<!-- 应用dubbo服务 -->
<!-- 消费方应用名, 不要和提供方同名,用于计算依赖关系 -->
< dubbo :application name ="zhan-consumer" />
<!-- 使用zookeeper注册服务中心 发现暴露服务地址-->
< dubbo :registry protocol ="zookeeper" address ="localhst:2181" />
<!-- <dubbo:registry address="N/A" /> -->
<!-- 调用暴露接口:生成远程代理服务对象 和 本地 bean一样 -->
< dubbo :reference interface ="com.zhan.service.DubboService" id ="dubboService" />
<!-- 使用debug调试 会连接超时 check='false' 启动时不校验服务提供方 -->
< dubbo :consumer timeout ="600000" check ="false" />
</ beans >
说明:
   dubbo:reference  的一些属性的说明:
      1 interface调用的服务接口
      2 check 启动时检查提供者是否存在,true 报错, false 忽略
      3 registry  从指定注册中心注册获取服务列表,在多个注册中心时使用,值为 <dubbo:registry> id 属性,多个注册中心 ID 用逗号分隔
      4 loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive ,分别表示:随机,轮循,最少活跃调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值