Dubbo(一)dubbo初体验,小白级入门案例

Dubbo

一、简介

1、dubbo是一个轻量级Java RPC框架,用于分布式系统的开发

  • RPC

全称Remote Procedure Call,远程过程调用。是一种进程间的通信方式,是一种技术思想,而不是规范。它允许程序调用另一个地址空间(网络的另一台机器上)的过程或函 数,而不用开发人员显式编码这个调用的细节。调用本地方法和调用远程方法一样。

  • 分布式系统

分布式系统是若干独立计算机(服务器)的集合,这些计算机对于用户来说就像单个相 关系统,分布式系统(distributed system)是建立在网络之上的服务器端一种结构。 分布式系统中的计算机可以使用不同的操作系统,可以运行不同应用程序提供服务,将 服务分散部署到多个计算机服务器上。

2、基本架构

  • 服务提供者
  • 服务消费者
  • 注册中心
  • 监控中心

在这里插入图片描述

二、使用方式

1、直接连接

  直接连接只需要服务提供者和服务消费者,不使用注册中心,但需要将服务提供者打包成jar文件,在消费者中通过groupId引用。

服务提供者
  • User(需要进行序列化)
public class User implements Serializable {
    private String id;
    private String name;
    private String age;
    private String address;

    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 String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
  • UserService接口

public interface UserService {
    User queryUser(String id);
}
  • UserServiceImpl
public class UserServiceImpl implements UserService {
    @Override
    public User queryUser(String id) {
        User user = new User();
        user.setId(id);
        user.setName("lisi");
        user.setAge("20");
        user.setAddress("中国");
        return user;
    }
}
  • 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="001-link-userService-provider" />

    <!--设置访问服务协议的名称与端口号-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--暴露服务接口,registry:如果不使用注册中心,则使用N/A,即直连方式-->
    <dubbo:service interface="cn.krain.dubbo.service.UserService" ref="userService" registry="N/A"/>

    <!--将接口的实现类加入到spring容器中-->
    <bean id="userService" class="cn.krain.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-userService-provider.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
服务消费者
  • UserController
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user")
    public String getUser(Model model, String id){
        User user = userService.queryUser(id);
        model.addAttribute(user);
        return "index";
    }
}
  • 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="002-link-consumer"/>

    <!--引用远程服务接口-->
    <dubbo:reference id="userService"
                     interface="cn.krain.dubbo.service.UserService"
                     url="dubbo://localhost: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="cn.krain.dubbo.web"/>

    <!--配置注解驱动-->
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</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">

    <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:002-link-consumer.xml,classpath:application.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
  • index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户详情</title>
</head>
<body>
<h1>用户详情</h1>
<div>
    id:${user.id}<br>
    name:${user.name}<br>
    age:${user.age}<br>
    address:${user.address}<br>
</div>
</body>
</html>

2、使用注册中心

使用zookeeper注册中心

  该方法与直接连接相比只有dubbo配置文件有所不同,以及需要一个接口工程,其他均相同。该接口工程中只有序列化的实体类和所创建的接口,在服务者和消费者使用时通过groupId引用即可。

  • 服务提供者的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="007-zk-userservice-provider"/>

    <!--设置协议和端口-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--使用zookeeper注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--暴露接口-->
    <dubbo:service interface="cn.krain.dubbo.service.UserService" ref="userServiceImpl"/>

    <!--将实现类放入到spring容器中-->
    <bean id="userServiceImpl" class="cn.krain.dubbo.service.impl.UserServiceImpl"/>

</beans>
  • 服务消费者
<?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="008-zk-consumer"/>

    <!--指定zookeeper注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--引用远程接口服务-->
    <dubbo:reference id="userService" interface="cn.krain.dubbo.service.UserService"/>


</beans>

三、版本控制

  一个接口可以有不同的实现方式,通过版本号调用不同的实现类。

四、监控中心

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

krain.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值