dubbo直连案例

dubbo直连案例,第1张

dubbo直连案例
  • 1.dubbo是什么?
  • 2.dubbo框架结构图
  • 3.为什么说dubbo性能高
  • 4. dubbo支持的协议
  • 5. dubbo直连案例(没有注册中心)

1.dubbo是什么?

dubbo是由阿里基于java开发的一个rpc(远程调用过程)框架,它提供了三大核心功能:面向接口的远程方法调用,智能容错和负载均衡,服务自动注册与发现。

2.dubbo框架结构图


1.服务提供者(Provider)
暴漏服务的服务提供方,服务提供者在启动的时候,向注册中心注册自己提供的服务。
2.服务消费者(Consumer)
调用远程服务的消费方,服务消费者在启动的时候,向注册中心订阅自己所需的服务,服务消费者,从服务提供者地址列表中,基于软负载均衡算法,选一台服务提供者进行调用,如果调用失败,再选另一台调用。
3.注册中心(Registry)
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4.监控中心(Monitor)
用于监控提供者和消费者的数据,统计在内存中服务注册或者服务调用的次数和调用时间,定时一分钟,将这些统计到的数据发送到监控中心。

3.为什么说dubbo性能高

1.序列化
我们知道在java网络中,一个对象想要在网络中进行传输,必须要实现Serializable接口。序列化的方式有很多比如xml,json,二进制流等,二进制流效率是最高的,因为计算机底层各种 *** 作实际上都是 *** 作二进制的数据,dubbo就是采用二进制流的方式。

2.网络通信
我们知道Http需要分七步走(三次握手四次挥手),而dubbo采用socket通信机制,只需要知道对方的主机和端口号即可建立连接,一步到位,提升了通信效率,不用反复连接,直接传输数据

4. dubbo支持的协议

dubbo支持的协议有dubbo,hessian,rmi,http,webservice,thrift,redis等,但是官方推荐使用的是dubbo协议!

5. dubbo直连案例(没有注册中心)

一.提供者
1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<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>
  <packaging>war</packaging>

  <name>link-provider</name>
  <groupId>com.yl</groupId>
  <artifactId>link-provider</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <!--dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.0</version>
    </dependency>
    <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>
  </dependencies>

</project>

2.测试接口以及实现类

package com.yl.service;

public interface HelloService {

    String hello();
}

package com.yl.service.impl;

import com.yl.service.HelloService;

public class HelloServiceImpl implements HelloService {
    @Override
    public String hello() {
        return "Hello Dubbo";
    }
}

3.提供者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://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="link-provider"/>

    <!--指定dubbo的协议和端口号-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--暴露服务
        使用dubbo:service来暴露服务
        interface 要暴露的服务的接口全限定类名
        ref 引用接口在spring容器中的标识值
        registry    使用直连方式的话,值为N/A
    -->
    <dubbo:service interface="com.yl.service.HelloService" ref="helloServiceImpl" registry="N/A"/>

    <!--加载接口实现类-->
    <bean id="helloServiceImpl" class="com.yl.service.impl.HelloServiceImpl"/>
</beans>

4.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">

  <!--spring的监听器配置-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:dubbo-provider.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

</web-app>

二.消费者
1.先install提供者到本地仓库
2.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<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>
  <packaging>war</packaging>

  <name>link-consumer</name>
  <groupId>com.yl</groupId>
  <artifactId>link-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <!--dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.0</version>
    </dependency>
    <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>
    <!--先install服务提供者到本地仓库,再使用其依赖-->
    <dependency>
      <groupId>com.yl</groupId>
      <artifactId>link-provider</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

</project>

3.controller

package com.yl.controller;

import com.yl.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello(Model model) {
        String str = helloService.hello();
        //调用远程服务接口
        model.addAttribute("hello",str);
        return "hello";
    }
}

4.消费者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://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="link-consumer"/>

    <!--引用远程接口
        id 引用远程接口服务的代理对象名称
        interface 接口都全限定类名
        url 调用远程服务接口的地址
        registry 直连方式,暂时不用注册中心
    -->
    <dubbo:reference id = "helloService" interface="com.yl.service.HelloService"
                     url = "dubbo://localhost:20880" registry="N/A"/>
</beans>

5.springmvc的配置

<?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/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!--扫描组件-->
    <context:component-scan base-package="com.yl.controller" />
    <!--注解驱动-->
    <mvc:annotation-driven/>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

6.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:dubbo-consumer.xml,classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

7.jsp文件

<%--
  Created by IntelliJ IDEA.
  User: mirror
  Date: 2022/5/1
  Time: 22:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>${hello}</h1>
</body>
</html>

三.测试
1.配置两个tomcat服务器,启动提供着和消费者
2.访问消费者的hello接口

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/869369.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存