1. 程式人生 > >SpringBoot整合dubbo

SpringBoot整合dubbo

pri not pack gui int ise 1.2 1.0 ram

Dubbo是阿裏巴巴公司開源的一個高性能優秀的服務框架,使得應用可通過高性能的 RPC 實現服務的輸出和輸入功能,可以和Spring框架無縫集成。

以上介紹來源於百度百科,具體dubbo相關可以自行查找資料,本文只是介紹SpringBoot簡單整合dubbo。

1.安裝zookeeper

1.1 去官網下載,本文以3.4.12 版本為例子http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/

1.2 下載之後解壓ZooKeeper

1.3 進入解壓文件夾conf目錄

1.4 將zoo_sample.cfg修改名稱為zoo.cfg

1.5 修改內容為如下,註意,本人是將ZooKeeper解壓到了e盤,具體

dataDir和dataDirLog屬性可以根據自己情況修改。

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=E:\\zookeeper\\data  
dataDirLog=E:\\zookeeper\\log  
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

1.6 啟動zookeeper,進入bin目錄,雙擊zkServer.cmd

2 新建項目springboot_dubbo_server,項目中加入dubbo依賴,完整pom如下:

<?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.dalaoyang</groupId>
    <artifactId>springboot_dubbo_server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot_dubbo_server</name>
    <description>springboot_dubbo_server</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

配置文件如下:

##端口號
server.port=8880

## Dubbo 服務消費者配置
spring.dubbo.application.name=dubbo_server
spring.dubbo.registry.address=zookeeper://39.108.123.128:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.dalaoyang.dubbo

定義一個Service Interface:HelloService.java

package com.dalaoyang.dubbo;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.dubbo
 * @email [email protected]
 * @date 2018/6/14
 */
public interface HelloService {
    String SayHello(String name);
}

接口的實現類:HelloServiceImpl.java

package com.dalaoyang.dubbo.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.dalaoyang.dubbo.HelloService;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.dubbo.imp
 * @email [email protected]
 * @date 2018/6/14
 */
@Service(version = "1.0.0")
public class HelloServiceImpl implements HelloService {

    @Override
    public String SayHello(String name) {
        return "Hello , "+name;
    }
}

到這裏dubbo服務提供者已經創建完成。

3.新建項目springboot_dubbo_client,pom與提供者一致,代碼如下:

<?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.dalaoyang</groupId>
    <artifactId>springboot_dubbo_client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot_dubbo_client</name>
    <description>springboot_dubbo_client</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

配置文件如下:

## 端口號
server.port=8881

##Dubbo配置
spring.dubbo.application.name=dubbo_client
spring.dubbo.registry.address=zookeeper://39.108.123.128:2181
spring.dubbo.scan=com.dalaoyang.controller

HelloService接口如下:

package com.dalaoyang.dubbo;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.dubbo
 * @email [email protected]
 * @date 2018/6/14
 */
public interface HelloService {
    String SayHello(String name);
}

創建一個controller進行測試,註意版本號要與提供者的版本號一致,dubbo掃描包要掃描到我們要使用的類上,代碼如下:

package com.dalaoyang.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dalaoyang.dubbo.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email [email protected]
 * @date 2018/6/14
 */
@RestController
public class HelloController {

    @Reference(version = "1.0.0")
    HelloService helloService;

    @GetMapping("sayHello")
    public String sayHello(String name){
        return helloService.SayHello(name);
    }
}

到這裏dubbo服務調用者也創建完成。

分別啟動服務提供者項目和服務調用者項目,在瀏覽器訪問http://localhost:8881/sayHello?name=dalaoyang,如圖,證明調用成功。

技術分享圖片

更多springboot-dubbo配置可以參考https://github.com/JeffLi1993/springboot-learning-example/blob/master/springboot-dubbo-server/DubboProperties.md

源碼下載 :大老楊碼雲

個人網站:https://www.dalaoyang.cn

關註作者公眾號

技術分享圖片

SpringBoot整合dubbo