前言:

2023-12-26 19:38:05

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

版本信息:

springboot:2.7.6Dubbo:2.7.3zookeeper:3.8.0jdk:1.8idea:2021.3.2

Maven坐标:

                    org.springframework.boot            spring-boot-starter-web                            org.projectlombok            lombok            true                                org.springframework.boot            spring-boot-starter-test            test                                org.apache.dubbo            dubbo-spring-boot-starter            2.7.3                            com.github.sgroschupf            zkclient            0.1                            org.apache.curator            curator-framework            2.12.0                            org.apache.curator            curator-recipes            2.8.0                                org.apache.zookeeper            zookeeper            3.4.14                                                org.slf4j                    slf4j-log4j12                                        

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

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableDubbopublic class ProviderApplication {    public static void main(String[] args) {        SpringApplication.run(ProviderApplication.class,args);    }}

TimeServiceImpl.java

import com.example.dubbo.TimeService;import org.apache.dubbo.config.annotation.Service;@Service //注意引入的是dubbo包下的Servicepublic class TimeServiceImpl implements TimeService {    @Override    public String getTime() {        System.out.println("provider被调用啦!");        return "Hello,Dubbo!";    }}

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

application-provider.properties

server.port=8082dubbo.application.name=provider-app

消费者:

ConsumerApplication.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;@SpringBootApplicationpublic class ConsumerApplication {    public static void main(String[] args) {        SpringApplication.run(ConsumerApplication.class,args);    }}

indexController.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;@RestControllerpublic class indexController {    @Reference//Duboo包下的    private TimeService timeService;    @GetMapping("/")    public String getTime(){        System.out.println("consumer被调用啦!");        return timeService.getTime();    }}

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

application-consumer.properties:

server.port=8081dubbo.application.name=consumer-app

公共类:

TimeService.java:

public interface TimeService {    String getTime();}

公共配置:

application.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

本文来自博客园,作者:Lollipop_pro,转载请注明原文链接:https://www.cnblogs.com/CooperCode/p/17929281.html