美文网首页学习
Springboot2.X+Alibaba-Dubbo+Zook

Springboot2.X+Alibaba-Dubbo+Zook

作者: 疯狂大蜗牛 | 来源:发表于2019-02-20 19:06 被阅读72次

    直接切入正题,springboot2.x 集成alibaba-dubbo真的非常方便。因为dubbo需要服务注册中心,这里我们使用推荐的zookeeper。

    整个项目搭建的第一步就是搭建zookeeper。可以去zookeeper官网下载最新的稳定版 官网地址:http://mirrors.shu.edu.cn/apache/zookeeper/
    下载完成后解压,进入conf文件夹修改zoo_sample.cfg 为zoo.cfg并修改为如下内容(主要是各种路径的修改)

    # 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=/home/jason/Tools/zookeeper-3.4.12/zookeeperdir/zookeeper-data
    dataLogDir=/home/jason/Tools/zookeeper-3.4.12/zookeeperdir/logs
    # 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
    

    然后进入bin文件夹启动zookeeper服务。(根据os系统选择启动文件linux->.sh, windows->.cmd)
    zkServer.sh start
    或者
    zkServer.cmd

    jason@jason-lenovo:~/Tools/zookeeper-3.4.12/bin$ zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /home/jason/Tools/zookeeper-3.4.12/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    

    至此zookeeper已经启动成功,下面开始springboot+dubbo搭建
    先搭建一个空的springboot项目,然后新建两个module,一个服务提供者provider,一个服务消费者consumer。
    我们先来看看provider的pom文件,主要是引入了dubbo-spring-boot-starter,这个真的挺方便的。

    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.***</groupId>
        <artifactId>dubbo-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>dubbo-service</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <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>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>0.2.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    在application.properties文件添加项目配置,主要是zookeeper的配置

    spring.application.name= springboot-dubbo
    server.port=9090
    
    demo.service.version = 1.0.0
    
    dubbo.scan.base-packages=com.***.dubboservice.serviceImpl
    
    dubbo.application.id = springboot-dubbo
    dubbo.application.name=springboot-dubbo
    
    dubbo.protocol.id = dubbo
    dubbo.protocol.name = dubbo
    dubbo.protocol.port = 20880
    
    dubbo.registry.id = my-registry1
    dubbo.registry.address=zookeeper://localhost:2181
    

    新建一个Service接口 HelloService

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

    新建接口实现类HelloServiceImpl,注意这里的@Service注解为dubbo注解而不是spring注解。
    此@Service注解会将服务注册至zookeeper。

    import com.alibaba.dubbo.config.annotation.Service;
    import com.***.dubboservice.HelloService;
    
    @Service
    public class HelloServiceImpl implements HelloService {
        @Override
        public String sayHello(String name){
            return "Hello "+name;
        }
    }
    

    最后注意springboot启动类上需要添加@EnableDubbo注解,至此provider全部完成。

    @SpringBootApplication
    @EnableDubbo
    public class DubboServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboServiceApplication.class, args);
        }
    
    }
    

    接下来需要实现consumer
    同样是新建一个module,pom文件与provider一致。需要引入dubbo-spring-boot-starter

    在开发之前需要做的事情是将provider引入至consumer,itellij中设置引入module dependency或者将provider打jar包的方式引入 Screenshot from 2019-02-20 18-53-27.png
    修改properties文件,同样注意别忘记服务注册中心zookeeper的配置
    spring.application.name = dubbo-consumer-demo
    server.port=8080
    
    demo.service.version = 1.0.0
    
    dubbo.application.id = dubbo-consumer-demo
    dubbo.application.name = dubbo-consumer-demo
    
    dubbo.protocol.id = dubbo
    dubbo.protocol.name = dubbo
    dubbo.protocol.port = 12345
    
    dubbo.registry.address=zookeeper://localhost:2181
    

    现在开发一个简单的Controller, 注意@Reference注解,该注解也是Dubbo注解用来注入HelloService.
    如果注入的地方报错,先检查一下dependency有没有问题。

    @RestController
    public class sayHelloController {
        @Reference
        HelloService helloService;
    
        @GetMapping("/hello")
        public ResponseEntity hello(){
            String a = helloService.sayHello("jason");
            return ResponseEntity.ok(a);
        }
    }
    

    最后就是启动类了,同样需要@EnableDubbo注解

    @EnableDubbo
    @SpringBootApplication(scanBasePackages = {"com.***.dubboweb.controller"})
    public class DubboWebApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboWebApplication.class, args);
        }
    
    }
    

    至此provider和consumer全部完成,分别启动provider和consumer。
    从浏览器访问接口:http://localhost:8080/hello得到如下结果,至此搭建结束,mark以下。

    Screenshot from 2019-02-20 19-04-13.png

    相关文章

      网友评论

        本文标题:Springboot2.X+Alibaba-Dubbo+Zook

        本文链接:https://www.haomeiwen.com/subject/vsxkyqtx.html