搭建 SpringBoot+zk+dubbo

搭建 SpringBoot+zk+dubbo

一, zk

  1. 下载zookeeper :zookeeper官网
  2. 解压完成后,将 conf 目录下的 zoo_sample.cfg 复制一份,重命名为:zoo.cfg
  3. 在zookeeper目录下,新建一个data文件夹,编辑zoo.cfg,将 dataDir 的值改为data文件夹的路径
  4. 进入bin目录,编辑zkServer.cmd,在倒数第二行增加 pause(运行出错后不会闪退)zkServer.cmd

SpringBoot+dubbo

导入依赖(注意Springboot和Dubbo版本):

		<parent>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-parent</artifactId>
	        <version>2.3.0.RELEASE</version>
	        <relativePath/> <!-- SpringBoot的版本号 -->
	    </parent>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

生产者

生产者项目结构

TestService

package com.producer.service;

public interface TestService {

    public String sayHello(String name);
}

TestServiceImpl

package com.producer.service.impl;

import com.producer.service.TestService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Service(version = "1.0.0",interfaceClass = TestService.class)
@Component
public class TestServiceImpl implements TestService{

    @Override
    public String sayHello(String name){
        return "hello,"+name;
    }
}

ProducerApplication

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
//生产者和消费者都要加这个注解,开启dubbo
@EnableDubbo
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }

}

application.properties

//端口号,防止冲突
server.port=8001

//应用名称
dubbo.application.name=dubbo-producer
//注册地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
//协议名称
dubbo.protocol.name=dubbo
//暴露端口号,-1为随机
dubbo.protocol.port=-1
//扫描服务注册bean,即service实现类所在的包
dubbo.scan.base-packages=com.producer.service

消费者

生产者和消费者的 TestService 的目录一定要一样,连一个字母都不能错,负责 生产者 会无法找到

消费者项目结构

TestClass

package com.producer.service.impl;

import com.producer.service.TestService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

@Component
public class TestClass {

    @Reference(version = "1.0.0", url = "dubbo://127.0.0.1:20880?version=1.0.0")
    //url后面要跟上版本号,不然会报错
    TestService testService;
    public String sayHello(String name){
        return testService.sayHello(name);
    }
}

TestController

package com.producer.conrtroller;

import com.producer.service.impl.TestClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    TestClass testClass;
    
    @RequestMapping("/sayHello/{name}")
    public String sayHello(@PathVariable("name") String name){
        return testClass.sayHello(name);
    }
}

application.properties

server.port=8002

dubbo.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.base-packages=com.consumer.service

启动

  1. 启动zookeeper bin 目录下的 zkServer.cmd,zkCli.cmd
  2. 启动生产者 和 消费者
  3. 访问:http://www.localhost:8002/sayHello/kobe

访问成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值