• 进一步改正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. Vue 路由的简单使用(命名路由、query路由传参、params路由传参)

    1 # Vue 路由 2 # 1.理解:一个路由(toute)就是一组映射关系(key-value),多个路由需要路由器(router)进行管理 3 # 2.前端路由:key是路径,value是组件 ...

  2. 花一分钟体验 Apache DolphinScheduler 第一个官方 Docker 镜像

    先前Apache DolphinScheduler 社区一直是发布 Dockerfile 和 K8s Chart.yaml 文件,由用户自行 build 镜像.随着越来越多的用户伙伴们的呼声高涨,社区 ...

  3. 我和Apache DolphinScheduler的缘分

    关于 DolphinScheduler社区 Apache DolphinScheduler(incubator) 于17年在易观数科立项,19年3月开源, 19 年8月进入Apache 孵化器,社区发 ...

  4. LuoguP3377 左偏树 (左偏树)

    TLE but corrct in most cases. inline int Find(int x){ //be careful with the way used for finding you ...

  5. 从零搭建云原生技术kubernetes(K8S)环境-通过kubesPhere的AllInOne方式

    前言 k8s云原生搭建,步骤有点多,但通过kubesphere,可以快速搭建k8s环境,同时有一个以 Kubernetes 为内核的云原生分布式操作系统-kubesphere,本文将从零开始进行kub ...

  6. 大家都能看得懂的源码 - 如何封装 cookie/localStorage/sessionStorage hook?

    本文是深入浅出 ahooks 源码系列文章的第九篇,该系列已整理成文档-地址.觉得还不错,给个 star 支持一下哈,Thanks. 今天来看看 ahooks 是怎么封装 cookie/localSt ...

  7. .Net Core使用Coravel实现任务调度

    前言 前段时间需要在一个新项目里添加两个后台任务,去定时请求两个供应商的API来同步数据:由于项目本身只是一个很小的服务,不太希望引入太重的框架,同时也没持久化要求:于是我开始寻找在Quartz.Ne ...

  8. haodoop数据压缩

    压缩概述 压缩技术能够有效减少底层存储系统(HDFS)读写字节数.压缩提高了网络宽带和磁盘空间的效率.在运行MR程序时,I/O操作,网络数据传输,Shuffle和Merge要花大量的时间,尤其是数据规 ...

  9. 云原生之旅 - 2)Docker 容器化你的应用

    前言 上文中我们用Golang写了一个HTTP server,本篇文章我们讲述如何容器化这个应用,为后续部署到kubernetes 做准备. 关键词:Docker, Containerization, ...

  10. CI/CD集成

    文章转载自:https://kuboard.cn/guide/cicd/ 下图展示了当前比较典型的持续构建集成的一种做法. 在是否自动将最新版本部署到 Kubernetes 环境这个问题上,可能会有多 ...