Spring整合Dubbo框架过程及原理解析
作者:Runtimeing 发布时间:2022-01-31 01:49:16
这篇文章主要介绍了Spring整合Dubbo框架过程及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。演示过程创建两个小工程,一个作为服务的提供者,一个作为服务的消费者。通过Dubbo来实现服务消费者远程调用服务提供者的方法。
dubbo 的使用需要一个注册中心,这里以Zookeeper为例来演示
1.Dubbo架构
Dubbo架构图(Dubbo官方提供)如下:
节点角色说明:
节点 | 角色名称 |
---|---|
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的服务消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行容器 |
调用关系说明:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
2.服务提供者开发
(1) 创建maven工程(打包方式为war)dubbodemo_provider,添加依赖pom.xml代码如下
<?xml version="1.0" encoding="UTF-8"?>
<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>
<packaging>war</packaging>
<artifactId>dubbodemo_provider</artifactId>
<dependencys>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<!-- dubbo2.6.X以上的版本开始 2.8.4不需要 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencys>
</project>
(2) 配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- * 监听其他的spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
(3) 创建服务接口
package com.test.dubbo.api;
public interface HelloService {
public String sayHello(String name);
}
(4) 创建服务实现类
package com.test.service.impl;
import com.test.dubbo.api.HelloService;
//这个service并不是spring提供的,是dubbo的service代替的
import com.alibaba.dubbo.config.annotation.Service;
@Service //把此服务注册到zookeeper
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}
==注意==:服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务
(5) 在src/main/resources下创建applicationContext-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: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://127.0.0.1:2181"/>
<!-- 注册 协议和port 端口默认是20880 -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
<dubbo:annotation package="com.test.service.impl" />
</beans>
6)启动服务 也就是把spring容器启动即可
可以用tomcat启动项目
也可以用main方法加载spring配置文件,也就是启动了spring容器
在com.itheima包下创建一个DemoProvider类来启动spring容器,代码如下
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class DemoProvider {
public static void main(String[] args) throws IOException {
// 加载配置文件,启动容器
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-provider.xml");
app.start();
System.in.read(); //等待控制台回车。如果不回车就一直卡这儿不继续
}
}
3.服务消费者开发
开发步骤:
(1) 创建maven工程(打包方式为war)dubbodemo_consumer,pom.xml配置和上面服务提供者相同
(2) 配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
(3) 将服务提供者工程中的HelloService接口复制到当前工程
(4) 编写Controller
package com.test.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.dubbo.api.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/demo")
public class HelloController {
@Reference//这里使用dubbo提供的,用来代替@Autowired来注入服务
private HelloService helloService;
@RequestMapping("/hello")
@ResponseBody
public String getName(String name){
//远程调用
String result = helloService.sayHello(name);
System.out.println(result);
return result;
}
}
==注意==:Controller中注入HelloService使用的是Dubbo提供的@Reference注解
(5) 在src/main/resources下创建spring文件夹,再创建springmvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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">
<!--spring的扫描包,扫描的是spring的注解-->
<context:component-scan base-package="cn.test"/>
<!--告诉zookeeper 当前项目是哪个-->
<dubbo:application name="dubbodemo_consumer"/>
<!--链接注册中心-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--注解扫描 扫描的是dubbo的 @Reference注解-->
<dubbo:annotation package="cn.test"/>
<!-- timeout:每次请求都会等待3秒 retries失败后重试次数-->
<dubbo:consumer timeout="3000" retries="0"/>
</beans>
dubbo的使用小demo已经完成。大家可以尝试一下
来源:https://www.cnblogs.com/changchangchang/p/12023943.html


猜你喜欢
- 这里以list为介绍:private static readonly T[] s_emptyArray = new T[0];public
- 本文以实例详述了C#实现Socket通信的解决方法,具体实现步骤如下:1、首先打开VS新建两个控制台应用程序:ConsoleApplicat
- __intSumintSum 函数可用于计算两个或多个整数值的总和。引用名称是可选的, 但它不能是有效的整数。{__intSum(2,5,M
- Kotlin this详解及实例为了表示当前函数的接收者(receiver), 们使用this表达式:在类的成员函数中,this指向这个类的
- DrawerLayout顾名思义就是一个管理布局的。使用方式可以与其它的布局类类似。DrawerLayout带有滑动的功能。只要按照draw
- 使用客户端打开指定的URL使用Process.Start方法可以在浏览器打开指定的URL。代码如下所示。[C#]//使用客户端打开“http
- 首先我们要知道,微信的聊天记录一般是不提供给我们获取的,所以一般情况下我们手机没root的话就拿不到了。就算是root后的手机,想要获取微信
- 从主线程发送消息到子线程(准确地说应该是非UI线程)package com.zhuozhuo;import android.app.Acti
- 项目前端由于采用Extjs4,列表分页需要返回三个参数:totalCount(记录总数)、start(开始位置)、limit(每页条数)。由
- 话不多说,上来就是干!?1234567891011121314151617181920212223242526272829303132333
- jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS
- 排列组合的概念排列:从n个不同元素中取出m(m≤n)个元素,按照一定的顺序排成一列,叫做从n个元素中取出m个元素的一个排列(Arrangem
- 最近滑动验证码在很多网站逐步流行起来,一方面对用户体验来说,比较新颖,操作简单,另一方面相对图形验证码来说,安全性并没有很大的降低。当然到目
- 在Android 5.0以后的版本中,定义一个button时,系统自动会加一个阴影的效果,有的时候这种效果看起来比较好,有的时候不符合UI的
- Java的三种标准注解和四种元注解先来说说什么是注解注解(也被称为元数据)为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个
- 1. 首先新建一个shiroConfig shiro的配置类,代码如下:@Configurationpublic class SpringS
- MongoDB是介于关系数据库和非关系数据库之间的一种产品,文件的存储格式为BSON(一种JSON的扩展),这里就主要介绍Java通过使用m
- 最近用到一些字符串加密,而.net中提供的加密算法中用起来比较复杂,便简单的封装了一下,方便日后使用。public class Encryp
- 文件创建:File.Create(Application.StartupPath + "\\AlarmSet.txt")
- 后端实现1. 数据库设计我们需要设计两个表:用户表和角色表。用户表字段 类型 描述id bigint(20) 用户 IDusername v