1. 概述
Dubbo是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案
Dubbo是阿里巴巴SOA服務化治理方案的核心框架,每天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被廣泛應用於阿里巴巴集團的各成員站點:
那麼,Dubbo是什麼?
Dubbo是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。
其核心部分包含:
- 遠程通訊: 提供對多種基於長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應”模式的信息交換方式。
- 集羣容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。
- 自動發現: 基於註冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方可以平滑增加或減少機器。
Dubbo能做什麼?
- 透明化的遠程方法調用,就像調用本地方法一樣調用遠程方法,只需簡單配置,沒有任何API侵入。
- 軟負載均衡及容錯機制,可在內網替代F5等硬件負載均衡器,降低成本,減少單點。
- 服務自動註冊與發現,不再需要寫死服務提供方地址,註冊中心基於接口名查詢服務提供者的IP地址,並且能夠平滑添加或刪除服務提供者。
Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基於Spring的Schema擴展進行加載。
主要核心部件
Remoting: 網絡通信框架,實現了sync-over-async 和 request-response 消息機制.
RPC: 一個遠程過程調用的抽象,支持負載均衡、容災和集羣功能
Registry: 服務目錄框架用於服務的註冊和服務事件發佈和訂閱。
Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基於Spring的Schema擴展進行加載。
2. 簡單實例
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.mor.maven</groupId>
- <artifactId>dubboserver</artifactId>
- <version>0.0.1</version>
- <packaging>jar</packaging>
- <name>dubboserver</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>3.1.4.RELEASE</spring.version>
- <slf4j.version>1.6.6</slf4j.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <!-- Spring -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-asm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- spring end -->
- <!-- log -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.5.3</version>
- </dependency>
- <!-- zkclient -->
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- <!-- zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.3.6</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>dubbo-demo</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.1</version>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- <encoding>UTF-8</encoding>
- <failOnError>false</failOnError>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
- <?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="hello-world-app" />
- <!-- 本機 僞集羣 測試 -->
- <dubbo:registry protocol="zookeeper" address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" />
- <dubbo:protocol name="dubbo" port="20880" />
- <dubbo:service interface="com.mor.server.dubbo.service.DemoServer"
- ref="demoService" /> <!-- 和本地bean一樣實現服務 -->
- <bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" />
- </beans>
- <?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="consumer-of-helloworld-app" />
- <span style="white-space:pre"> </span><!-- 使用本地僞集羣註冊中心暴露發現服務地址 -->
- <dubbo:registry protocol="zookeeper" address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" /> <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
- <dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
- </beans>
- package com.mor.server.dubbo.service;
- public interface DemoServer {
- String sayHello(String str);
- }
- <span style="font-family:Courier New;">package com.mor.server.dubbo.service;
- import java.util.Date;
- public class DemoServerImpl implements DemoServer {
- public String sayHello(String str) {
- str = "Hello " + str + "2:" + new Date();
- System.out.println("server:" + str);
- return str;
- }
- }</span><strong style="font-family: Arial;">
- </strong>
- package com.mor.main;
- import java.io.IOException;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- public static void main(String[] args) throws IOException {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
- context.start();
- System.out.println("按任意鍵退出");
- System.in.read();
- }
- }
- log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n
- log4j.rootLogger=INFO,Stdout
- log4j.logger.org.apache.wicket=INFO
- log4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFO
- log4j.logger.org.apache.wicket.version=INFO
- log4j.logger.org.apache.wicket.RequestCycle=INFO
客戶端代碼:
- package com.mor.client.dubbo.action;
- import java.util.Date;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.mor.server.dubbo.service.DemoServer;
- public class ChatAction {
- public void SayHello(){
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
- context.start();
- DemoServer demoServer = (DemoServer) context.getBean("demoService");
- System.out.println("client:"+demoServer.sayHello("Morning"+"1:"+new Date())+"3:"+new Date());
- }
- }
- package com.mor.client.dubbo.main;
- import com.mor.client.dubbo.action.ChatAction;
- public class Main {
- public static void main(String[] args){
- ChatAction act = new ChatAction();
- act.SayHello();
- }
- }
1.先啓動 zookeeper 集羣
服務端打包