Dubbo快速入门

本文是一篇关于Dubbo的快速入门教程,详细介绍了如何创建服务提供方(provider)和消费方(consumer)。从创建maven工程、配置web.xml、定义服务接口和实现,到启动zookeeper、注册服务,再到消费方的接口复制、controller编写,最后进行服务调用测试,整个过程清晰明了。
摘要由CSDN通过智能技术生成

Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。在这个案例中我们要创建两个应用,一个作为服务的提供方(provider),一个作为服务的消费方(consumer)。通过Dubbo来实现服务消费方远程调用服务提供方的方法。

1. 服务提供方的开发

1.1 创建maven工程(打包方式为war)

首先创建一个空的工程,然后我们在其下创建maven子工程dubbodemo_provider,在它的pom.xml文件中添加如下依赖:

<?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.yuhong</groupId>
    <artifactId>dubbodemo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>
		<packaging>war</packaging>

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

    <dependencies>
        <!--spring先关依赖-->
        <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-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>
        <!--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>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8081</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 注意:如果本地有多个provider时,tomcat需要设置不同的端口号,避免冲突。

 1.2 配置web.xml文件

首先我们要在/src/main路径下手动创建一个webapp文件夹,点击Maven刷新之后,文件夹上可以看到一个蓝点。

接着我们去project structure中创建相应的web.xml文件。Facets → Web (选中要添加的项目)→Deployment Descriptors中添加路径,找到我们刚才创建的webapp文件夹,其后加上\WEB-INF\web.xml即可

 在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:applicationContext*.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
</web-app>

这里applicationContext.xml文件还没有配置,所以会被标红,下文会进行相应配置 

1.3 创建服务接口

public interface HelloService {
    public String sayHello(String name);
}

1.4 创建服务实现类

@Service
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "hello" + name;
    }
}

注意:服务实现类上使用的Service注解是Dubbo提供的,带有该注解的类如果被扫描就会被对外发布成服务

1.5 创建applicationContext-service.xml

在src/main/resources下创建该文件,并添加如下内容

<?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地址ifconfig获取-->
		<!--端口号通过jps    netstat 0tunpl可以查看-->
    <dubbo:registry address="zookeeper://localhost:2181"/> 
    <!-- 注册 协议和port, 默认端口20880 --> 
    <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
    <!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
    <dubbo:annotation package="com.yuhong.service.impl"/>
</beans>

这里本人使用的是在虚拟机的linux上运行的zookeeper,通过ifconfig获取ip地址填入即可

最终呈现的目录如下

 1.6 启动服务测试

首先在虚拟机的linux中起到zookeeper。找到zookeeper所在文件夹中的bin子文件夹,运行 ./zkServer.sh start 来启动zookeeper

通过tomcat7:run来启动服务

然后在linux中运行 ./zkCli.sh来查看注册情况

上图中可以看到dubbo中已经有了provider这项服务

2. 服务消费方开发

2.1 创建maven子工程

和创建provider的方式相同,我们在创建一个consumer的子工程,不同的地方是,要将tomcat的端口号改为不同的端口号,如8082

2.2 配置web.xml文件

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:applicationContext*.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-web.xml</param-value>
        </init-param>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 2.3 复制服务接口

将服务提供者工程中的HelloService接口复制到当前工程, 不需要实现类

2.4 编写controller

@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;
    }
}

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

2.5 在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://localhost:2181"/>
    <!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
    <dubbo:annotation package="com.yuhong.controller"/>
</beans>

同样上图中的localhost要改为zookeeper所在的Ip地址

最终呈现的目录如下:

 2.6 运行测试

要注意运行顺序:先运行zookeeper→再运行provider→最后运行consumer

在浏览器中输入http://localhost:8082/demo/hello?name=Anna来查看结果

 

 注意:如果运行顺序不对,肯能会报错 java.lang.NullPointerException

此时我们在zookepper中查看时,会发现服务提供者的内容为空

只要关闭服务重新按照正确的方式启动就能解决。

以上就是本次操作的所有内容 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值