分散式框架Dubbo入門

_五月雨發表於2020-08-20

Dubbo簡介

Apache Dubbo是一款高效能的Java RPC框架。其前身是阿里巴巴公司開源的一個高效能、輕量級的開源Java RPC框架,可以和Spring框架無縫整合。

dubbo官方中文文件地址

Dubbo結構介紹

快速部署

一、配置註冊中心,官方推薦使用zookeeper

zookeeper說明文件
下載地址

  • zoo.cfg配置檔案介紹
// 資料儲存目錄,修改資料儲存目錄 ../data 複製的時候注意,小心空格
dataDir=/tmp/zookeeper 
//預設配置檔案,修改路徑如下
dataDir=../data 
// 埠
clientPort=2181
  • bin目錄下zkEnv檔案注意事項

  • 啟動zookeeper服務:zkServer

二、簡單Dubbo專案部署步驟

公共介面服務

1. 建立一個公共介面工程 dubbodemo_interface

詳情見maven使用連結

2. 建立一個HelloService介面

package com.atguigu.service;

/**
 * ClassName: HelloService
 * Description: 提供者服務層介面
 * date: 2020/8/20 10:44
 *
 * @author July
 * @since JDK 1.8
 */
public interface HelloService {
    String sayHello(String name);
}

服務提供者

1. 建立一個服務提供者dubbodemo_provider(maven web工程)

詳情見maven使用連結

2. 匯入maven座標

<?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.gtguigu</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>

    <dependency>
      <groupId>com.atguigu</groupId>
      <artifactId>dubbodemo_interface</artifactId> //匯入公共抽取介面座標
      <version>1.0-SNAPSHOT</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>8082</port>
          <!-- 請求路徑 -->
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

3. 建立提供者實現類

已經在提供者的pom.xml已經匯入了公共介面HelloService的座標,所以直接可以使用HelloService介面

//直接建立實現類
package com.atguigu.service.impl;

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

/**
 * ClassName: HelloServiceImpl
 * Description:
 * date: 2020/8/20 10:45
 *
 * @author July
 * @since JDK 1.8
 */
//注意,使用的是com.alibaba.dubbo.config.annotation.Service;
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

注意事項:@Service 使用的是com.alibaba.dubbo.config.annotation.Service

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_3_1.xsd"
         version="3.1">
  <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>

5. 用 Spring 配置宣告暴露服務

provida.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="hello-world-app"  />

    <!-- 使用註冊中心暴露服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 用dubbo協議在20880埠暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 掃描指定包,加入@Service註解的類會被髮布為服務  -->
    <dubbo:annotation package="com.atguigu.service.impl" />
</beans>

服務消費者(參照提供者配置)

1. 建立一個服務消費者dubbodemo_consumer(maven web工程)

詳情見maven使用連結

2. 匯入座標,和提供者相同,只需要把tomcat埠號修改下即可

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <!-- 指定埠 -->
          <port>8083</port>  //修改埠號,不要衝突
          <!-- 請求路徑 -->
          <path>/</path>
        </configuration>
      </plugin>

3. 通過 Spring 配置引用遠端服務

consumer.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://127.0.0.1:2181"/>
    <!-- 掃描的方式暴露介面  -->
    <dubbo:annotation package="com.atguigu.controller" />
    <!-- 執行dubbo不檢查提供者是否提前開啟  -->
    <!-- <dubbo:consumer check="false"></dubbo:consumer> -->
</beans>

4. 編寫消費者實現類

package com.atguigu.controller;

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

/**
 * ClassName: HelloController
 * Description:
 * date: 2020/8/20 11:09
 *
 * @author July
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/demo")
public class HelloController {
    @Reference //注意使用com.alibaba.dubbo.config.annotation.Reference
    private HelloService helloService; //由於已經在maven依賴中匯入了HelloService介面,所以可以直接呼叫

    //遠端呼叫
    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello(String name){
        String s = helloService.sayHello(name);
        System.out.println(s);
        return s;
    }
}

5. 配置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_3_1.xsd"
         version="3.1">

  <servlet>
    <servlet-name>springmvc</servlet-name>
      <!-- 配置前端控制器 -->
    <servlet-class>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>/</url-pattern>
  </servlet-mapping>

</web-app>

測試訪問

1. 啟動zookeeper服務 zkServer

2. 啟動tomcat服務

注意事項:需要先啟動提供者服務,在啟動消費者服務

3. 測試訪問資源


可以看到資源訪問沒問題

相關文章