Dubbo的使用

本文详细介绍了Dubbo的基本原理流程,包括如何通过XML配置和注解方式实现服务提供者与消费者的注册、服务暴露和引用。涵盖了版本选择、依赖引入、应用与注册中心配置,以及实际代码示例。

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

Dubbo
原理流程
图片:

1、引入dubbo

// An highlighted block
<!-- 引入dubbo -->
                <dependency>
                        <groupId>com.alibaba</groupId>
                        <artifactId>dubbo</artifactId>
                        <version>2.6.2</version>
                </dependency>
        <!-- 由于我们使用zookeeper作为注册中心,所以需要操作zookeeper
dubbo 2.6以前的版本引入zkclient操作zookeeper 
dubbo 2.6及以后的版本引入curator操作zookeeper
下面两个zk客户端根据dubbo版本21即可
-->
                <dependency>
                        <groupId>com.101tec</groupId>
                        <artifactId>zkclient</artifactId>
                        <version>0.10</version>
                </dependency>
                <!-- curator-framework -->
                <dependency>
                        <groupId>org.apache.curator</groupId>
                        <artifactId>curator-framework</artifactId>
                        <version>2.12.0</version>
                </dependency>

方式一:xml
供应者:
2、配置提供者

// An highlighted block
<!--当前应用的名字  -->
        <dubbo:application name="gmall-user"></dubbo:application>
        <!--指定注册中心的地址  -->
    <dubbo:registry address="zookeeper://118.24.44.169:2181" />
    <!--使用dubbo协议,将服务暴露在20880端口  -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 指定需要暴露的服务 -->
    <dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl" />

3、启动服务

// An highlighted block
       public static void main(String[] args) throws IOException {
                ClassPathXmlApplicationContext context = 
                                new ClassPathXmlApplicationContext("classpath:spring-beans.xml");         
                System.in.read(); 
        }

消费者:

// An highlighted block
<!-- 应用名 -->
        <dubbo:application name="gmall-order-web"></dubbo:application>
        <!-- 指定注册中心地址 -->
        <dubbo:registry address="zookeeper://118.24.44.169:2181" />
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="userService" interface="com.atguigu.gmall.service.UserService"></dubbo:reference>

方式二:注解
提供方:

// An highlighted block
 <dubbo:application name="gmall-user"></dubbo:application>
    <dubbo:registry address="zookeeper://118.24.44.169:2181" />
    <dubbo:protocol name="dubbo" port="20880" />
<dubbo:annotation package="com.atguigu.gmall.user.impl"/>

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.UserService;
import com.atguigu.gmall.user.mapper.UserAddressMapper;

@Service //使用dubbo提供的service注解,注册暴露服务
public class UserServiceImpl implements UserService {

        @Autowired
        UserAddressMapper userAddressMapper;

消费方:

// An highlighted block
<dubbo:application name="gmall-order-web"></dubbo:application>
<dubbo:registry address="zookeeper://118.24.44.169:2181" />
<dubbo:annotation package="com.atguigu.gmall.order.controller"/>


@Controller
public class OrderController {
        
        @Reference  //使用dubbo提供的reference注解引用远程服务
        UserService userService;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值