天天看點

Dubbo

Dubbo是阿裡巴巴公司開源的一個高性能優秀的服務架構,使得應用可通過高性能的RPC實作服務的輸出和輸入功能,可以和spring架構無縫內建。

1.主要核心部件

Remoting:網絡通信架構,實作了sync-over-async和request-reponse消息機制

RPC:一個遠端過程調用的一個抽象,支援負載均衡、容災和叢集功能

Registry:服務目錄架構用于服務的注冊和服務事件釋出和訂閱

2.工作原理

Provider:暴露服務方稱之為“服務提供者”。

Consumer:調用遠端服務方稱之為“服務消費者”。

Registry:服務注冊與發現的中心目錄服務稱之為“服務注冊中心”。

Monitor:統計服務的調用次數和調用時間的日志服務稱之為“服務監控中心”。

3.demo

3.1建立一個maven web工程

3.2在web.xml中添加如下代碼

<context-param>

                   <param-name>contextConfigLocation</param-name>

                   <param-value>classpath:config/applicationContext.xml</param-value>

         </context-param>

         <listener>

                   <listener-class>org.springframework.web.context.ContextLoaderListener

                   </listener-class>

         </listener>

                   <listener-class>

                            org.springframework.web.context.request.RequestContextListener

3.3建立接口和實作類

Dubbo
Dubbo

3.4定義生産者和消費者

在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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">

   <dubbo:application name="hello-world-app"/>

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

   <dubbo:protocol name="dubbo" port="20880"/>

   <dubbo:service interface="com.alibaba.hello.api.HelloService"ref="helloService"/>

   <bean id="helloService" class="com.alibaba.hello.impl.HelloServiceImpl"/>

</beans>

在consumer.xml中添加如下代碼:

   <dubbo:application name="consumer-of-helloworld-app"/>

   <dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService"/>

3.5編寫測試代碼進行測試

建立provider.java檔案

packagecom.alibaba.hello.test;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

public classProvider

{

    publicstatic void main(String[] args)

    {

       ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext(new String[]{"config/provider.xml"});

       // 啟動成功,監聽端口為20880System.in.read();//按任意鍵退出

    }

}

建立consumer.java檔案

importcom.alibaba.hello.api.HelloService;

public classConsumer

    public  static void  main(String[]  args){

       ClassPathXmlApplicationContext  context= new  ClassPathXmlApplicationContext(newString[]{"config/consumer.xml"});

       HelloService  helloService= (HelloService)context.getBean("helloService");

       //getserviceinvocationproxyStringhello=helloService.sayHello("world");

       //doinvoke!System.out.println(hello);

       //cool,howareyou~

上一篇: Dubbo