项目使用Spring+Dubbo+ZooKeeper,使用Maven管理。
先看项目结构,dubbo-api存放需要调用的接口,dubbo-provider实现接口并且暴露接口,dubbo-consumer是接口的调用方。
项目依赖
<dependencies>
<!--Log Start-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--Log End-->
<!--Spring Start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring End-->
<!--Dubbo Start-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!--Dubbo End-->
<!--ZooKeeper Start-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--ZooKeeper End-->
</dependencies>
首先在dubbo-api中定义一个接口DemoService
package com.hqq.service.impl.service;
/**
* Package: com.hqq.service
* User: 何芊芊
* Date: 2017/12/22
* Time: 10:25
* Description:
*/
public interface DemoService {
String echName(String name);
}
在dubbo-provider中实现该接口(依赖api模块)
package com.hqq.service.impl;
import com.hqq.service.impl.service.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Package: com.hqq.service.impl
* User: 何芊芊
* Date: 2017/12/22
* Time: 10:28
* Description:
*/
public class DemoServiceImpl implements DemoService {
private Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
public String echName(String name) {
logger.debug("======DemoServiceImpl echName=====" + name);
return name;
}
}
编写dubbo配置文件
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
<dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
<dubbo:service interface="com.hqq.service.impl.service.DemoService" ref="demoService" protocol="dubbo" />
<!--具体实现该接口的 bean-->
<bean id="demoService" class="com.hqq.service.impl.DemoServiceImpl"/>
</beans>
启动provider端
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Package: PACKAGE_NAME
* User: 何芊芊
* Date: 2017/12/22
* Time: 10:32
* Description:
*/
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-dubbo.xml");
System.out.println(ac.getDisplayName());
ac.start();
System.out.println("程序已启动……");
System.in.read();
}
}
运行结果:
org.springframework.context.support.ClassPathXmlApplicationContext@1ee2c5
程序已启动……
在consumer中调用该接口
package com.hqq.demo;
import com.hqq.service.impl.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Package: com.hqq.demo
* User: 何芊芊
* Date: 2017/12/22
* Time: 10:43
* Description:
*/
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-dubbo-client.xml");
context.start();
System.out.println("Consumer Start");
DemoService demoService = context.getBean(DemoService.class);
System.out.println("Consumer");
System.out.println(demoService.echName("heqianqian"));
}
}
配置文件如下:
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/>
<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
<dubbo:reference id="permissionService" interface="com.hqq.service.impl.service.DemoService"/>
</beans>
调用结果
Consumer Start
Consumer
heqianqian
在dubbo-admin平台上查看
关于dubbo-admin的配置请看Dubbo admin管理平台搭建
项目源码 dubbo-demo