Spingboot整合Dubbo+zookeeper

前言:

2023-12-26 19:38:05

最近学习分布式技术:Dubbo+zookeeper,准备写一个demo用springboot整合dubbo和zookeeper。但是看了网上一些教程都是几年前的,试着跟着写了几个demo没一个跑起来,基本是maven依赖方面的问题。

版本信息:

java 复制代码
springboot:2.7.6
Dubbo:2.7.3
zookeeper:3.8.0
jdk:1.8
idea:2021.3.2

Maven坐标:

xml 复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</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.14</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

Demo项目:

项目准备:

1、点击创建一个springboot项目

2、勾选web和lombok

3、创建

4、修改IDEA的Maven配置

5、刷新Maven

6、运行测试

7、修改pom配置文件

8、刷新maven

项目结构:

src:

├─main

│ ├─java

│ │ └─com

│ │ └─example

│ │ └─dubbo

│ │ │ DubboApplication.java

│ │ │ TimeService.java

│ │ │

│ │ ├─consumer

│ │ │ │ ConsumerApplication.java

│ │ │ │

│ │ │ └─controller

│ │ │ indexController.java

│ │ │

│ │ └─provider

│ │ │ ProviderApplication.java

│ │ │

│ │ └─impl

│ │ TimeServiceImpl.java

│ │

│ └─resources

│ │ application-consumer.properties

│ │ application-provider.properties

│ │ application.properties

│ │

│ ├─static

│ └─templates

生产者:

ProviderApplication.java

java 复制代码
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

TimeServiceImpl.java

java 复制代码
import com.example.dubbo.TimeService;
import org.apache.dubbo.config.annotation.Service;


@Service //注意引入的是dubbo包下的Service
public class TimeServiceImpl implements TimeService {

    @Override
    public String getTime() {
        System.out.println("provider被调用啦!");
        return "Hello,Dubbo!";
    }
}

注意要将ProviderApplication.java的运行配置文件改为provider!

application-provider.properties

properties 复制代码
server.port=8082
dubbo.application.name=provider-app

消费者:

ConsumerApplication.java

java 复制代码
import com.example.dubbo.TimeService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;

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

indexController.java

java 复制代码
import com.example.dubbo.TimeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class indexController {

    @Reference//Duboo包下的
    private TimeService timeService;

    @GetMapping("/")
    public String getTime(){
        System.out.println("consumer被调用啦!");
        return timeService.getTime();
    }
}

注意要将ConsumerApplication.java的运行配置文件改为consumer!

application-consumer.properties:

properties 复制代码
server.port=8081
dubbo.application.name=consumer-app

公共配置:

application.properties:

properties 复制代码
# 换成你自己zookeeper服务的地址
dubbo.registry.address=zookeeper://localhost:2181

启动:

依次启动zookeeper、ProviderApplication.java、ConsumerApplication.java

项目地址:

百度网盘:https://pan.baidu.com/s/1dyxzUGJaiQUF0ibXGKBITA

提取码:ex2s

gitee:https://gitee.com/qinglianguzhou/dbuuo-demo.git