dubbo簡單示例
1. 代碼結構
2.創建test-dubbo-api
創建服務接口類qinfeng.zheng.service.DemoApiService
package qinfeng.zheng.service;
public interface DemoApiService {
String getUser(Long userId);
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-dubbo-api</artifactId>
</project>
3. 創建test-dubbo-provider
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>qinfeng.zheng</groupId>
<artifactId>test-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
創建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
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--定義了提供方應用信息,用於計算依賴關系;在 dubbo-admin 或 dubbo-monitor 會顯示這個名字,方便辨識 -->
<dubbo:application name="test-provider" />
<!--使用 zookeeper 註冊中心暴露服務,註意要先開啟 zookeeper -->
<dubbo:registry address="zookeeper://192.168.96.100:2181" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--具體實現該接口的 bean -->
<bean id="demoService" class="qinfeng.zheng.service.impl.DemoApiServiceImpl" />
<!--使用 dubbo 協議實現定義好的 api.PermissionService 接口 -->
<dubbo:service interface="qinfeng.zheng.service.DemoApiService" ref="demoService" protocol="dubbo" />
</beans>
創建qinfeng.zheng.service.impl.DemoApiServiceImpl
package qinfeng.zheng.service.impl;
import qinfeng.zheng.service.DemoApiService;
public class DemoApiServiceImpl implements DemoApiService {
public String getUser(Long userId) {
System.out.println("生產者調用消費者服務接口userId:" + userId);
return "哈哈,關你啥事...";
}
}
創建服務啟動類qinfeng.zheng.ProviderApp
package qinfeng.zheng;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* 創建時間: 21:57 2018/9/18
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
public class ProviderApp {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");
System.out.println(context.getDisplayName() + ": here");
context.start();
System.out.println("服務已經啟動...");
System.in.read(); //保持服務一直在運行
}
}
4. 創建test-dubbo-consumer
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>qinfeng.zheng</groupId>
<artifactId>test-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
創建配置文件dubbo-consumer.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
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 -->
<dubbo:application name="test-consumer" />
<!--zk集群配置方式-->
<!--<dubbo:registry address="zookeeper://192.168.96.100:2181?backup=192.168.96.130:2181,192.168.96.131:2181" />-->
<dubbo:registry address="zookeeper://192.168.96.100:2181" />
<dubbo:reference id="demoApiService" interface="qinfeng.zheng.service.DemoApiService" />
</beans>
創建消費方啟動類ConsumerApp
import org.springframework.context.support.ClassPathXmlApplicationContext;
import qinfeng.zheng.service.DemoApiService;
/**
* 創建時間: 22:16 2018/9/18
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
public class ConsumerApp {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
context.start();
DemoApiService demoApiService = (DemoApiService) context.getBean("demoApiService");
String ret = demoApiService.getUser(1l);
System.out.println(ret);
System.in.read();
}
}
5. 啟動zookeeper註冊中心
啟動prodiver,consumer兩個服務
服務調用成功!!!
dubbo簡單示例