Dubbo——HelloWorld

Dubbo是什麼?

dubbo是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。
簡單來說dubbo是一個服務框架,如果沒有分佈式需求是不需要用的。

其核心包括
1.遠程通訊:提供對多種基於長連接的NIO框架抽象封裝,包括多線程模型,序列化,以及“請求-響應”模式的信息交換方式。

2.集羣容錯:提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。

3.自動發現:基於註冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方可以平滑增加或減少機器。

4.Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基於Spring的Schema擴展進行加載。

官方網站:dubbo.io

HelloWorld

依賴包:
這裏寫圖片描述

ServiceProvider(服務提供方):

目錄結構:
這裏寫圖片描述

SimpleSerrvice:

public interface SimpleService {

    String sayHello(String name);

    public List getUsers();
}

SimpleServiceImpl:

public class SimpleServiceImpl implements SimpleService {

    @Override
    public String sayHello(String name) {
        //
        return "Hello " + name;
    }

    @Override
    public List<User> getUsers() {
        List<User> list = new ArrayList<User>();
        User u1 = new User();
        u1.setName("zhang1");
        u1.setAge(18);
        User u2 = new User();
        u2.setName("zhang2");
        u2.setAge(20);
        list.add(u2);
        list.add(u1);

        return list;
    }

}

simple-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-2.5.xsd
   http://code.alibabatech.com/schema/dubbo 
   http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

   <!-- 具體實現的bean -->
   <bean id="simpleService" class="com.zhang.simple.provider.impl.SimpleServiceImpl"/>

   <!-- 提供方應用信息,用於計算依賴關係 -->
   <dubbo:application name="simple-provider"></dubbo:application>

   <!-- 使用zookeeper註冊中心暴露服務 -->
   <dubbo:registry address="zookeeper://192.168.0.3:2181" />

   <!-- 用dubbo協議在2080端口暴露 -->
   <dubbo:protocol name="dubbo" port="20880"/>

   <!-- 聲明需要暴露的服務接口  寫操作設置retries=0避免重複調用soa -->
   <dubbo:service retries="0" interface="com.zhang.simple.provider.SimpleService" ref="simpleService"/>

</beans>

Provider:

public static void main(String[] args) throws IOException {
        @SuppressWarnings("resource")
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"simple-provider.xml"});
        context.start();
        System.in.read();

    }

Consumer(消費方):

目錄結構:
這裏寫圖片描述

simple-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-2.5.xsd
   http://code.alibabatech.com/schema/dubbo 
   http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

   <!-- 消費方應用名 用於計算依賴關係  與服務提供方不一樣-->
   <dubbo:application name="simple-consumer"/>

   <dubbo:registry address="zookeeper://192.168.0.3:2181"/>

   <dubbo:reference id="simpleService" check="false" interface="com.zhang.simple.provider.SimpleService"/>
</beans>

SimpleService:

public interface SimpleService {

    String sayHello(String name);

    public List getUsers();
}

Consumer:

public static void main(String[] args) throws IOException {
        @SuppressWarnings("resource")
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"simple-Consumer.xml"});
        context.start();

        SimpleService simpleService = (SimpleService)context.getBean("simpleService");
        String hello = simpleService.sayHello("zhang");
        System.out.println(hello);
        System.in.read();

    }

搭建好zookeeper服務器後,先運行Provider,再運行Consumer,即可打印hello zhang

zookeeper服務器中多出了/dubbo節點,

[zk:192.168.0.3:2181]ls /dubbo
[com.zhang.simple.provider.SimpleService]

服務提供方和消費方中,SimpleService所在的包名必須一致,
如:都爲com.zhang.simple.provider.SimpleService

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章