β

SpringBoot整合dubbo

Harries Blog™ 6 阅读

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配置可以参考 git hub.com/JeffLi1993/…

源码 下载 :大老杨码

个人 网站 :www.dalaoyang.cn

原文

https ://juejin.im/post/5b21d6696fb9a01e4e5e6e63

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。 PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处: Harries Blog™ » SpringBoot整合dubbo

作者:Harries Blog™
追心中的海,逐世界的梦
原文地址:SpringBoot整合dubbo, 感谢原作者分享。

发表评论