• Dubbo快速入门


    一、Dubbo概述

    官网地址:Apache Dubbo

    dubbo是一个轻量级、高性能java RPC框架,主要三大核心功能:面向接口的远程方法调用、智能容错和负载均衡、服务自动注册和发现

    1、什么是RPC

    RPC即为远程过程调用,如A服务器上的某个功能调用B服务器上的某个功能即为远程过程调用,

    一个RPC框架两个很重要也是影响效率性能的点是

    • 如何高效实现序列化和反序列化
    • 如何快速建立通信

    2、dubbo就是通过这一思想实现的框架,实现语言是java

    3、架构

    Provider:服务提供方,服务启动时向注册中心注册自己提供的服务

    Consumer:服务消费者,在启动服务时向注册中心订阅自己所需的服务,从服务提供者地址列表中基于负载均衡算法选一台提供者进行调用,如果调用失败再选一台调用;consumer调用provider提供的服务使用的协议就是dubbo协议端口是20880,dubbo提供的协议有很多如http、dubbo、rmi等,

    Registry:注册中心返回服务提供者地址列表给服务消费者,如果有变更,注册中心将基于长连接推送变更数据给服务消费者;这个注册中心可以不用,直接消费服务提供者提供的服务,这种消费称为直连消费

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

    二、dubbo直连工程

    dubbo直连消费就是没有注册中心,消费者直接消费服务提供者提供的服务

    1、创建provider工程(服务提供者)

     依赖

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>dubboartifactId>
    4. <version>2.6.0version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframeworkgroupId>
    8. <artifactId>spring-contextartifactId>
    9. <version>4.3.16.RELEASEversion>
    10. dependency>
    11. <dependency>
    12. <groupId>org.springframeworkgroupId>
    13. <artifactId>spring-webmvcartifactId>
    14. <version>4.3.16.RELEASEversion>
    15. dependency>

    服务接口以及实现类

    1. public interface MsgService {
    2. String hello(String hello);
    3. }
    4. public class MsgServiceImpl implements MsgService {
    5. public String hello(String hello) {
    6. return "hello"+hello;
    7. }
    8. }

     springApplicationContext.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. 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">
    5. <dubbo:application name="dubbo-provider"/>
    6. <dubbo:protocol name="dubbo" port="20880"/>
    7. <dubbo:service interface="service.MsgService" ref="msgServiceImpl" registry="N/A"/>
    8. <bean id="msgServiceImpl" class="service.impl.MsgServiceImpl"/>
    9. beans>

    web.xml

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    5. http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    6. version="3.1">
    7. <context-param>
    8. <param-name>contextConfigLocationparam-name>
    9. <param-value>classpath:springApplicationContext.xmlparam-value>
    10. context-param>
    11. <listener>
    12. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    13. listener>
    14. web-app>

    2、创建consumer工程(服务消费者)

    依赖

    1. <dependency>
    2. <groupId>org.examplegroupId>
    3. <artifactId>providerartifactId>
    4. <version>1.0-SNAPSHOTversion>
    5. dependency>
    6. <dependency>
    7. <groupId>com.alibabagroupId>
    8. <artifactId>dubboartifactId>
    9. <version>2.6.0version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.springframeworkgroupId>
    13. <artifactId>spring-contextartifactId>
    14. <version>4.3.16.RELEASEversion>
    15. dependency>
    16. <dependency>
    17. <groupId>org.springframeworkgroupId>
    18. <artifactId>spring-webmvcartifactId>
    19. <version>4.3.16.RELEASEversion>
    20. dependency>

    测试方法

    1. @RestController
    2. public class MsgController {
    3. @Autowired
    4. private MsgService msgService;
    5. @RequestMapping("hello")
    6. public String hello(Model model){
    7. String msg = msgService.hello("ni hao");
    8. model.addAttribute("msg",msg);
    9. return "index";
    10. }
    11. }

    springMvc.xml

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. http://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/mvc
    11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12. package="demo.controller"/>
    13. "org.springframework.web.servlet.view.InternalResourceViewResolver">
    14. "prefix" value="/"/>
    15. "suffix" value=".jsp"/>

    springApplicationContext.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. 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">
    5. <dubbo:application name="dubbo-consumer"/>
    6. <dubbo:reference interface="service.MsgService"
    7. id="msgService"
    8. url="dubbo://localhost:20880"
    9. registry="N/A"/>
    10. beans>

    web.xml

    1. "1.0" encoding="UTF-8"?>
    2. "http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    5. http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    6. version="3.1">
    7. dispatcherServlet
    8. org.springframework.web.servlet.DispatcherServlet
    9. contextConfigLocation
    10. classpath:springMvc.xml,classpath:springApplicationContext.xml
    11. 1
    12. dispatcherServlet
    13. /

    使用tomcat依次部署provider和consumer两个项目,浏览器访问

    http://localhost:8080/hello

    三、dubbo推荐的项目结构

    官方推荐的dubbo项目结构包括三部分,分别是

    • 服务消费工程:war工程 服务提供方
    • 服务提供工程:war工程 服务消费方
    • 服务接口工程:jar工程 ,业务接口和实体类

    对上面的工程改造,创建一个interface的java工程,此工程只有一个接口类

    1. //注意实体类要实现Serializable
    2. public class User implements Serializable {
    3. private String name;
    4. private String sex;
    5. public String getName() {
    6. return name;
    7. }
    8. public void setName(String name) {
    9. this.name = name;
    10. }
    11. public String getSex() {
    12. return sex;
    13. }
    14. public void setSex(String sex) {
    15. this.sex = sex;
    16. }
    17. }
    18. //接口
    19. public interface MsgService {
    20. String hello(String hello);
    21. }

     provider工程需要修改的地方

    1、pom依赖引入接口坐标

    1. org.example
    2. interface
    3. 1.0-SNAPSHOT

    2、删除provider原有的接口

    3、实现类实现接口工程的接口和方法

    4、核心xml

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. 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">
    5. "dubbo-provider"/>
    6. "dubbo" port="20880"/>
    7. "service.MsgService" ref="msgServiceImpl" registry="N/A"/>
    8. "msgServiceImpl" class="service.impl.MsgServiceImpl"/>

    consumer工程需要修改地方

    1、pom依赖删掉之前的provider坐标,增加interface工程坐标

    2、springApplicationContext文件

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. 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">
    5. "dubbo-consumer"/>
    6. "service.MsgService"
    7. id="msgService"
    8. url="dubbo://localhost:20880"
    9. registry="N/A"/>

     3、测试

    1. @RestController
    2. public class MsgController {
    3. //接口工程接口bean
    4. @Autowired
    5. private MsgService msgService;
    6. @RequestMapping("hello")
    7. public String hello(Model model){
    8. String msg = msgService.hello("ni hao");
    9. model.addAttribute("msg",msg);
    10. return "index";
    11. }
    12. }

    四、dubbo结合注册中心使用

    上面说的直连方式在企业项目中基本不会使用,因为随着项目系统复杂,服务的数量增加,通过注册中心可以统一的管理服务,实际管理的就是服务url地址

    注册中心可以有很多种,dubbo官方推荐使用zookeeper

    关于zookeeper学习参考文章 ZooKeeper_程序三两行的博客-CSDN博客_zookeeper

    1、创建接口工程interface

    maven创建一个java工程即可,创建服务接口类

    1. public interface MsgService {
    2. String hello(String hello);
    3. }

     接口和上面直连方式一样没有什么变化

    2、创建服务提供者provider

    maven创建一个web工程,相较于直连方式,引入依赖有所变化

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>org.examplegroupId>
    6. <artifactId>providerartifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <packaging>warpackaging>
    9. <dependencies>
    10. <dependency>
    11. <groupId>com.alibabagroupId>
    12. <artifactId>dubboartifactId>
    13. <version>2.6.2version>
    14. dependency>
    15. <dependency>
    16. <groupId>org.apache.curatorgroupId>
    17. <artifactId>curator-frameworkartifactId>
    18. <version>4.1.0version>
    19. dependency>
    20. <dependency>
    21. <groupId>org.springframeworkgroupId>
    22. <artifactId>spring-contextartifactId>
    23. <version>4.3.16.RELEASEversion>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframeworkgroupId>
    27. <artifactId>spring-webmvcartifactId>
    28. <version>4.3.16.RELEASEversion>
    29. dependency>
    30. <dependency>
    31. <groupId>org.examplegroupId>
    32. <artifactId>interfaceartifactId>
    33. <version>1.0-SNAPSHOTversion>
    34. dependency>
    35. dependencies>
    36. project>

     核心配置文件

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. 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">
    5. <dubbo:application name="dubbo-provider"/>
    6. <dubbo:protocol name="dubbo" port="20880"/>
    7. <dubbo:registry address="zookeeper://localhost:2181"/>
    8. <dubbo:service interface="service.MsgService" ref="msgServiceImpl"/>
    9. <bean id="msgServiceImpl" class="service.impl.MsgServiceImpl"/>
    10. beans>

    其他代码和上面直连方式一样 

    3、创建服务消费者consumer

    依赖和provider一样

    核心配置文件

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. 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">
    5. <dubbo:application name="dubbo-consumer"/>
    6. <dubbo:registry address="zookeeper://localhost:2181"/>
    7. <dubbo:reference id="msgService" interface="service.MsgService"/>
    8. beans>

    其他代码和上面直连方式一样 

    4、测试

    1. @RestController
    2. public class MsgController {
    3. //接口工程接口bean
    4. @Autowired
    5. private MsgService msgService;
    6. @RequestMapping("hello")
    7. public String hello(Model model){
    8. String msg = msgService.hello("ni hao");
    9. return msg;
    10. }
    11. }

    5、查看zookeeper目录

    五、dubbo配置 

    关于dubbo相关的配置,我们一般都在服务提供者工程中进行配置,因为服务提供正更了解服务的各种参数

    1、关闭检查

    dubbo默认是开启检查依赖服务的,我们可以在配置文件中关闭

    1. <dubbo:registry address="zookeeper://localhost:2181" check="false"/>
    2. <dubbo:reference interface="service.RoleService" check="false"/>

    2、重试次数

    消费着访问服务提供者,如果访问失败,则切换重试访问其他服务器,但是切换会带来更长的延迟,访问时间变长,影响用户体验,通过retries="2"来设置重试次数

    1. <dubbo:service retries="2" interface=""/>
    2. <dubbo:reference retries="2" interface=""

    这个配置一般不用设置,默认重试一次就行

    3、超时时间

    服务提供和访问超时时间,一般只在服务提供者设置就行

     <dubbo:service interface="service.MsgService" ref="msgServiceImpl" timeout="500"/>

    4、版本号

    随着项目的迭代更新,一个接口服务可能有多个实现类,区分不同的接口实现使用version

    如下provider工程增加一个服务实现类MsgServiceImpl2

    配置文件变化

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. 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">
    5. "dubbo-provider"/>
    6. "dubbo" port="20880"/>
    7. "zookeeper://localhost:2181"/>
    8. "service.MsgService" ref="msgServiceImpl" version="1.0"/>
    9. "service.MsgService" ref="msgServiceImpl" version="2.0"/>
    10. "msgServiceImpl" class="service.impl.MsgServiceImpl"/>

     consumer配置文件

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. 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">
    5. "dubbo-consumer"/>
    6. "zookeeper://localhost:2181"/>
    7. "msgService" interface="service.MsgService" version="1.0"/>
    8. "msgService2" interface="service.MsgService" version="2.0"/>

    测试

    1. @RestController
    2. public class MsgController {
    3. //接口工程接口bean
    4. @Autowired
    5. private MsgService msgService;
    6. @Autowired
    7. private MsgService msgService2;
    8. @RequestMapping("hello")
    9. public String hello(Model model){
    10. String msg = msgService.hello("ni hao");
    11. String msg2 = msgService2.hello("ni hao 2");
    12. return msg+"==="+msg2;
    13. }
    14. }

    六、监控中心

    dubbo-admin可视化的dubbo服务管理,安装时候需要指定注册中心,启动时候从注册中心获取服务消费者和提供者管理

    参考使用

    (4条消息) dubbo-admin安装以及dubbo-admin简单使用_尤雨东的博客-CSDN博客_dubbo-admin

  • 相关阅读:
    mui tap事件,mui.confirm弹窗出现两次
    B. Kalindrome Array
    手把手教你,如何利用积木易搭3D扫描仪完成文物三维建模?
    数据结构01
    SpringBoot 事务与AOP
    JavaScript-内置对象
    【小程序】微信公众号模板消息跳转小程序发送失败:errcode=40013 , errmsg=invalid appid rid:...
    Fair原理篇Fair逻辑动态化架构设计与实现
    js中dom和bom有什么区别
    如何获得jd商品分类API数据
  • 原文地址:https://blog.csdn.net/qq_34491508/article/details/125909041