dubbo的介紹大家自行百度,下面以一個demo作爲dubbo學習的開始
DUBBO 入門例子 - 一個服務一個實現
dubbo版本2.5.8
註冊中心使用zookeeper,版本3.4
代碼結構如下
api是接口服務定義
consumer是服務消費者
provider是服務生產者
api代碼如下
public interface DemoService {
String sayHi(String name);
}
只是用來定義服務接口,代碼很簡單
provider代碼如下
接口實現類
public class ProviderService implements DemoService {
@Override
public String sayHi(String name) {
return "provider " + name;
}
}
啓動類
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
context.start();
System.in.read();
}
}
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-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方應用信息,用於計算依賴關係 -->
<dubbo:application name="hello-world-app" />
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 聲明需要暴露的服務接口 -->
<dubbo:service interface="com.tiger.dubbo.api.DemoService" ref="demoService" />
<!-- 和本地bean一樣實現服務 -->
<bean id="demoService" class="com.tiger.dubbo.provider.ProviderService" />
</beans>
pom文件如下,consumer類似
<?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>dubboooo</artifactId>
<groupId>tiger</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.8</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>tiger</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
consumer代碼如下
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
context.start();
DemoService service = (DemoService) context.getBean("demoService");
System.out.println(service.sayHi("wawa"));
}
}
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-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方一樣 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
<dubbo:reference id="demoService" interface="com.tiger.dubbo.api.DemoService" />
</beans>
依次啓動zookeeper、provider、consumer,consumer控制檯顯示“provider wawa”則成功。
例子 - 一個服務多個實現
以上是dubbo最簡單的demo,下面在demo的基礎上做一些簡答的修改。
上面的代碼是provider提供接口實現,然後由consumer消費。如果同一個接口有多個實現consumer是如何區分的?
增加新的接口實現
public class SecondProviderService implements DemoService {
@Override
public String sayHi(String name) {
return "second " + name;
}
}
在provider.xml文件中暴露新的實現
<dubbo:service interface="com.tiger.dubbo.api.DemoService" ref="demoService" />
<bean id="demoService" class="com.tiger.dubbo.provider.ProviderService" />
<dubbo:service interface="com.tiger.dubbo.api.DemoService" ref="secService" />
<bean id="secService" class="com.tiger.dubbo.provider.SecondProviderService" />
現在的問題就是consumer如何區分兩個實現。
dubbo是通過分組概念來區分的,在service配置文件中增加group參數,區分每個實現,修改後代碼如下
<dubbo:service interface="com.tiger.dubbo.api.DemoService" ref="demoService" group="first" />
<bean id="demoService" class="com.tiger.dubbo.provider.ProviderService" />
<dubbo:service interface="com.tiger.dubbo.api.DemoService" ref="secService" group="second"/>
<bean id="secService" class="com.tiger.dubbo.provider.SecondProviderService" />
通過group參數爲first和second來區分兩個實現,consumer調用代碼不變,只是在配置文件中需要指定消費的是哪個實現
<dubbo:reference id="demoService" interface="com.tiger.dubbo.api.DemoService" group="first" />
運行結果這裏就不寫了,大家可以自己試試.