Dubbo实例~直连的方式

  • 2.4 Dubbo实例~直连的方式
    • 2.4.1 服务的提供者
    • 2.4.2 服务的消费者
    • 2.4.1 服务的提供者、消费者两个服务跑起来

2.4 Dubbo实例~直连的方式

提供者和消费者直接连接,不需要注册中心

2.4.1 服务的提供者

步骤:
1.创建一个maven web工程:服务的提供者

2.创建一个实体bean查询的结果
3.提供一个服务接口:xxXX
4.实现这个服务接口:xxxxImpl

5.配置dubbo服务提供者的核心配置文件
a. 声明dubbo服务提供者的名称:保证唯一
b. 声明dubbo使用的协议和端口号
c. 暴露服务,使用直连方式
6.添加监听器

  1. 配置相关依赖(创建一个maven web工程:服务的提供者

pom

<?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><groupId>com.guo.dubbo</groupId><artifactId>001-link-userservice-provider</artifactId><version>1.0.0</version><packaging>war</packaging><!--让提供者项目打成jar包双击Ctrl 输入命令:mvn 001-link-userservice-provider install--><dependencies><!--引入spring依赖 spring-context上下文  spring-webmvc  --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.20</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.20</version></dependency><!--引入dubbo依赖--><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.2</version></dependency></dependencies><build><plugins><!--JDK 1.8 编译插件--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins><defaultGoal>compile</defaultGoal></build>
</project>
  1. 服务的提供者:创建一个实体bean查询的结果,提供一个服务接口:xxXX,实现这个服务接口:xxxxImpl

  1. 配置dubbo服务提供者的核心配置文件
    a. 声明dubbo服务提供者的名称:保证唯一
    b. 声明dubbo使用的协议和端口号
    c. 暴露服务,使用直连方式

src/main/resources/dubbo-userservice-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:dubbo="http://code.alibabatech.com/schema/dubbo"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"><!--服务的提供者声明名称:必须保证服务名称的唯一性,它的名称是dubbo内部使用的唯一表示--><dubbo:application name="001-link-userservice-provider"/><!--访问服务协议的名称及端口号,dubbo官网推荐使用的是dubbo协议20880--><!--name:指定协议的名称port:指定协议的端口号(默认是20880)--><dubbo:protocol name="dubbo" port="20880"/><!--暴露服务接口 dubbo:serviceinterface:暴露的服务接口的全限定类名ref:接口引用的实现类在spring容器中的标识registry:如果不使用注册中心,则值为:N/A  (直连)--><dubbo:service interface="com.guo.dubbo.service.UserService" ref="userService" registry="N/A"/><!--将接口的实现类加载到spring容器中--><bean id="userService" class="com.guo.dubbo.service.impl.UserSericeImpl"/>
</beans>
  1. 添加监听器

src/main/webapp/WEB-INF/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-userservice-provider.xml</param-value></context-param><!--监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
  1. 创建一个Tomcat

2.4.2 服务的消费者

思路:
1.创建一个maven web工程:服务的消费者
2.配置pom文件 :添加需要的依赖(spring,dubbo)
3.设置dubbo的核心配置文件()
4.编写controller
5.配置中央调度器(servlet:DispatcherServlet)

  1. 配置pom文件 :添加需要的依赖(spring,dubbo)

服务消费者需要引入服务提供者的业务接口,调用业务接口所提供的方法,因此,我们需要在服务提供者的项目上打一下jar包,发布到本地仓库,这样在服务消费者直接可以引入服务消费者的相关依赖

<?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><groupId>com.guo.dubbo</groupId><artifactId>002-link-consumer</artifactId><version>1.0.0</version><packaging>war</packaging><dependencies><!--spring依赖  spring-context spring-webmvc--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.9</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.9</version></dependency><!--dubbo依赖--><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.2</version></dependency><!--引入服务提供者的依赖,,这里需要先在服务的提供者项目打一下jar包,这里引用那个jar包--><dependency><groupId>com.guo.dubbo</groupId><artifactId>001-link-userservice-provider</artifactId><version>1.0.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.10.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
</project>
  1. 设置dubbo的核心配置文件()

src/main/resources/dubbo-consumer.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:dubbo="http://code.alibabatech.com/schema/dubbo"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">
<!--声明消费者的名称:保证唯一性--><dubbo:application name="002-link-consumer"/><!--引用远程服务接口:id:远程服务接口对象的名称interface:调用远程接口的全限定类名url:访问服务接口的地址registry:不使用注册中心,直连值为:N/A--><!--服务消费者dubbo的核心配置文件--><dubbo:reference id="userService"interface="com.guo.dubbo.service.UserService"url="dubbo://localhost:20880"registry="N/A"/>
</beans>

src/main/resources/aplication.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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--扫描组件--><context:component-scan base-package="com.guo.dubbo.web"/><!--配置注解驱动--><mvc:annotation-driven/><!--前端页面——视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean><!--视图解析器-->
</beans>
  1. 控制层
    UserController.java
package com.guo.dubbo.web;import com.guo.dubbo.model.User;
import com.guo.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.GetMapping;import javax.jws.WebParam;@Controller
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/user")public String userDetail(Model model,Integer id){User user = userService.queryUserById(id);model.addAttribute("user",user);return "index";}
}
  1. 配置中央调度器servlet
    src/main/webapp/WEB-INF/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:aplication.xml,classpath:dubbo-consumer.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
  1. jsp页面展示数据
    src/main/webapp/index.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head><title>用户详情</title>
</head>
<body><h1>用户详情</h1><div>用户标识:${user.id}</div><div>用户名称:${user.name}</div><div>用户年龄:${user.age}</div>
</body>
</html>
  1. 创建一个Tomcat服务

2.4.1 服务的提供者、消费者两个服务跑起来

服务成功启动,去访问服务消费者的控制层
http://localhost:8080/user?id=1001

BUG:

  1. xml 中引入命名空间容易出错
  2. 实体类的序列化实现序列化接口

dubbo官方推荐必须有一个接口工程,它就是一个maven java工程要求接口工程里存放到内容如下:
1.对外暴露的服务接口(service接口)
2.实体bean对象

Dubbo实例~直连的方式相关推荐

  1. 初识Dubbo(直连方式)

    Dubbo直连方式具体步骤实现 1.创建服务提供者 1.1.创建maven web工程 1.2.整理pom文件,添加spring依赖和dubbo依赖和jdk1.8编译插件 1.3.在src/main目 ...

  2. Spark系列十七:经典案列使用直连的方式,Kafka,SparkSteaming,Redis

    先一个一个java程序,读取日志文件中的数据,然后将数据写入到Kafka中,然后写一个SparkSteaming程序,使用直连的方式读取Kafka中的数据,计算如下指标 该文件是一个电商网站某一天用户 ...

  3. centos选择什么版本_有几千个 Dubbo 实例的瓜子二手车,为什么要选择2.7.3版本?...

    随着瓜子业务的不断发展,系统规模在逐渐扩大,目前在瓜子的私有云上已经运行着数百个 Apache Dubbo ( 下文简称 Dubbo )应用,上千个 Dubbo 实例.瓜子各部门业务迅速发展,版本没有 ...

  4. JS基础-Java Class类以及获取Class实例的三种方式

    JS基础-Java Class类以及获取Class实例的三种方式 由于JVM为每个加载的class创建了对应的Class实例,并在实例中保存了该class的所有信息,包括类名.包名.父类.实现的接口. ...

  5. Dubbo(RPC原理、Dubbo架构负载均衡配置方式)(3)

    1.什么是负载均衡 先来个官方的解释. 维基百科对负载均衡的定义:负载均衡改善了跨多个计算资源(例如计算机,计算机集群,网络链接,中央处理单元或磁盘驱动的的工作负载分布.负载平衡旨在优化资源使用,最大 ...

  6. Dubbo(RPC原理、Dubbo架构负载均衡配置方式)(2)

    1.Dubbo 的架构图解 上述节点简单说明: Provider: 暴露服务的服务提供方 Consumer: 调用远程服务的服务消费方 Registry: 服务注册与发现的注册中心 Monitor: ...

  7. SpringBoot2.x整合Dubbo(直连模式)

    SpringBoot 是一种快速开发框架,帮助我们快速整合第三方框架(Maven方式继承).完全采用注解化(使用注解方式启动SpringMvc),简化XML 内置Http服务(tomcat.jetty ...

  8. 实例说明扩展JQuery方式

    如何扩展Jquery? 1. 有两种方式来扩展JQuery,分别是: $.extend(object): 为域名jQuery增加方法,为静态方法,全局方法,任何人都可以调用,调用的时候,需要直接加上$ ...

  9. Spring容器中获取Bean实例的七种方式(附实战源码)

    目录 写作说明 一:写作原因 二:源码出处 实现方式 一:使用BeanFactory直接获取(不推荐) 二:在初始化时保存ApplicationContext对象 三:继承自抽象类Applicatio ...

最新文章

  1. Runloop, 多线程
  2. 前端最佳实践之可维护性
  3. 关闭NPC call(__)
  4. oracle查询优化不走缓存,Oracle数据库存储优化问题
  5. 解决离线安装依赖包的方法
  6. select框的text与value值的获取(实用版)
  7. 坑爹的RegExp test()
  8. iOS开发系列--Objective-C之协议、代码块、分类
  9. fs2410开发板搭建网站服务器,FS2410开发板使用步骤
  10. Spring Boot整合ehcache的详细使用
  11. HTKbook翻译之第十二章网络、词典及语言模型
  12. 零基础计算机入门,分享人人可以参考的路线,私藏的干货视频和书单,为你的程序员生涯助力,呐喊!
  13. 服务器修改密码次数过多提示被锁定,路由器密码错误次数过多锁死了怎么办?...
  14. NDK (C++) 开发中如何使用 ASan 检测内存越界、溢出等内存错误
  15. 微信小程序【网易云音乐实战】(第三篇 自定义组件、排行榜的制作、内网穿透、tabBar、个人中心)
  16. 企业微信上传临时素材文件
  17. 编码的奥秘:从算盘到芯片
  18. 【新书速递】信息安全标委会委员推荐的金融科技安全工具书
  19. 访问web页面出现Whitelabel Error Page原因
  20. 【熵与特征提取】从近似熵,到样本熵,到模糊熵,再到排列熵,究竟实现了什么?(第一篇)——近似熵及其MATLAB实现

热门文章

  1. NC65添加自定义按钮
  2. java jbk转utf8,将jbk格式的文件转换成utf-8的
  3. 使用js技术对单个div中的滚动条进行样式设置!
  4. mfc编程 孙鑫_MFC文本编程——孙鑫第五课
  5. CMake Error at /usr/lib/x86_64-linux-gnu/cmake/Qt5Core/Qt5CoreConfig.cmake:27 (message)
  6. JSR303校验前端传递的数据
  7. 两台电脑之间Sql Server数据库的链接配置
  8. Python学习记录 字典元素的访问
  9. Magic Powder - 1 CodeForces - 670D1(优先队列进一步理解)
  10. MySQL繁忙度查询_mysql 慢查询优化