分布式-Dubbo
1. Dubbo是什么
- Apache Dubbo是一款高性能的Java RPC框架。
- RPC全称为remote procedure call,即远程过程调用,是指整个网络远程调用过程。
2. Dubbo的调用过程
节点说明:
- Container: 服务运行容器
- Provider: 服务提供方
- Consumer: 服务消费方
- Register: 注册中心
- Monitor: 统计服务的调用次数和调用时间的监控中心
3. 入门案例
在这之前要先安装Zookeeper作为注册中心,下载解压,修改默认的数据目录既可。
3.1 服务提供者
3.1.1 新建项目导入依赖
- Dubbo可以与Spring框架进行无缝集成。先创建个Maven的项目,在pom.xml
中导入Spring,Bubbo和Zookeeper的依赖
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
3.1.2 修改Web.xml
修改Web.Xml以创建Spring容器
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<listener>
<listener- class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.1.3 编写Service
package com.demo.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.demo.service.HelloService;
//注意,这个@Service因为是要通过Dubbo进行扫描的,所有要导入dubbo的包,不能导入Spring的
@Service
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}
3.1.4 新建application.xml
在Resources下新建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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
<dubbo:application name="dubbodemo_provider" />
<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
<dubbo:registry address="zookeeper://192.168.129.139:2181"/>
<!-- 注册 协议和port(默认为20880) -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
<dubbo:annotation package="com.demo.service.impl" />
</beans>
3.1.5 启动服务
当服务启动后,Dubbo就会把这个服务提供者,也就是dubbodemo_provider的信息注册到Zoopeeker中,可以在安装Zoopeeker时修改的数据文件夹里找到。
3.2 服务消费者
3.2.1 新建项目
与服务提供者一样,新建Maven项目,导入Spring,Dubbo和Zookeeper的包
3.2.2 修改Web.xml
修改Web.xml增加SpringMvc的相关配置,即增加DispatcherServlet。
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet- class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载 -->
<init-param>
<param-name>contextConfigLocation</param-name>
//applicationContext-web.xml里面会编写Dubbo相关配置
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.2.3 编写Controller
编写HelloController类
@Controller
@RequestMapping("/demo")
public class HelloController {
@Reference //这个是Dubbo的注解,可以把服务提供者的service注入进来
private HelloService helloService;
@RequestMapping("/hello")
@ResponseBody
public String getName(String name){
//远程调用
String result = helloService.sayHello(name);
System.out.println(result);
return result;
}
}
3.2.4 新建application-web.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
<dubbo:application name="dubbodemo-consumer" />
<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
<dubbo:registry address="zookeeper://192.168.129.139:2181"/>
<!-- 扫描的方式暴露接口 -->
<dubbo:annotation package="com.demo.controller" />
</beans>
3.2.5 启动测试
至此,启动测试既可。
4.负载均衡
在集群负载均衡时,Dubbo 提供了多种均衡策略(包括随机、轮询、最少活跃调用数、一致性
Hash),缺省为random随机调用。
在服务提供者和服务消费者都可以进行负载均衡的配置:
- 服务提供者配置负载均衡
@Service(loadbalance = "random")
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}
- 服务消费者配置负载均衡
@Controller
@RequestMapping("/demo")
public class HelloController {
@Reference(loadbalance = "random") //这个是Dubbo的注解,可以把服务提供者的service注入进来
private HelloService helloService;
........
}
}