基于Dubbo协议的项目示例及详细讲解(二)

来源:互联网 发布:linux oracle rpm安装 编辑:程序博客网 时间:2024/05/21 19:46

上一篇文章中完成了接口模块,本文完成服务提供者模块。

1、同接口模块一样,创建一个quickstart基本结构的maven项目并删除自动生成的App.java类。选中项目右键->properties->Java Build Path->Libraries->Add External JARS引入导出的接口jar包DubboInterface.jar.

2、在pom.xml文件中增加Spring(只增加Spring用到的核心依赖)、Dubbo和zookeeper客户端的依赖。

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>
3.2.9.RELEASE</version>
</dependency>

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>dubbo</artifactId>
   <version>2.5.3</version>
</dependency>
    
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId> 
<version>0.5</version>
<exclusions>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion> 
</exclusions>
</dependency>

3、创建一个DubboProvider.java类,实现ISayHelloInterface接口,对外提供服务。

package com.song.demo.DubboProvider.implement;

import org.springframework.stereotype.Service;
import com.song.demo.DubboInterface.ISayHelloInterface;
@Service//使用注解注入
public class DubboProvider implements ISayHelloInterface {

public String sayHello(String name) {
//返回信息给消费者
return "Provider接收到信息了,你好,"+name;
}
}

4、配置spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee" 
xsi:schemaLocation="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-3.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-3.2.xsd   
http://www.springframework.org/schema/jee   
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 
http://www.springframework.org/schema/tx   
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesMode" value="2"/>
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:env.properties</value>
</list>
</property>
</bean>
<!-- 开启注解 -->
<context:annotation-config />
    <context:component-scan base-package="com.song.demo.DubboProvider" />
    <!-- 提供方应用信息 -->
    <dubbo:application name="dubboprovider"></dubbo:application>
    <!-- 使用zookeepeer暴露服务地址 (还可以通过multicast广播注册中心暴露服务地址)-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务地址 -->
    <dubbo:protocol accesslog="/data/logs/access.log"  name="dubbo" server="netty" host="127.0.0.1" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.song.demo.DubboInterface.ISayHelloInterface" protocol="dubbo" ref="dubboProvider"version="1.0.0"/>
</beans>

5、配置dubbo.properties文件(这个文件在下一步的启动类中使用到)

#需要启动的容器
dubbo.container=spring
#查看dubbo文档,Dubbo是通过JDK的ShutdownHook来完成优雅停机的:
#http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%E4%BC%98%E9%9B%85%E5%81%9C%E6%9C%BA
#但能实现优雅停机的前提是,在启动时,需要指定参数-Ddubbo.shutdown.hook=true

dubbo.shutdown.hook=true
#dubbo是基于spring的,指定spring配置文件
dubbo.spring.config=spring.xml

6、启动类

package com.song.demo.DubboProvider;

public class StartDubboProvider1 {
public static void main(String[] args) {
System.out.println("DubboProvider starting...");
//通过classpath下的dubbo.properties配置传入要加载的容器,加载容器有多种方式设置
com.alibaba.dubbo.container.Main.main(args);
}
}

至此,Provider服务提供者模块完成了,启动zookeeper服务器后(监听2181端口,默认配置就是),运行Provider的启动类,就可以注册到zookeeper并对外提供服务了。启动后控制台输出如下表示成功:


整个Provider模块的目录结构如下:


0 0