dubbo项目基本结构如下,分别部署于不同服务器:
1.provider(接口API 实现)
2.consumer(web)
3.zookeeper
4.DB
provider构建
1.api构建
intellij中新建"maven"项目,groupId:com.dubbo.api.demo,artifactId:demo,projectName:dubbo-api-demo();
java下添加interface:cmo.dubbo.api.demo.DemoService;
public interface DemoService {
String sayHello(String name);
}
maven-install
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! 则表示成功启动