使用annnotation方式接入dubbo的操作说明

dubbo依赖放在common包中,需版本升级为1.0.1-SNAPSHOT

 

<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.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.6</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions> </dependency>


====================服务端====================

0、项目分包,分离接口与实现类,对外服务接口统一放在facade包里。

(web,facade,biz(dao+service),integration,test)
1、项目配置java类添加annotation:
@ImportResource("classpath:dubbo-server-config.xml")

env.properties文件追加以下配置项
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
2、
dubbo-server-config.xml文件内容:
    <dubbo:application name="${app.name}" />
    <dubbo:registry address="${dubbo.registry.address}" />
    <dubbo:annotation package="...." />
    <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />

3、服务实现类添加@Service,如下示例:
@com.alibaba.dubbo.config.annotation.Service(
        version = "1.0.1",
        interfaceClass = EmailSenderFacade.class,
        timeout = 3000,
        delay = -1)
@Component
public class EmailSenderService implements EmailSenderFacade {
    //...
}

====================消费端====================
1、项目配置java类添加annotation:
@ImportResource("classpath:dubbo-consumer-config.xml")
env.properties文件追加以下配置项
dubbo.registry.address=zookeeper://127.0.0.1:2181

2、
dubbo-consumer-config.xml文件内容:
    <dubbo:application name="${app.name}" />
    <dubbo:registry address="${dubbo.registry.address}" />
    <dubbo:annotation package="..." />

3、服务消费配置,如下示例:

@Reference(version="1.0.1")
private EmailSenderFacade emailSenderFacade;

emailSenderFacade.send...

相关软件安装
e:
cd E:\apache-activemq-5.12.0-bin\apache-activemq-5.12.0\bin
activemq.bat start

d:


dubbo服务监控应用:dubbo-admin-2.5.3.war,放在tomcat app目录下,改名为dubbo-admin
war包中自带有dubbo.properties,修改配置
dubbo.registry.address=zookeeper://localhost:2181
dubbo.admin.root.password=root

 

 

启动zookeeper,dubbo-admin:

 

cd D:\zookeeper-3.4.6\zookeeper-3.4.6\bin\ zkServer.cmd

cd E:\apache-tomcat-6.0.44-x64\bin
startup.bat

 

root/root访问http://localhost:9090/dubbo-admin/governance/services 查看发布的服务
上一篇:【从零单排】详解 Log4j + Slf4j 等日志框架(下篇)


下一篇:如何在Eclipse中搭建MyBatis基本开发环境?(使用Eclipse创建Maven项目)