我们现在来学习下发布Dubbo服务,主要参考dubbo开发包里的demo源码;由浅入深的讲解下这个小demo;
详细视频教程,请看 http://www.java1234.com/a/yuanchuang/dubbo/
首先创建一个maven项目dubbo-demo-provider
pom.xml加入依赖:
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.0.35.Final</version> </dependency> </dependencies>
然后定义一个服务接口:
package com.java1234.service; /** * 服务提供者接口 * @author Administrator * */ public interface DemoProviderService { public String sayHello(String name); }
再定义接口实现类:
package com.java1234.service.impl; import com.java1234.service.DemoProviderService; /** * 服务提供者接口实现类 * @author Administrator * */ public class DemoProviderServiceImpl implements DemoProviderService{ public String sayHello(String name) { return "服务员001"; } }
然后再搞个dubbo配置文件dubbo-demo-provider.xml:
<?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" 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"> <!-- 提供方应用名称, 用于计算依赖关系 --> <dubbo:application name="demo-provider"/> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 使用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- service实现类作为本地的一个bean --> <bean id="demoProviderService" class="com.java1234.service.impl.DemoProviderServiceImpl"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.java1234.service.DemoProviderService" ref="demoProviderService"/> </beans>
测试类:
import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ProviderTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-provider.xml"}); context.start(); System.out.println("服务提供者注册成功(端口:20880)"); try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } context.close(); } }
然后我们测试发布dubbo服务,
首先我们要先启动zookeeper服务,
然后我们运行测试类,发布服务注册到zookeeper注册中心去;
说明发布服务OK;
后面我们会讲到dubbo-admin 有web管理系统可以看到具体的服务信息;
上一篇:zookeeper安装
下一篇:Dubbo Admin管理控制台