深入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和註解方式混合使用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章