2020超新Dubbo的简单使用(保姆级)

分布式RPC框架Dubbo

1. 下载与安装服务注册中心zookeeper

  • 之前的文章已经发布了zookeeper的下载与安装教程,如果忘记了的小伙伴点击这里传送门zookeeper的下载与安装

2. Dubbo快速入门

2.1 服务提供方开发

开发步骤:

(1)创建maven工程(打包方式为war)dubbodemo_provider,在pom.xml文件中导入如下坐标

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
    <spring.version>5.2.2.RELEASEspring.version>
  properties>
  <dependencies>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jmsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
      <version>2.6.8version>
    dependency>
    <dependency>
      <groupId>org.apache.zookeepergroupId>
      <artifactId>zookeeperartifactId>
      <version>3.6.0version>
    dependency>
    <dependency>
      <groupId>com.github.sgroschupfgroupId>
      <artifactId>zkclientartifactId>
      <version>0.1version>
    dependency>
    <dependency>
      <groupId>javassistgroupId>
      <artifactId>javassistartifactId>
      <version>3.12.1.GAversion>
    dependency>
    <dependency>
      <groupId>org.apache.curatorgroupId>
      <artifactId>curator-frameworkartifactId>
      <version>4.0.1version>
    dependency>
    <dependency>
      <groupId>org.apache.curatorgroupId>
      <artifactId>curator-recipesartifactId>
      <version>4.0.1version>
    dependency>

    <dependency>
      <groupId>io.nettygroupId>
      <artifactId>netty-allartifactId>
      <version>4.1.48.Finalversion>
    dependency>

    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.68version>
    dependency>
  dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.8.0version>
        <configuration>
          <source>1.8source>
          <target>1.8target>
        configuration>
      plugin>
      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <version>2.2version>
        <configuration>
          
          <port>8081port>
          
          <path>/path>
        configuration>
      plugin>
    plugins>
  build>

(2)配置web.xml文件


<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 Applicationdisplay-name>
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext*.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
web-app>

(3)创建服务接口

package com.yogurt.service;

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

(4)创建服务实现类

package com.yogurt.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yogurt.service.HelloService;

@Service  // 使用dubbo的Service注解
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

注意:服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务

(5)在src/main/resources下创建applicationContext-service.xml


<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">dubbo:application>
    
    <dubbo:registry address="zookeeper://39.96.85.96:2181">dubbo:registry>
    
    <dubbo:protocol name="dubbo" port="20880">dubbo:protocol>
    
    <dubbo:annotation package="com.yogurt.service.impl">dubbo:annotation>
beans>
2.2 服务消费方开发

开发步骤:

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

(2)配置web.xml文件


<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 Applicationdisplay-name>
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:applicationContext-web.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

(3)将服务提供者工程中的HelloService接口复制到当前工程

(4)编写Controller

package com.yogurt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yogurt.service.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @Reference
    private HelloService helloService;
    
    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello(String name){
        return helloService.sayHello(name);
    }
}

(5)在src/main/resources下创建applicationContext-web.xml


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

    
    <context:component-scan base-package="com.xin.controller" />
    
    <mvc:annotation-driven/>
    
    <dubbo:application name="dubbodemo_consumer" />
    
    <dubbo:registry address="zookeeper://39.96.85.96:2181"/>
    
    <dubbo:annotation package="com.yogurt.controller" />
beans>

(6)运行测试

tomcat7:run启动 (保证服务器上的zookeeper正常启动,先启动dubbo_provider,再启动dubbo_consumer)

访问:http://localhost:8082/hello?name=qq 进行测试

3. Dubbo管理控制台

github下载地址:https://github.com/apache/dubbo-admin

4. 使用dubbo可能出现的问题

4.1 启动控制台报错

java.lang.IllegalStateException: No ConfigurableListableBeanFactory set

解决:dubbo的版本和Spring的版本不支持,修改Spring的版本,或者dubbo的版本

经测试:spring 5.0.5 RELEASE + dubbo 2.6.0 + zookeeper 3.4.14 可用

如果使用 dubbo 2.6.8 + zookeeper 3.6.0 版本 需要引入curator jar包

<dependency>
      <groupId>org.apache.curatorgroupId>
      <artifactId>curator-frameworkartifactId>
      <version>4.0.1version>
dependency>

重要:

解决:dubbo的版本和Spring的版本不支持,修改Spring的版本,或者dubbo的版本

经测试:spring 5.0.5 RELEASE + dubbo 2.6.0 + zookeeper 3.4.14 可用

如果使用 dubbo 2.6.8 + zookeeper 3.6.0 版本 需要引入curator jar包

<dependency>
      <groupId>org.apache.curatorgroupId>
      <artifactId>curator-frameworkartifactId>
      <version>4.0.1version>
dependency>

重要:

如果在使用过程中,提示的信息有注册dubbo失败(Failed to register dubbo),确保端口开放,防火墙允许(在本机运行则忽略),如果还有错误提示,尝试更换spring和dubbo以及zookeeper的版本,极有可能是兼容问题。

你可能感兴趣的:(zookeeper)