dubbo項目基本結構以下,分別部署於不一樣服務器:java
1.provider(接口API 實現)git
2.consumer(web)github
3.zookeeperweb
4.DBspring
provider構建api
1.api構建服務器
intellij中新建"maven"項目,groupId:com.dubbo.api.demo,artifactId:demo,projectName:dubbo-api-demo();app
java下添加interface:cmo.dubbo.api.demo.DemoService;maven
public interface DemoService { String sayHello(String name); }
maven-installide
2.provider 實現構建
pom.xml中添加DUBBO-API,DUBBO,SPRING
<dependencies> <dependency> <groupId>com.dubbo.api.demo</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.16.RELEASE</version> </dependency> </dependencies>
intellij中新建"maven"項目,groupId:com.dubbo.provider.demo,artifactId:demo,projectName:dubbo-provider-demo();
java下添加class:cmo.dubbo.provider.demo.DemoServiceImpl:
package com.dubbo.provider.demo; import java.text.SimpleDateFormat; import java.util.Date; import com.alibaba.dubbo.rpc.RpcContext; import com.dubbo.api.demo.*; /** * Created by Administrator on 17-1-1. */ public class DemoServiceImpl implements DemoService { public String sayHello(String name) { System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress(); } }
resources下添加dubbo配置META-INFO.spring-dubbo-provider.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:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <dubbo:application name="dubbo-provider-demo" ></dubbo:application> <dubbo:registry id="zk1" address="zookeeper://127.0.0.1:2181" protocol="zookeeper"></dubbo:registry> <dubbo:protocol id="mydubbo" name="dubbo" port="20886"></dubbo:protocol> <dubbo:provider register="zk1" protocol="mydubbo"></dubbo:provider> <dubbo:service interface="com.dubbo.api.demo.DemoService" ref="demoService"></dubbo:service> <bean id="demoService" class="com.dubbo.provider.demo.DemoServiceImpl" /> </beans>
啓動provider,在Edit Configuration 中添加application 的啓動方法,main class選擇com.alibaba.dubbo.container.Main。控制檯顯示[2017-01-07 15:07:25] Dubbo service server started! 則表示成功啓動