Dubbo入门(一)

本文详细介绍了Dubbo的基本概念、服务注册中心Zookeeper的作用,以及通过创建服务提供方和消费方的实例,展示了如何使用Dubbo进行远程调用。涵盖了Maven配置、Spring集成和Zookeeper部署等关键步骤。
摘要由CSDN通过智能技术生成

Dubbo学习(一)

一、dubbo概念

Dubbo是一款 高性能的Java RPC框架。其前身是阿里巴巴公司开源的、轻量级的开源Java RPC 框架,可以和Spring框架无缝集成。

什么是REC?

RPC 就是 Remote Procedure Call,远程过程调用,它相对应的是本地过程调用。

需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。

RPC是一个泛化的概念,严格来说一切远程过程调用手段都属于RPC范畴。

二、dubbo

img

节点说明

节点说明
Provider服务提供方
Consumer需要调用远程服务的服务消费方
Registry服务注册与发现中心
Monitor监控中心
Container服务器运行的容器

虚线都是异步访问,实线都是同步访问 蓝色虚线:在启动时完成的功能 红色虚线(实线)都是程序运行过程中执行的功能

调用关系说明: 
	首先服务提供者Provider启动然后向注册中心注册自己所能提供的服务。
	服务消费者Consumer启动向注册中心订阅自己所需的服务。
	然后注册中心将提供者元信息通知给Consumer,之后Consumer因为已经从注册中心获取提供者的地址,因此可以通过负载均衡选择一个Provider直接调用。
	之后服务提供方元数据变更的话注册中心会把变更推送给服务消费者。
	服务提供者和消费者都会在内存中记录着调用的次数和时间,然后定时的发送统计数据到监控中心。

三、服务注册中心Zookeeper

通过前面的Dubbo架构图可以看到,Registry(服务注册中心)在其中起着至关重要的作用。Dubbo官方推荐使用Zookeeper作为服务注册中心。

3.1 Zookeeper介绍

Zookeeper它是一个分布式服务框架,是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。

zookeeper=文件系统+监听通知机制

Zookeeper树型目录服务:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-orO2vLrr-1655740130156)(C:\Users\10302\AppData\Roaming\Typora\typora-user-images\1655737657542.png)]

流程说明:
	服务提供者(Provider)启动时: 向 /dubbo/com.foo.BarService/providers 目录下写入自己的URL地址
	服务消费者(Consumer)启动时: 订阅 /dubbo/com.foo.BarService/providers 目录下的提供者URL 地址。并向 /dubbo/com.foo.BarService/consumers 目录下写入自己的URL地址
	监控中心(Monitor)启动时: 订阅 /dubbo/com.foo.BarService 目录下的所有提供者和消费者URL地址

3.2 安装Zookeeper

​ 参考地址:

​ https://blog.csdn.net/she_lock/article/details/80435176#comments


四、Dubbo入门案例

Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。本小节就是要创建两个应用,一个作为服务的提供方,一个作为服务的消费方。通过Dubbo来实现服务消费方远程调用服务提供方的方法。

准备共两个服务,服务提供方和服务消费方。案例中我们的注册中心使用Zookeeper 。

4.1 服务提供方

开发步骤:

  • 创建服务提供方dubbodemo-provider,pox文件如下
<?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.kkb</groupId>
    <artifactId>dubbodemo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.0.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8081</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

  • 配置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">
    
       <display-name>Archetype Created web Application</display-name>
       <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath*:applicationContext*.xml</param-value>
       </context-param>
    
       <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
    
    </web-app>
    
  • 创建服务接口

    package com.kkb.service;
    public interface HelloService {
        public String sayHello(String name);
    }
    
  • 创建服务实现类

    需要注意引入@Service的包。服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务

    package com.kkb.service.impl;
    import com.alibaba.dubbo.config.annotation.Service;
    import com.kkb.service.HelloService;
    //import org.springframework.stereotype.Service;
    @Service
    public class HelloServiceImpl implements HelloService {
        @Override
        public String sayHello(String name) {
            return "hello" + name;
        }
    }
    
  • 在src/main/resources下创建applicationContext-service.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:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
           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
               http://www.springframework.org/schema/mvc/spring-mvc.xsd
               http://code.alibabatech.com/schema/dubbo
               http://code.alibabatech.com/schema/dubbo/dubbo.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
        <dubbo:application name="dubbodemo-provider" />
        <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
        <dubbo:registry address="zookeeper://192.168.141.128:2181" />
        <!-- 注册 协议和port -->
        <dubbo:protocol name="dubbo" port="20881" ></dubbo:protocol>
        <!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
        <dubbo:annotation package="com.kkb.service.impl" />
    
    </beans>
    
  • 启动服务

    tomcat7:run


4.2 服务消费方

开发步骤

  • 创建maven工程(打包方式为war)dubbodemo_consumer,pom.xml配置和上面服务提供者相同,只需要将Tomcat插件的端口号改为8082即可。

  • 配置web.xml文件

    <!DOCTYPE web-app PUBLIC 
    	"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 			"http://java.sun.com/dtd/web-app_2_3.dtd" > 
    <web-app> 
        <display-name>Archetype Created Web Application</display-name> 		<servlet> 
        	<servlet-name>springmvc</servlet-name> 						<servletclass>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载 --> 
        <init-param> 
            <param-name>contextConfigLocation</param-name> 
            <param-value>classpath:applicationContext-web.xml</param-value> 	</init-param> 
        <load-on-startup>1</load-on-startup> 
        </servlet> 
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name> 
            <url-pattern>*.do</url-pattern> 
        </servlet-mapping> 
    </web-app>
    
  • 将服务提供者工程中的HelloService接口复制到当前工程

    Controller中注入HelloService使用的是Dubbo提供的@Reference注解

  • 编写Controller

    package com.kkb.controller; 
    import com.alibaba.dubbo.config.annotation.Reference; 
    import com.kkb.service.HelloService; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller @RequestMapping("/demo") 
    public class HelloController { 
        @Reference 
        private HelloService helloService;
        
        @RequestMapping("/hello") 
        @ResponseBody 
        public String getName(String name){ 
            //远程调用 
            String result = helloService.sayHello(name); 		
            System.out.println(result);
            return result; 
        } 
    }
    
  • 在src/main/resources下创建applicationContext-web.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:p="http://www.springframework.org/schema/p" 					xmlns:context="http://www.springframework.org/schema/context" 		xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 				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 				http://www.springframework.org/schema/mvc/spring-mvc.xsd 			 http://code.alibabatech.com/schema/dubbo 								http://code.alibabatech.com/schema/dubbo/dubbo.xsd 					http://www.springframework.org/schema/context 						http://www.springframework.org/schema/context/spring-context.xsd"> 
        
        <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 --> 
        <dubbo:application name="dubbodemo-consumer" /> 
        <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址--> 			<dubbo:registry address="zookeeper://192.168.134.129:2181"/> 
        <!-- 扫描的方式暴露接口 --> 
        <dubbo:annotation package="com.kkb.controller" /> 
    </beans>
    
  • 运行测试

    tomcat7:run启动

    在浏览器输入http://localhost:8082/demo/hello.do?name=xiaomi,查看浏览器输出结果.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值