深入dubbo内核(2):注解式配置dubbo服务

dubbo注解式配置
版本:2.5.8

  • 服务提供方配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "
>

<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="demo-provider"/>
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="10.0.74.174:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- xml配置 -->
<!--和本地bean一样实现服务-->
<!--<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>-->
<!--声明需要暴露的服务接口-->
<!--<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>-->
<!-- 提供方注解式配置 -->
<!--<context:component-scan base-package="com.alibaba.dubbo.demo.provider"/>-->
<dubbo:annotation package="com.alibaba.dubbo.demo.provider"/>
</beans>

服务提供方注解式配置非常简单,只需增加 @< dubbo:annotation />注解,扫描要暴露的服务即可,无需其他配置。多个包下的服务可以采取多次扫描配置的形式。

  • 提供方服务类
package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.demo.DemoService;
@Service
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name + " " + DemoServiceImpl.class.getSimpleName();
}
}

在要暴露的服务类上增加service注解即可暴露服务。但是要注意的是这里的service注解和spring的注解并不是同一个而是com.alibaba.dubbo.config.annotation.Service.

  • 服务消费方配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.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.xsd"
>

<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="demo-consumer"/>
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="10.0.74.174:2181"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<!--<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>-->
<dubbo:annotation package="com.alibaba.dubbo.demo.consumer"/>
<context:component-scan base-package="com.alibaba.dubbo.demo.consumer"/>
</beans>

服务消费方配置同样使用@< dubbo:annotation />注解扫描消费方实现类。但是要增加spring包扫描将其置于spring容器下管理(不增加会无法注入,报nullPointException).

  • 消费方实现类
package com.alibaba.dubbo.demo.consumer;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.stereotype.Component;
/**
* Created by ken.lj on 2017/7/31.
*/

@Component
public class Consumer {
private static DemoService demoService;
@Reference
public void setDemoService(DemoService demoService) {
Consumer.demoService = demoService;
}
public static void sayHello() {
while (true) {
try {
Thread.sleep(1000);
String hello = demoService.sayHello("world"); // call remote method
System.out.println(hello); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}

消费方实现类一定要加org.springframework.stereotype.Component注解。
注入的提供方服务通过com.alibaba.dubbo.config.annotation.Reference注解进行注入。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Reference {
...
}

@Reference是作用在方法和属性上的,所以一般静态注入使用set方法进行注入,而普通注入则直接作用在属性上即可。

  • @Reference和@Service的个性化配置
  1. 服务的分组
    服务分组和xml配置基本类似。当一个服务有多个实现类的时候同于group属性区分,但是在消费方和服务方必须保持一致否则将无法注入。
@Service(group = "demo1")
public class DemoServiceImpl1 implements DemoService {
public String sayHello(String name) {
return "Hello " + name + " " + DemoServiceImpl1.class.getSimpleName();
}
}
@Service(group = "demo")
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name + " " + DemoServiceImpl.class.getSimpleName();
}
}
  1. 版本号和服务分组一样,消费方和服务方必须保持一致否则将无法注入。
@Service(group = "demo",version = "1.0")
public class DemoServiceImpl1 implements DemoService {
public String sayHello(String name) {
return "Hello " + name + " " + DemoServiceImpl1.class.getSimpleName();
}
}
@Service(group = "demo1",version = "1.0")
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name + " " + DemoServiceImpl.class.getSimpleName();
}
}

消费方和服务方要保持一致。

  1. timeout属性配置超时时间。
  2. retries集群容错,缺省为0。
  3. 其他属性类似于启动时检查,注册中心等不再介绍,但是建议采用xml和注解方式混合使用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值