• 进一步改正dubbo框架中简单的直连式的不足

  • 需要用到3个相互独立的maven工程,项目1为maven的java工程作为接口工程,项目2,3为maven的web工程

    工程1:o3-link-interface	作为接口工程
    工程2:o4-link-userservice-provider 作为服务的提供者
    工程3:o5-link-consumer 作为使用服务的消费者

工程1

  • 结构:与简单的直连式不同的是,引入了接口工程,将实体类和所提供的服务的接口放在接口工程里

  • pom文件

    <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> <groupId>com.example.dubbo</groupId>
    <artifactId>o3-link-interface</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging> <build>
    <plugins>
    <!--JDK1.8编译插件-->
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
  • 实体类:注意要实现序列化接口,数据需要通过socket网络传输

    package com.example.dubbo.model;
    
    import java.io.Serializable;
    
    public class User implements Serializable {
    private String id;
    private String name;
    private String age; @Override
    public String toString() {
    return "User{" +
    "id='" + id + '\'' +
    ", name='" + name + '\'' +
    ", age='" + age + '\'' +
    '}';
    } public String getId() {
    return id;
    } public void setId(String id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public String getAge() {
    return age;
    } public void setAge(String age) {
    this.age = age;
    } public User(String id, String name, String age) {
    this.id = id;
    this.name = name;
    this.age = age;
    } public User() {
    }
    }
  • 服务接口:

    package com.example.dubbo.service;
    
    import com.example.dubbo.model.User;
    
    public interface UserService {
    /**
    * 根据用户id,获取用信息
    */
    User queryUserById(String id);
    }

工程2

  • 结构

  • pom文件:要引入接口工程的依赖,知道要对哪些待提供的服务进行实现

    <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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.dubbo</groupId>
    <artifactId>o4-link-userservice-provider</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version> <dependencies>
    <!-- Spring依赖 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.16.RELEASE</version>
    </dependency> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.16.RELEASE</version>
    </dependency> <!-- dubbo依赖 -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
    </dependency> <!-- 接口工程 -->
    <dependency>
    <groupId>com.example.dubbo</groupId>
    <artifactId>o3-link-interface</artifactId>
    <version>1.0.0</version>
    </dependency> </dependencies> <build>
    <plugins>
    <!--JDK1.8编译插件-->
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
  • dubbo的服务提供者的配置文件

    <?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 服务提供者标识 -->
    <dubbo:application name="o4-link-userservice-provider"/>
    <!-- 使用的协议和端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 提供的服务 -->
    <dubbo:service interface="com.example.dubbo.service.UserService" ref="userService" registry="N/A"/>
    <!-- 服务的实现类-->
    <bean id="userService" class="com.example.dubbo.service.impl.userServiceImpl"/>
    </beans>
  • web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0"> <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:dubbo-link-userservice-provider.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app>
  • 提供的服务实现

    package com.example.dubbo.service.impl;
    
    import com.example.dubbo.model.User;
    import com.example.dubbo.service.UserService; public class userServiceImpl implements UserService {
    @Override
    public User queryUserById(String id) {
    User user = new User();
    user.setId(id);
    user.setName("橘子");
    user.setAge("18");
    return user;
    } @Override
    public int queryAllUserCount() {
    return 3;
    }
    }

工程3

  • 结构

  • pom文件:要引入接口工程的依赖,知道可以申请哪些服务

    <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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.dubbo</groupId>
    <artifactId>o5-link-consumer</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version> <dependencies>
    <!--Spring依赖-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.16.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.16.RELEASE</version>
    </dependency> <!--dubbo依赖-->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
    </dependency> <!-- 接口工程 -->
    <dependency>
    <groupId>com.example.dubbo</groupId>
    <artifactId>o3-link-interface</artifactId>
    <version>1.0.0</version>
    </dependency>
    </dependencies> <build>
    <plugins>
    <!--JDK1.8编译插件-->
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
  • dubbo里消费者的配置文件

    <?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 消费者标识 -->
    <dubbo:application name="o5-link-consumer"/> <!-- 引用的远程服务 -->
    <dubbo:reference id="userService" interface="com.example.dubbo.service.UserService" url="dubbo://127.0.0.1:20880" registry="N/A"/>
    </beans>
  • spring核心配置文件

    <?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"
    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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描组件 -->
    <context:component-scan base-package="com.example.dubbo.web.controller"/> <!-- 注解驱动 -->
    <mvc:annotation-driven/> <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
    </bean>
    </beans>
  • Controller层

    package com.example.dubbo.web.controller;
    
    import com.example.dubbo.model.User;
    import com.example.dubbo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping; @Controller
    public class UserController {
    @Autowired
    UserService userService;
    /**
    * 响应前端请求,返回用用户详细信息以及总的用户个数
    */
    @RequestMapping("/getUserDetail.do")
    public String getUserDetail(String id, Model model){
    //获取数据
    User user = userService.queryUserById(id);
    int userCount = userService.queryAllUserCount();
    //存放数据
    model.addAttribute("user", user);
    model.addAttribute("userCount", userCount);
    //跳转到用户详情页面
    return "userDetail";
    }
    }
  • web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0"> <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application.xml classpath:dubbo-link-consumer.xml</param-value>
    </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    </web-app>
  • 返回给前端的响应页面:userDetail.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>用户详情页</title>
    </head>
    <body>
    <div>用户id:${user.id}</div>
    <div>用户名:${user.name}</div>
    <div>用户年龄:${user.age}</div>
    <div>用户数量:${userCount}</div>
    </body>
    </html>

测试

  • 将服务提供者工程和消费者工程部署到tomcat上并运行

  • 运行结果

分析

  • 优点:

  • 在直连式的基础上引入了接口工程,其中包含实体类和待提供的服务的接口,定义了可以提供哪些服务

  • 服务者工程只要在其pom文件中引入对上述接口工程的依赖,对待提供的服务进行实现即可

  • 消费者工程只要在其pom文件中引入对上述接口工程的依赖,对所提供的服务进行申请访问即可

  • 上述接口工程的使用很好的隔离了服务消费者和服务提供者之间的耦合,在二者之间搭建了一个沟通调用的桥梁

  • 缺点:

  • 当提供的服务较多时,对服务者提供的服务以及消费者可以申请的服务不太好管理,无法对现有服务种类进行很好的统计与管理

Dubbo 03: 直连式 + 接口工程的更多相关文章

  1. Dubbo 02: 直连式

    直连式 需要用到两个相互独立的maven的web项目 项目1:o1-link-userservice-provider 作为服务的提供者 项目2:o2-link-consumer 作为使用服务的消费者 ...

  2. C#的显式接口和隐式接口(转载)

    接口的实现分为:隐式实现和显式实现.如果类或者结构要实现的是单个接口,可以使用隐式实现,如果类或者结构继承了多个接口那么接口中相同名称成员就要显式实现.显示实现是通过使用接口的完全限定名来实现接口成员 ...

  3. 转】C#接口-显式接口和隐式接口的实现

    [转]C#接口-显式接口和隐式接口的实现 C#中对于接口的实现方式有隐式接口和显式接口两种: 类和接口都能调用到,事实上这就是“隐式接口实现”. 那么“显示接口实现”是神马模样呢? interface ...

  4. 读书笔记_Effective_C++_条款四十一:了解隐式接口和编译期多态

    从本条款开始,就进入了全书的第七部分:模板与泛型编程.模板与泛型在C++中是非常重要的部分,还记得本书第一章时,把C++视为一个联邦,它由四个州政府组成,其中一个政府就是模板与泛型了. 本条款是一个介 ...

  5. 读书笔记 effective c++ Item 41 理解隐式接口和编译期多态

    1. 显示接口和运行时多态 面向对象编程的世界围绕着显式接口和运行时多态.举个例子,考虑下面的类(无意义的类), class Widget { public: Widget(); virtual ~W ...

  6. Java 8 新特性1-函数式接口

    Java 8 新特性1-函数式接口 (原) Lambda表达式基本结构: (param1,param2,param3) -> {代码块} 例1: package com.demo.jdk8; i ...

  7. C#学习-显式接口

    显式的接口实现解决了命名冲突问题. 在使用显式的接口实现方式时,需要注意以下几个问题. 若显式实现接口,方法不能使用任何访问修饰符,显式实现的成员都默认为私有: 现式实现的成员默认是私有的,所以这些成 ...

  8. [JavaScript,Java,C#,C++,Ruby,Perl,PHP,Python][转]流式接口(Fluent interface)

    原文:https://en.m.wikipedia.org/wiki/Fluent_interface(英文,完整) 转载:https://zh.wikipedia.org/wiki/流式接口(中文, ...

  9. Java 8 新特性:1-函数式接口

    (原) Java 8 新特性1-函数式接口 Lambda表达式基本结构: (param1,param2,param3) -> {代码块} Lambda表达式结构: (type1 arg1,typ ...

随机推荐

  1. SpringBoot定时任务 - 经典定时任务设计:时间轮(Timing Wheel)案例和原理

    Timer和ScheduledExecutorService是JDK内置的定时任务方案,而业内还有一个经典的定时任务的设计叫时间轮(Timing Wheel), Netty内部基于时间轮实现了一个Ha ...

  2. 使用 Golang 代码生成图表的开源库对比

    本文的目标读者 对用 Golang 代码生成折线图.扇形图等图表有兴趣的朋友. 本文摘要 主要介绍 Go 中用以绘图的开源库,分别是: GitHub - wcharczuk/go-chart: go ...

  3. 使用JMeter测试.Net5.0,.Net6.0框架下无数据处理的并发情况

    1.   安装JMeter及使用 1.1下载JMeter 登录官方网站找到下载链接进行下载:https://jmeter.apache.org/download_jmeter.cgi 1.2配置环境变 ...

  4. 数据库简介与MySQL简介

    MySQL简介 数据存取演变史 起源······文本文件 在最开始使用计算机都没有相应的规范我们的数据一般都是自己起一个名字然后就根据这个路径存储数据并且存储数据的格式也都五花八门就产生了很多奇奇怪怪 ...

  5. Ubuntu20.04配置 ES7.17.0集群

    Ubuntu20.04配置 ES7.17.0集群 ES能做什么? elasticsearch简写es,es是一个高扩展.开源的全文检索和分析引擎,它可以准实时地快速存储.搜索.分析海量的数据. Ubu ...

  6. java基础———注释

    注释是写给读者看的,并不会被执行! 单行注释 以 //开头 例如://注释内容              可以注释一行文本 多行注释 以/*开头     以 */结束 例如:/*注释内容*/      ...

  7. 「学习笔记」斜率优化dp

    目录 算法 例题 任务安排 题意 思路 代码 [SDOI2012]任务安排 题意 思路 代码 任务安排 再改 题意 思路 练习题 [HNOI2008]玩具装箱 思路 代码 [APIO2010]特别行动 ...

  8. Linux驱动开发十六.input系统——3.系统自带的input驱动

    前面两章我们通过input子系统构建了一个按键类型的输入设备的驱动,其实Linux的内核还提供了一套基于GPIO的按键驱动程序,和LED设备一样,我们只需要在编译内核的过程中进行配置然后在设备树中定义 ...

  9. APICloud 可视化编程 - 拖拉拽实现专业级源码

    低代码开发平台是无需编码 (0 代码或⽆代码) 或通过少量代码就可以快速生成应用程序的开发平台.它的强⼤之处在于,允许终端⽤户使⽤易于理解的可视化⼯具开发自己的应用程序,而不是传统的编写代码⽅式.当遇 ...

  10. 第五十四篇:网络通信Axios

    好家伙,补充知识 1.什么是Axios? Axios可以在浏览器中发送 XMLHttpRequests Axios 是一个基于 promise 的 HTTP 库,简单的讲就是可以发送get.post请 ...