dubbo消费者调用多个服务_Dubbo配合SpringBoot,实现接口多个实现(group)

本文介绍了如何在SpringBoot项目中结合Dubbo,使用@Service和@Reference的group属性来实现一个接口的多个实现。首先,详细讲解了Zookeeper的安装配置过程,包括解决启动时可能出现的问题。然后,展示了生产者和消费者的项目结构,以及相关配置,如@Service和@Reference的group设置。最后,简要提到了Dubbo-admin的使用和消费者启动后的验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot配合Dubbo,使用@Service和@Reference,group实现接口多实现

公司项目升级,需要实现springBoot + Dubbo,并支持一个接口多个实现的情况。遇到了几个坑,在这里记录下。

1. 安装Zookeeper

在 官网 上下载最新版本3.5.6(注意下载 bin 包)

将下载好的压缩包,解压到对应目录

cd apache-zookeeper-3.5.6-bin/conf/ // 切换到配置目录

mv zoo_sample.cfg zoo.cfg //更改默认配置文件名称

vi zoo.cfg // 编辑配置文件,自定义dataDir,我的配置贴在下面

这里会遇到第一个坑:zk从3.5.5开始,带有bin名称的包才是我们想要的下载可以直接使用的里面有编译后的二进制的包,而tar.gz的包里面是只是源码,无法直接使用。不小心下了 apache-zookeeper-3.5.6.tar.gz 这个包,启动时会报错"错误: 找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain"。

zoo.cfg

# 发送心跳的间隔时间, 毫秒为单位

tickTime=2000

# 集群用到,初始化连接时,leader最多能容忍client多少个心跳间隔,默认是10,就代表10*2000=20000 , 毫秒

initLimit=10

# 集群用到,正常通信时,如果client超过此时间间隔没有心跳

syncLimit=5

# 保存数据的目录

dataDir=/Users/ouitsu/document/apache-zookeeper-3.5.6-bin/data

# 暴露的接口

clientPort=2181

# zk3.5的新特性,Zookeeper AdminServer,默认端口是8080,坑的一批

admin.serverPort=12181

这里是第二个坑,zk的Zookeeper AdminServer默认端口和Dubbo admin默认端口冲突,会报错:Unable to start AdminServer, exiting abnormally org.apache.zookeeper.server,在这里定义端口即可解决。

配置完成,开始启动zk

cd到zookeeper的bin目录下

./zkServer.sh start 启动

./zkServer.sh stop 停止

开始搭建项目

1. 生产者

目录结构如下:

provider_pom文件如下

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.0.RELEASE

com.uaepay.pay.channel

dubbo_provider

0.0.1-SNAPSHOT

dubbo_provider

Demo project for Spring Boot

1.8

com.alibaba.spring.boot

dubbo-spring-boot-starter

2.0.0

org.springframework.boot

spring-boot-devtools

runtime

true

uaepay.cmf.channel

dubbo_facade

1.0-SNAPSHOT

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

io.projectreactor

reactor-test

test

com.101tec

zkclient

0.10

org.apache.zookeeper

zookeeper

3.4.12

org.springframework.boot

spring-boot-starter-web

2.2.0.RELEASE

org.springframework.boot

spring-boot-maven-plugin

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

provider_application:

package com.uaepay.pay.channel.dubbo_provider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@EnableDubbo //启动dubbo的配置

public class DubboProviderApplication {

public static void main(String[] args) {

SpringApplication.run(DubboProviderApplication.class, args);

}

两个生产实现

package com.uaepay.pay.channel.dubbo_provider.service;

import com.alibaba.dubbo.config.annotation.Service; // 这里要注意引用dubbo的Service

import org.springframework.stereotype.Component; // 对应的,使用spring的Component

import uaepay.cmf.channel.MockResponseService;

import uaepay.cmf.channel.vo.User;

@Service(group = "mockResponseServiceImpl") // 定义group

@Component

public class MockResponseServiceImpl implements MockResponseService {

@Override

public User getUserInfo() {

User user = new User();

user.setUserName("1111");

return user;

}

}

package com.uaepay.pay.channel.dubbo_provider.service;

import com.alibaba.dubbo.config.annotation.Service;// 使用alibaba.dubbo的Service注解

import org.springframework.stereotype.Component;// 使用Spring的Component注解

import uaepay.cmf.channel.MockResponseService;

import uaepay.cmf.channel.vo.User;

@Service(group = "mockResponseServiceImpl2") //定义group

@Component

public class MockResponseServiceImpl2 implements MockResponseService {

@Override

public User getUserInfo() {

User user = new User();

user.setUserName("222222");

return user;

}

}

application.properties

dubbo.application.name=dubbo-provider-service

dubbo.registry.address=zookeeper://127.0.0.1:2181

dubbo.protocol.port=20782

dubbo.protocol.dispatcher=message

dubbo.protocol.threadpool=fixed

dubbo.protocol.threads=200

server.port=8871

2. 消费者

目录结构如下:

pom文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.0.RELEASE

com.uaepay.pay.channel

dubbo_consumer

0.0.1-SNAPSHOT

dubbo_consumer

Demo project for Spring Boot

1.8

com.alibaba.spring.boot

dubbo-spring-boot-starter

2.0.0

org.springframework.boot

spring-boot-devtools

runtime

true

uaepay.cmf.channel

dubbo_facade

1.0-SNAPSHOT

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

com.101tec

zkclient

0.10

org.apache.zookeeper

zookeeper

3.4.12

org.springframework.boot

spring-boot-starter-web

2.2.0.RELEASE

org.springframework.boot

spring-boot-configuration-processor

true

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

com.alibaba

fastjson

1.2.28

org.springframework.boot

spring-boot-maven-plugin

ConsumerApplication:

package com.uaepay.pay.channel.dubbo_consumer;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@EnableDubbo

public class DubboConsumerApplication {

public static void main(String[] args) {

SpringApplication.run(DubboConsumerApplication.class, args);

}

}

消费者的实现类

package com.uaepay.pay.channel.dubbo_consumer.service;

import com.alibaba.dubbo.config.annotation.Reference;// 使用Dubbo的reference 获取对应实现

import com.alibaba.fastjson.JSON;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import uaepay.cmf.channel.MockResponseService;

import java.util.LinkedHashMap;

import java.util.TreeMap;

@RestController

@RequestMapping("/test")

public class MockServiceImpl {

@Reference(group = "mockResponseServiceImpl")//对应定义group

private MockResponseService mockResponseServiceImpl;

@Reference(group = "mockResponseServiceImpl2")// 对应定义group

private MockResponseService mockResponseServiceImpl2;

@RequestMapping("/getUserInfo")

public void getUserInfo(){

System.out.println("得到的详情为..."+JSON.toJSONString(mockResponseServiceImpl.getUserInfo()));

}

@RequestMapping("/getUserInfo1")

public void getUserInfo11(){

System.out.println("得到的详情为..."+JSON.toJSONString(mockResponseServiceImpl2.getUserInfo()));

}

}

application.properties

dubbo.application.name=dubbo-consumer-service

dubbo.registry.address=zookeeper://127.0.0.1:2181

dubbo.consumer.check=false

server.port=8862

facade包

项目结构如下:

pom文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

uaepay.cmf.channel

dubbo_facade

1.0-SNAPSHOT

dubbo_facade

http://www.example.com

UTF-8

1.7

1.7

junit

junit

4.11

test

org.projectlombok

lombok

true

commons-lang

commons-lang

2.4

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-jar-plugin

3.0.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

maven-site-plugin

3.7.1

maven-project-info-reports-plugin

3.0.0

实体类

package uaepay.cmf.channel.vo;

import java.io.Serializable;

public class User implements Serializable {

private String userName;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

}

接口

package uaepay.cmf.channel;

import uaepay.cmf.channel.vo.User;

public interface MockResponseService {

public User getUserInfo();

}

运行项目

启动provider

Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session

Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x100000dd3c5000c, negotiated timeout = 30000

成功打印出这些,说明生产者已经注册成功,如果未出现,检查一下是不是没引入com.101tec.zkclient的依赖,或者zk是否成功启动。

启动Consumer

has been built.

has been built.

成功打印这个,证明消费者配置成功。

如果打印的是

重要的子模块有两个:Dubbo-admin-server和Dubbo-admin-ui

Dubbo-admin-server:

默认端口8080

是一个springboot项目,直接启动即可

Dubbo-admin-ui:

cd到Dubbo-admin-ui路径,npm install 安装依赖

依赖安装完成,npm run dev

运行成功打开http://localhost:8081 即可

如果有具体步骤不懂,可以查看项目中的README.md(子模块中也有),写的很详细。

至此,大功告成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值