分布式简介
Dubbo (开源分布式服务框架)
Dubbo 是由阿里巴巴开源的基于Java的高性能RPC框架。 和许多RPC系统一样,dubbo基于定义服务的想法,指定可以通过参数和返回类型远程调用的方法。 在服务器端,服务器实现此接口并运行一个dubbo服务器来处理客户端调用。在客户端,有一个存根,提供与服务端相同的方法。
dubbo-architecture.png
Dubbo提供三个关键功能,包括基于接口的远程调用,容错和负载均衡以及自动服务注册和发现。
分布式实列
本节演示怎样使用maven构建dubbo服务提供者项目和dubbo服务消费者项目,dubbo服务注册使用广播模式。
场景设计
假设服务提供者叫helloworldApi,提供sayHello(String name)接口,该接口由HelloWorldService的实现类来实现,通过dubbo的广播模式提供给服务消费者。 消费者通过dubbo协议调用sayHello,并输出结果。
项目结构为:
helloworld-api (api工程)
helloworld-service (service工程,依赖helloworld-api和)
helloworld-dubbo-boot (dubbo提供者配置工程,依赖helloworld-Service,可启动)
helloworld-dubbo-client (dubbo消费者配置工程,依赖helloworld-api)
helloworld-test (测试工程,依赖helloworld-dubbo-client,可启动)
环境依赖
JDK 1.6+
Maven 3
以下教程创建java文件时均忽略了创建package的过程,请注意在对应的package里面创建java文件。
helloworld-api
参考3.4节,使用maven-archetype-quickstart原型创建工程,配置如图:
添加HelloworldApi接口,代码如下:
package com.tansun.helloworld.api;
public interface HelloworldApi {
String sayHello(String name);
}
完成后执行maven的clean install指令。
helloworld-service
参考3.4节,使用maven-archetype-quickstart原型创建工程,配置如图:
helloworld-service添加依赖
我们使用spring来管理对象,使用@Service注解来申明对象,所以需要在pom中添加spring相关的依赖。 打开pom.xml,在dependencies标签下添加以下代码:
org.springframework spring-context 3.2.9.RELEASE com.tansun helloworld-api 0.0.1-SNAPSHOT 添加HelloworldService接口,代码如下:package com.tansun.helloworld.service;
public interface HelloworldService {
String sayHello(String name);
}
添加HelloworldService接口实现,代码如下:
package com.tansun.helloworld.service.impl;
import com.tansun.helloworld.service.HelloworldService;
import org.springframework.stereotype.Service;
@Service(“helloworldService”)
public class HelloworldServiceImpl implements HelloworldService {
public String sayHello(String name) {
return "hello world " + name;
}
}
添加HelloworldApi接口实现,代码如下:
package com.tansun.helloworld.api.impl;
import com.tansun.helloworld.api.HelloworldApi;
import com.tansun.helloworld.service.HelloworldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service(“helloworldApi”)
public class HelloworldApiImpl implements HelloworldApi {
@Autowired
private HelloworldService helloworldService;
public String sayHello(String name) {
return helloworldService.sayHello(name);
}
}
完成后执行maven的clean install指令。
helloworld-dubbo-boot
参考3.4节,使用maven-archetype-quickstart原型创建工程,配置如图:
helloworld-dubbo-boot添加依赖
helloworld-dubbo-boot使用dubbo框架将helloworld-service发布成微服务,需要依赖dubbo和helloworld-service。
打开pom.xml,在dependencies标签下添加以下代码:
com.alibaba dubbo 2.8.4 org.apache.zookeeper zookeeper 3.4.6 com.github.sgroschupf zkclient 0.1 de.ruedigermoeller fst 2.44.3 com.tansun helloworld-service 0.0.1-SNAPSHOT 添加spring配置 创建src/main/resources/spring目录,在该目录创建文件application.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Spring配置 </description>
<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan base-package="com.tansun">
</context:component-scan>
添加dubbo服务提供者配置 创建src/main/resources/dubbo目录,在该目录创建文件provider-helloworld.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="helloworld-service" />
<!-- 使用zookeeper模式暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="172.19.0.223:2181" file="dubbo/registry.properties" />
<!-- dubbo协议缺省端口为20880,rmi协议缺省端口为1099,http和hessian协议缺省端口为80; 设置-1为动态分配端口,端口号 为 缺省值自增长 -->
<dubbo:protocol name="dubbo" port="-1" accesslog="dubbo/access.log" />
<!-- 增加暴露远程服务配置 -->
<dubbo:service interface="com.tansun.helloworld.api.HelloworldApi" ref="helloworldApi" version="1.0.0" />
添加dubbo启动配置 在src/main/resources目录创建文件dubbo.properties,内容如下:
dubbo.container=spring,log4j
dubbo.spring.config=classpath*:/spring/application.xml,classpath*:/dubbo/provider-helloworld.xml
dubbo.shutdown.hook=true
dubbo.consumer.check=false
dubbo.log4j.file=dubbo/dubbo.log
dubbo.log4j.level=INFO
创建启动类DubboBoot.java,内容如下:
package com.tansun.helloworld.dubbo.boot;
import com.alibaba.dubbo.container.Main;
public class DubboBoot {
public static void main(String[] args) {
Main.main(args);
}
}
完成后运行mvn install构建项目,然后运行DubboBoot,控制台显示"Dubbo service server started!",dubbo服务发布成功。
helloworld-dubbo-client
参考3.4节,使用maven-archetype-quickstart原型创建工程,配置如图:
helloworld-dubboclient添加依赖
helloworld-dubbo-client依赖dubbo框架,使用HelloworldApi消费helloworld-service微服务,需要依赖dubbo和helloworld-api 打开pom.xml,在dependencies标签下添加以下代码:
com.alibaba dubbo 2.8.4 org.apache.zookeeper zookeeper 3.4.6 com.github.sgroschupf zkclient 0.1 de.ruedigermoeller fst 2.44.3 com.tansun helloworld-api 0.0.1-SNAPSHOT 添加dubbo服务消费者配置 创建src/main/resources/dubbo目录,在该目录创建文件consumer-helloworld.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="helloworld-test" />
<!-- 使用zookeeper模式暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="172.19.0.223:2181" file="dubbo/registry.properties" />
<!-- 增加引用远程服务配置 -->
<dubbo:reference id="helloworldApi" interface="com.tansun.helloworld.api.HelloworldApi" version="1.0.0"/>
完成后执行maven的clean install指令。
helloworld-test
参考3.4节,使用maven-archetype-quickstart原型创建工程,配置如图:
helloworld-test添加依赖
helloworld-test依赖helloworld-dubbo-client。
打开pom.xml,在dependencies标签下添加以下代码:
com.tansun helloworld-dubbo-client 0.0.1-SNAPSHOT 修改App.java,内容如下:package com.tansun.helloworld;
import com.tansun.helloworld.api.HelloworldApi;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
-
Hello world!
/
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"classpath:/dubbo/consumer-helloworld.xml"});
context.start();HelloworldApi helloworldApi = (HelloworldApi) context.getBean("helloworldApi"); System.out.println(helloworldApi.sayHello("张三")); System.exit(0);
}
}
完成后执行maven的clean install指令。
运行App类,控制台输出"hello world 张三"。
说明
helloworld-dubbo-boot启动helloworld-service服务,并(通过dubbo框架)注册到广播平台(224.5.6.7:1234)。 helloworld-test(通过dubbo框架)从广播平台(224.5.6.7:1234)订阅helloworld-service服务地址(其实就在本地),并(通过dubbo框架)获取HelloworldApi的实例。 因此,helloworld-test中context.getBean(“helloworldApi”),获得的是一个由helloworld-dubbo-boot提供的远程的bean。
案例
如果你本机有安装zookeeper并启动,也可以配置zookeeper地址为127.0.0.1:2181。
下载dubbo-quick-start-demo.zip