Spring整合Dubbo框架过程及原理解析

网友投稿 542 2023-07-06

Spring整合Dubbo框架过程及原理解析

Spring整合Dubbo框架过程及原理解析

这篇文章主要介绍了Spring整合Dubbo框架过程及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。演示过程创建两个小工程,一个作为服务的提供者,一个作为服务的消费者。通过Dubbo来实现服务消费者远程调用服务提供者的方法。

dubbo 的使用需要一个注册中心,这里以Zookeeper为例来演示

1.Dubbo架构

Dubbo架构图(Dubbo官方提供)如下:

节点角色说明:

节点

角色名称

Provider

暴露服务的服务提供方

Consumer

调用远程服务的服务消费方

Registry

服务注册与发现的注册中心

Monitor

统计服务的调用次数和调用时间的监控中心

Container

服务运行容器

调用关系说明:

服务容器负责启动,加载,运行服务提供者。

服务提供者在启动时,向注册中心注册自己提供的服务。

服务消费者在启动时,向注册中心订阅自己所需的服务。

注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

2.服务提供者开发

(1) 创建maven工程(打包方式为war)dubbodemo_provider,添加依赖pom.xml代码如下

xmlns:xsi="http://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">

4.0.0

war

dubbodemo_provider

com.alibaba

dubbo

2.6.6

io-ty

netty-all

4.1.32.Final

org.apache.curator

curator-framework

4.0.0

org.apache.zookeeper

zookeeper

org.apache.zookeeper

zookeeper

3.4.7

com.github.sgroschupf

zkclient

0.1

javassist

javassist

3.12.1.GA

com.alibaba

fastjson

1.2.47

xmlns:xsi="http://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">

4.0.0

war

dubbodemo_provider

com.alibaba

dubbo

2.6.6

io-ty

netty-all

4.1.32.Final

org.apache.curator

curator-framework

4.0.0

org.apache.zookeeper

zookeeper

org.apache.zookeeper

zookeeper

3.4.7

com.github.sgroschupf

zkclient

0.1

javassist

javassist

3.12.1.GA

com.alibaba

fastjson

1.2.47

(2) 配置web.xml文件

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">

contextConfigLocation

classpath:spring/applicationContext-provider.xml

org.springframework.web.context.ContextLoaderListener

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">

contextConfigLocation

classpath:spring/applicationContext-provider.xml

org.springframework.web.context.ContextLoaderListener

(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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

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文件

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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

springmvc

*.do

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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

springmvc

*.do

(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文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

dubbo的使用小demo已经完成。大家可以尝试一下

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot实现国际化过程详解
下一篇:spring整合Quartz框架过程详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~