转载

SpringBoot整合dubbo

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。

以上介绍来源于百度百科,具体dubbo相关可以自行查找资料,本文只是介绍SpringBoot简单整合dubbo。

1.安装zookeeper

1.1 去官网下载,本文以3.4.12 版本为例子 mirrors.hust.edu.cn/apache/zook…

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 yangyang@dalaoyang.cn
 * @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 yangyang@dalaoyang.cn
 * @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 yangyang@dalaoyang.cn
 * @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 yangyang@dalaoyang.cn
 * @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

更多springboot-dubbo配置可以参考 github.com/JeffLi1993/…

源码下载 :大老杨码云

个人网站:www.dalaoyang.cn

原文  https://juejin.im/post/5b21d6696fb9a01e4e5e6e63
正文到此结束
Loading...