dubbo入门案例!!!

入门案例之前我们先介绍一下:zookeeper。

Zookeeper是Apacahe Hadoop的子项目,可以为分布式应用程序协调服务,适合作为Dubbo服务的注册中心,负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互。

就不用安装了,我会上传一个安装包。

总结:

        1、什么是zookeeper?
                zookeeper:负责管理ip和port,是服务提供者和服务消费者的注册中心
        2、zookeeper的安装和启动
                安装:
                   解压即安装
                启动:
                    双击bin/zkServer.cmd

开始入门案例:(项目结构)

父工程的pom.xml

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- 由于使⽤了zookeeper作为注册中⼼,则需要加⼊zookeeper的客户端jar包: -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>

1、dobbo_interface模块

这个模块中我们就只写一个接口模拟一下就可以.

在com.by.service中写一个HelloService接口

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.service;

/**
 * <p>Project: dubbo_parent - HelloService</p>
 * <p>Powered by scl On 2024-01-17 13:56:01</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
public interface HelloService {
    String hello();
}

2、dobbo_provider模块

在这个模块中我们需要做:实现上个模块的接口,创建spring boot的启动类,创建配置类

pom.xml:

<dependencies>
        <dependency>
            <groupId>com.by</groupId>
            <artifactId>dubbo_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

HelloServiceImpl:(注意这个@Service注解是dubbo下的)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.service;

import com.alibaba.dubbo.config.annotation.Service;

/**
 * <p>Project: dubbo_parent - HelloServiceImpl</p>
 * <p>Powered by scl On 2024-01-17 13:57:42</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@Service
public class HelloServiceImpl implements HelloService{
    @Override
    public String hello() {
        return "你好啊!!!";
    }
}

启动类:DubboProviderApp:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <p>Project: dubbo_parent - DubboProviderApp</p>
 * <p>Powered by scl On 2024-01-17 13:59:35</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@SpringBootApplication
@EnableDubbo //让dubbo去扫描dubbo的注解
public class DubboProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApp.class,args);
    }
}

application.properties:

#zookeeper\u7684\u5730\u5740
dubbo.registry.address=zookeeper://127.0.0.1:2181
#\u901A\u8BAF\u534F\u8BAE\uFF1Armi\u3001http\u3001dubbo
dubbo.protocol.name=dubbo
#\u5F53\u524D\u670D\u52A1\u7684\u540D\u79F0
dubbo.application.name=dubbo-provider

3、dobbo_consumer模块

在这个模块中我们需要测试一下我们的功能。实现上个模块的接口,创建spring boot的启动类,创建配置类。

pom.xml:

<dependencies>
        <dependency>
            <groupId>com.by</groupId>
            <artifactId>dubbo_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

HelloController:(注意:@Reference也是dubbo下的)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.by.service.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * <p>Project: dubbo_parent - HelloController</p>
 * <p>Powered by scl On 2024-01-17 15:02:44</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@Controller
public class HelloController {

    @Reference
    private HelloService helloService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return helloService.hello();
    }
}

启动类:DubboConsumerApplication:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <p>Project: dubbo_parent - DubboConsumerApplication</p>
 * <p>Powered by scl On 2024-01-17 15:00:04</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class,args);
    }
}

配置文件:application.porperties

#zookeeper\u7684\u5730\u5740
dubbo.registry.address=zookeeper://127.0.0.1:2181
#\u901A\u8BAF\u534F\u8BAE\uFF1Armi\u3001http\u3001dubbo
dubbo.protocol.name=dubbo
#\u5F53\u524D\u670D\u52A1\u7684\u540D\u79F0
dubbo.application.name=dubbo-consumer
server.port=80

注意:
        1、zookeeper必须启动
        2、 @Reference 和 @Service必须到dubbo的包
        3、必须先启动provider再起consumer

        4、模块provider和consumer的端口号要区分开

结果展示:

相关推荐

  1. Dubbo_入门

    2024-01-18 16:18:06       30 阅读
  2. <span style='color:red;'>dubbo</span>

    dubbo

    2024-01-18 16:18:06      35 阅读
  3. <span style='color:red;'>Dubbo</span>

    Dubbo

    2024-01-18 16:18:06      39 阅读
  4. <span style='color:red;'>Dubbo</span>

    Dubbo

    2024-01-18 16:18:06      12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-18 16:18:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-18 16:18:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-18 16:18:06       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-18 16:18:06       20 阅读

热门阅读

  1. Pandas实战100例 | 案例 74: 分组后计算平均值

    2024-01-18 16:18:06       31 阅读
  2. UDP服务器和客户端的创建步骤

    2024-01-18 16:18:06       22 阅读
  3. ElasticSearch入门

    2024-01-18 16:18:06       30 阅读
  4. MongoDB聚合:$set

    2024-01-18 16:18:06       34 阅读
  5. DynamoDB和Cassandra、MongoDB的比较

    2024-01-18 16:18:06       33 阅读
  6. 面试 React 框架八股文十问十答第十一期

    2024-01-18 16:18:06       32 阅读
  7. PyTorch中的nn.LeakyReLU()、nn.Module和nn.ModuleList

    2024-01-18 16:18:06       29 阅读
  8. PyTorch中的AOTAutograd、PrimTorch和TorchInductor

    2024-01-18 16:18:06       28 阅读
  9. 虚拟化技术、Docker、K8s笔记总结

    2024-01-18 16:18:06       31 阅读
  10. uni-app根据腾讯地图接口三级联动组件

    2024-01-18 16:18:06       35 阅读
  11. 【封装一个日志库(linux)】【转载】

    2024-01-18 16:18:06       31 阅读