dubbo整合springboot入门案例
dubbo整合springboot入门案例
创建server-common-api项目
server-common-api中创建消费者和提供者都需要的类,实现代码复用
用户地址实体类
package com.yl.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
/**
* 用户地址
*
* @author Y-wee
*/
@AllArgsConstructor
@Getter
@Setter
@ToString
public class UserAddress implements Serializable {
/**
* 主键
*/
private Integer id;
/**
* 用户地址
*/
private String userAddress;
/**
* 用户id
*/
private String userId;
/**
* 收货人
*/
private String consignee;
/**
* 电话号码
*/
private String phoneNum;
/**
* 是否为默认地址:Y-是,N-否
*/
private String isDefault;
}
订单服务接口
package com.yl.service;
import com.yl.entity.UserAddress;
import java.util.List;
/**
* 订单
*
* @author Y-wee
*/
public interface OrderService {
/**
* 初始化订单
*
* @param userId
*/
List<UserAddress> initOrder(String userId);
}
用户服务接口
package com.yl.service;
import com.yl.entity.UserAddress;
import java.util.List;
/**
* 用户
*
* @author Y-wee
*/
public interface UserService {
/**
* 按照用户id返回所有的收货地址
*
* @param userId
* @return
*/
List<UserAddress> getUserAddressList(String userId);
}
创建boot-server-user-provider项目
boot-server-user-provider实现用户相关业务,作为服务提供者
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.yl</groupId>
<artifactId>server-common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
用户业务实现类
package com.yl.boot.server.user.provider.service.impl;
import java.util.Arrays;
import java.util.List;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yl.entity.UserAddress;
import com.yl.service.UserService;
import org.springframework.stereotype.Service;
/**
* 用户
*
* @author Y-wee
*/
@Service
// 暴露服务,注意包名是dubbo的
@com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService {
/**
* 获取用户地址
*
* @param userId
* @return
*/
@Override
public List<UserAddress> getUserAddressList(String userId) {
System.out.println("---UserServiceImpl..3.....");
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");
if (Math.random() > 0.5) {
throw new RuntimeException();
}
return Arrays.asList(address1, address2);
}
}
配置文件
server:
port: 8091
dubbo:
application:
# 当前服务注册名称
name: boot-server-user-provider
registry:
# 注册地址
address: 127.0.0.1:2181
# 注册中心协议类型
protocol: zookeeper
protocol:
# 通信协议
name: dubbo
# 通信端口
port: 20081
monitor:
# 连接监控中心方式
protocol: registry
主启动类添加@EnableDubbo激活dubbo注解
package com.yl.boot.server.user.provider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class BootServerUserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootServerUserProviderApplication.class,args);
}
}
启动服务,将提供者以boot-server-user-provider名称注册到zookeeper,可以通过dubbo-admin控制台查看
创建boot-server-order-consumer项目
导入依赖,因为consumer用到web功能所以导入spring-boot-starter-web依赖,其他与boot-server-user-provider一致
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
订单业务实现类
package com.yl.boot.server.order.consumer.service.impl;
import java.util.Arrays;
import java.util.List;
import com.yl.entity.UserAddress;
import com.yl.service.OrderService;
import com.yl.service.UserService;
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
/**
* 订单
*
* @author Y-wee
*/
@Service
public class OrderServiceImpl implements OrderService {
/**
* 声明需要调用的远程服务接口
*/
@Reference
private UserService userService;
/**
* 调用远程用户服务获取用户地址
*
* @param userId
* @return
*/
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户id:" + userId);
// 查询用户的收货地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
return addressList;
}
}
订单contoller接口
package com.yl.boot.server.order.consumer.controller;
import java.util.List;
import com.yl.entity.UserAddress;
import com.yl.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* 订单
*
* @author Y-wee
*/
@RestController
public class OrderController {
@Resource
private OrderService orderService;
/**
* 调用用户服务
*
* @param userId
* @return
*/
@GetMapping("/initOrder")
public List<UserAddress> initOrder(@RequestParam("uid") String userId) {
return orderService.initOrder(userId);
}
}
配置文件
server:
port: 8092
dubbo:
application:
name: boot-server-order-consumer
registry:
address: zookeeper://127.0.0.1:2181
monitor:
protocol: registry
主启动类添加@EnableDubbo激活dubbo注解
package com.yl.boot.server.order.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 BootServerOrderConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootServerOrderConsumerApplication.class,args);
}
}
启动服务,访问:http://localhost:8092/initOrder?uid=1
实现通过dubbo远程调用用户服务
记得快乐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!