基于SpringBoot的Dubbo启动

dubbo框架

image.png

1 模块添加

  1. 新建三个模块,分别表示消息的提供者,消息的消费者和公共模块
image-20210923104204300.png
  • dubbo-parent的pom

说明:加入springboot的父依赖,作为版本控制

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
</parent>
  • service-provider的pom
<dependencies>
   <dependency> <!-- 公共模块的依赖 -->
     <groupId>com.yqj</groupId>
     <artifactId>common</artifactId>
     <version>1.0-SNAPSHOT</version>
   </dependency>
   <dependency> <!-- dubbo的依赖 -->
     <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</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
   </dependency>
</dependencies>
  • service-consumer的pom
<dependencies>
  <dependency> <!-- 公共模块的依赖 -->
    <groupId>com.yqj</groupId> 
    <artifactId>common</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
  <dependency> <!-- dubbo的依赖 -->
    <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</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

2 消息提供者模块

  1. 主启动类
@EnableDubbo //扫描dubbo的组件
@SpringBootApplication
public class UserServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceProviderApplication.class,args);
    }
}
  1. 服务接口(定义在common公共模块里面)目的是可以让其他模块得到依赖
public interface UserService {
    public String getUserAddress(String userId);
}
  1. 服务实现
@Service //dubbo的service注解,说明该类是一个资源
@Component
public class UserServiceImpl implements UserService {

    @Override
    public String getUserAddress(String userId) {
        String url = "dubbo localhost " + userId;
        return url;
    }
}
  1. 配置文件
server:
  port: 9001

dubbo:
  application:
    name: user-service-provider
  registry:
    protocol: zookeeper
    address: localhost:2181
  protocol:
    name: dubbo
    port: 20880
  monitor:
    protocol: registry

3 消息消费者

  1. 主启动类
@EnableDubbo
@SpringBootApplication
public class ServiceConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class,args);
    }
}
  1. 服务接口
public interface OrderService {
    public String getRemoteUserAddress(String userId);
}
  1. 服务实现
@Service
public class OrderServiceImpl implements OrderService {

    @Reference //远程注入
    private UserService userService; //从公共模块中导入 UserService

    @Override
    public String getRemoteUserAddress(String userId) {
        return userService.getUserAddress(userId);
    }
}
  1. controller调用服务的方法
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{userId}")
    public String getRemoteUserAddress(@PathVariable("userId") String userId) {
        return orderService.getRemoteUserAddress(userId);
    }
}
  1. 配置文件
server:
  port: 8001
dubbo:
  application:
    name: order-service-consumer
  registry:
    address: zookeeper://localhost:2181
  monitor:
    protocol: registry

4 测试

  1. 启动zookeeper
  2. 访问网页测试 http://localhost:8001/order/12
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攻城老湿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值