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. 简单实例
实例源码:
http://download.csdn.net/detail/morning99/8077301
首先简历maven工程pom.xml引用 dubbo zkclient zookeeper 和 spring jar 包
- <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>
http://blog.csdn.net/morning99/article/details/40426133
特别注意:zookeeper的服务端 和 本地调用 客户端的jar包版本最好要一致,否则会出现意想不到的惊喜。
本人使用的zookeeper 是3.3.6 版本
项目结构图
服务端
applicationProvider.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="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>
applicationConsumer.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="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>
服务端代码:
demodubboserversrcmainjavacommorserverdubboserviceDemoServer.java
- 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
- 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 集群
2.在启动 服务端
3.最后 启动客户端调用服务
服务端打包
服务端打包