Dubbo的简单了解

深入理解RPC与Dubbo

1.RPC的简单介绍

• 简单的说,RPC就是从一台机器(客户端)上通过参数传递的方式调用另一台机器(服务器)上的一个函数或方法(可以统称为服务)并得到返回的结果。
• RPC 会隐藏底层的通讯细节(不需要直接处理Socket通讯或Http通讯)
• RPC 是一个请求响应模型。客户端发起请求,服务器返回响应(类似于Http的工作方式)
• RPC 在使用形式上像调用本地函数(或方法)一样去调用远程的函数(或方法)。

2.直连服务

(1) 第一个模块

直连不需要注册器,只需要提供者和消费者,因此需要两个模块
第一个模块负责 逻辑代码的编写具体过程是

  1. 创建实体类,并且给出接口和服务实现类
  2. 编写dubbo核心配置文件 主要包括:
  <!-- 首先服务提供者声明名称  -->
    <dubbo:application name="001-dubbo" />
    <!-- name:指定协议的名称 port:端口号 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- interface:指定接口地址  引用:具体的接口实现类地址 registry:是否直连 N/A直连 -->
    <dubbo:service interface="com.cn.service.UserService" ref="userService" registry="N/A"/>
    <bean id="userService" class="com.cn.service.impl.UserServiceImpl"/>

3.配置web.xml文件的内容,包括监听器和上下文配置位置。

<web-app>
   <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>

4.pom文件中导入spring-context,spring-webmvc和dubbojar包
5.将项目打包到本地仓库,让消费者导入调用

(2)消费者模块

1.配置dubbo的核心配置文件

  <dubbo:application name="002-dubbo"/>
    <dubbo:reference id="userService" interface="com.cn.service.UserService"
                     url="dubbo://localhost:20880" registry="N/A"/>

2.配置spring配置文件

        <context:component-scan base-package="com.cn.web"/>
        <mvc:annotation-driven/>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"/>
            <property name="suffix" value=".jsp"/>
        </bean>

3.编写控制类代码
4.pom文件导入spring-context,spring-webmvc和dubbo以及之前提供者的jar包
5.配置web.xml文件

<web-app>
  <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-consumer.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

3.dubbo

(1)相关图片

最佳实践图

(2)总结使用过程

1. 需要编写一个接口和实体类的java项目,用于调用

2. 一个提供者的web项目,需要实现服务。

(1) pom文件中引入spring,dubbo,curator-framework和java项目
(2) 需要编写dubbo配置文件
a. 该项目的唯一性

<dubbo:application name=”001-dubbo”/>
b.	端口号和名字
<dubbo:protocol name=”dubbo” prot=”20880”/>
c.	注册中心
<dubbo:registry address=”zookeeper://localhost:2181”/>
d.	暴露接口
<dubbo:service interface=”com.cn.service.UserService”ref=”userServiceImpl”version=”1.0.0”/>
<dubbo:service interface=”com.cn.service.UserService”ref=”userServiceImpl2”version=”2.0.0”/>
<bean id=”userServiceImpl” class=”com.cn.service.UserServiceImpl”/>
<bean id=”userServiceImpl2” class=”com.cn.service.UserServiceImpl2”/>

(3) web.xml文件

<web-app>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:dubbo-version.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

(4) 编写服务类代码

3. 写消费者项目

Pom文件和提供者一样
(1) 配置dubbo文件

<dubbo:application name=”002-dubbo”/>
<dubbo:registry address=”zookeeper://localhost:2181”/>
<dubbo:reference interface=”com.cn.service.UserService” version=”1.0.0” id=”userService”/>
<dubbo:reference interface=”com.cn.service.UserService” version=”2.0.0” id=”userService2”/>

(2) 配置spring文件

<context component-scan=”com.cn.controller”/>
<mvc annotation-driven/>
<bean class=”internalResoucres…”>
	<property name=”pre..” value=”/”>
	<property name=”suf..” value=”.jsp”>
</bean>

(3) 配置web.xml文件

web-app>
  <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:dubbo-version-consumer.xml,classpath:springContext.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值