部署2021年dubbo3.0相关的版本
SpringBoot 2.4.3安装 zookeeper-3.7.0 单机引入依赖 dubbo-spring-boot-starter 3.0.4、org.apache.curator 5.2.0
Zookeeper下载安装Curator 框架提供了一套高级的 API,简化了 ZooKeeper 的操作。它增加了很多使用 ZooKeeper 开发的特性,可以处理 ZooKeeper 集群复杂的连接管理和重试机制。
下载地址
https://dlcdn.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
安装教程
https://dubbo.apache.org/zh/docs/v2.7/admin/install/zookeeper/
SpringBoot 2.4.3,项目名称 micro-dunno
业务提供者没有controller层,消费者只有controller对外暴露服务
pom.xml
创建 common 模块4.0.0 org.example.dubbo micro-dubbo pom 1.0-SNAPSHOT common consumer provider org.springframework.boot spring-boot-starter-parent 2.4.3 UTF-8 UTF-8 1.8 1.8 1.8 org.springframework.boot spring-boot-starter com.alibaba fastjson 1.2.75
公共 api
package org.example.dubbo.common;
public interface UserService {
public String getUserInfo();
}
pom.xml
创建 provider 项目(服务提供者)micro-dubbo org.example.dubbo 1.0-SNAPSHOT 4.0.0 org.example.dubbo.common common
引入依赖 pom.xml
启动类micro-dubbo org.example.dubbo 1.0-SNAPSHOT 4.0.0 org.example.dubbo.server provider org.example.dubbo.common common 1.0-SNAPSHOT compile org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter 3.0.4 org.apache.curator curator-x-discovery 5.2.0 org.springframework.boot spring-boot-maven-plugin
ProviderApplication.java
package org.example.dubbo.server;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
System.out.println("-----run ok-----");
}
}
实现业务
实现接口业务 UserServiceImpl.java
package org.example.dubbo.server;
import org.example.dubbo.common.UserService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
@Override
public String getUserInfo(){
return "小明";
}
}
application.yml
server:
port: 5801
logging:
level:
root: INFO
file:
name: ./dubbo-provider.log
dubbo:
application:
name: dubbo-provider #当前服务/应用的名字
registry: ##注册中心的协议和地址
address: zookeeper://127.0.0.1:2181
protocol: ##通信规则(通信协议和接口)
name: dubbo
port: 20880
创建 consumer 模块
pom.xml
micro-dubbo org.example.dubbo 1.0-SNAPSHOT 4.0.0 org.example.dubbo.consumer consumer org.example.dubbo.common common 1.0-SNAPSHOT compile org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter 3.0.4 org.apache.curator curator-x-discovery 5.2.0 org.springframework.boot spring-boot-maven-plugin
启动类 ConsumerApplication.java
package org.example.dubbo.consumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}}
Dubbo客户端
Dubbo客户端 DubboApiClient.java
package org.example.dubbo.consumer;
import org.apache.dubbo.config.annotation.DubboReference;
import org.example.dubbo.common.UserService;
import org.springframework.stereotype.Component;
@Component
public class DubboApiClient {
@DubboReference(check = false)
private UserService userService;
public String getUserInfo(){
return userService.getUserInfo();
}
}
对外暴露接口
UserController.java
package org.example.dubbo.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private DubboApiClient dubboApiClient;
@RequestMapping("/getUser")
public String getUser(){
return dubboApiClient.getUserInfo();
}
}
配置文件 application.yml
server:
port: 5805
logging:
level:
root: INFO
file:
name: ./dubbo-consumer.log
dubbo:
application:
name: dubbo-provider #当前服务/应用的名字
registry: #注册中心的协议和地址
address: zookeeper://127.0.0.1:2181
protocol: #通信规则(通信协议和接口)
name: dubbo
port: 20880
# consumer:
# check: false # 关闭所有服务的启动时检查:(默认没有提供者时报错) 写在定义服务消费者一方
# retries: 3 #失败重试次数
项目结构图
访问consumer 服务提供者的,输入访问地址 http://120.78.12.26:5805/getUser 后,返回远程接口提供者provider的具体业务实现结果。
完
参考地址https://blog.csdn.net/instanceof_zjl/article/details/122014926
https://blog.csdn.net/smartbetter/article/details/53083816
其他
SpringBoot中使用RMI进行远程方法调用
https://www.cnblogs.com/shamo89/p/10631320.html



