dubbo入门(三)— 消费者

本文档介绍了如何在Dubbo中创建并配置消费者,包括工程结构的搭建、consumer.xml配置文件详解,以及如何实现远程服务调用。通过OrderServiceImpl示例展示了消费者如何成功调用提供者的服务,并详述了启动程序后的运行状态和Dubbo管理控制台的显示情况。

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

创建消费者

工程结构
在这里插入图片描述

配置consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
       ">
    <context:component-scan base-package="com.accp.gmall.service.impl" />
    <!-- 指定当前应用/服务的名字 -->
    <dubbo:application name="order-service-consumer"></dubbo:application>
    <!-- 指定注册中心的位置 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    <!-- 声明需要调用的远程服务的接口;生成远程服务代理 -->
    <dubbo:reference interface="com.accp.gmall.service.UserService" id="userService"/>
</beans>

需要调用远程服务的接口是提供者暴露的服务:

<dubbo:service interface="com.accp.gmall.service.UserService" ref="userService"/>

远程调用实现:OrderServiceImpl

import com.accp.gmall.bean.UserAddress;
import com.accp.gmall.service.OrderService;
import com.accp.gmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserService userService;

    public void initOrder(String userId) {

        //1、查询用户的收货地址
        List<UserAddress> userAddressList = userService.getUserAddressList("1");
        System.err.println(userAddressList.toString());
    }
}

启动程序:

import com.accp.gmall.service.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class MainApplication {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("consumer.xml");
        OrderService orderService = classPathXmlApplicationContext.getBean(OrderService.class);
        orderService.initOrder("1");
        System.in.read();
    }
}

运行成功:
在这里插入图片描述

dubbo管理控制台显示:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值