当前位置: 首页 > news >正文

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{@Overridepublic 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 {@Referenceprivate HelloService helloService;@RequestMapping("/hello")@ResponseBodypublic 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的端口号要区分开

结果展示:

http://www.lryc.cn/news/284976.html

相关文章:

  • sm2和aes加解密
  • cv2.findContours报错解决
  • RHEL - 更新升级软件或系统
  • JNPF低代码开发平台总体架构介绍
  • axios的传参方式
  • 受电端协议芯片是如何让Type-C接口设备实现快充?
  • 浪花 - 搜索标签前后端联调
  • GPU与SSD间的P2P DMA访问机制
  • 未来的NAS:连接您的数字生活
  • C++ 设计模式之备忘录模式
  • 【项目搭建三】SpringBoot引入redis
  • 漫谈广告机制设计 | 听闻RTA要搞二次竞价了?牛啊!
  • 第04章_IDEA的安装与使用(下)(IDEA断点调试,IDEA常用插件)
  • HBase鉴权设计以及Kerberos鉴权方法
  • 【华为GAUSS数据库】IDEA连接GAUSS数据库方法
  • [java基础揉碎]键盘输入语句
  • Redis 面试题 | 01.精选Redis高频面试题
  • Crow:实现点击下载功能
  • 2024年华为OD机试真题-内存冷热标记-Python-OD统一考试(C卷)
  • Webpack5入门到原理9:处理字体图标资源
  • 【Docker】在Windows操作系统安装Docker前配置环境
  • Webpack5入门到原理21:提升开发体验
  • YOLOv8改进 | Conv篇 | 在线重参数化卷积OREPA助力二次创新(提高推理速度 + FPS)
  • conda国内加速
  • RabbitMQ-数据持久化
  • JS-WebAPIs-本地存储(五)
  • 了解Vue中日历插件Fullcalendar
  • Cloudreve存储策略-通过从机存储来拓展容量
  • java进阶-jvm精讲及实战
  • vue中引入sass、scss