当前位置:   article > 正文

dubbo+zookeeper+springboot_dubbo zookeeper springboot

dubbo zookeeper springboot

zookeeper安装

官网下载稳定版

Apache ZooKeeper

 下载到Linux的目录/usr/local/zookeeper下

wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz

解压

  1. //解压
  2. tar -zxvf apache-zookeeper-3.7.1-bin.tar.gz
  3. //解压后删除掉,没用了
  4. rm apache-zookeeper-3.7.1-bin.tar.gz

创建个文件夹/usr/local/zookeeper/data存它的数据

mkdir data

修改配置文件中的data地址

vim apache-zookeeper-3.7.1-bin/conf/zoo_sample.cfg 

改成这样

复制一份配置文件命名为zoo.cfg(默认启动zoo.cfg)

  1. //进入配置文件目录
  2. cd apache-zookeeper-3.7.1-bin/conf/
  3. //复制一份
  4. cp zoo_sample.cfg ./zoo.cfg

进入bin目录启动zookeeper

./zkServer.sh start

dubbo

 

dubbo+springboot+zookeeper

先了解下这个注解

第一步:

首先说明下,这个父-子工程结构,我的目的只是说我的提供方和消费者能够进行本地模块间的调用interface模块的接口,现实开发我不知道是怎么样子的(我是无业大4游民,实习生都还没人要,快哭了)

创建好我们的一个目录结构,消费者和提供方一定要是springboot工程,实际开发就是在不同电脑上,要联网进行RPC(远程调用)

parent

pom:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>parent</artifactId>
  8. <packaging>pom</packaging>
  9. <version>1.0-SNAPSHOT</version>
  10. <modules>
  11. <module>interface</module>
  12. <module>provider</module>
  13. </modules>
  14. <properties>
  15. <maven.compiler.source>8</maven.compiler.source>
  16. <maven.compiler.target>8</maven.compiler.target>
  17. </properties>
  18. </project>

interface

userService.java

  1. package service;
  2. public interface userService {
  3. public String hello();
  4. }

他的作用就是提供个接口

provider

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <artifactId>parent</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <artifactId>provider</artifactId>
  11. <properties>
  12. <java.version>1.8</java.version>
  13. </properties>
  14. <dependencies>
  15. <!-- interface模块的坐标,这样我们就能调用interface模块的一些方法-->
  16. <dependency>
  17. <artifactId>interface</artifactId>
  18. <groupId>org.example</groupId>
  19. <version>1.0-SNAPSHOT</version>
  20. </dependency>
  21. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. <version>2.7.3</version>
  26. </dependency>
  27. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. <version>2.7.3</version>
  32. </dependency>
  33. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <version>2.7.3</version>
  38. <scope>test</scope>
  39. </dependency>
  40. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  41. <dependency>
  42. <groupId>org.projectlombok</groupId>
  43. <artifactId>lombok</artifactId>
  44. <version>1.18.24</version>
  45. <scope>provided</scope>
  46. </dependency>
  47. <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
  48. <dependency>
  49. <groupId>org.apache.dubbo</groupId>
  50. <artifactId>dubbo-spring-boot-starter</artifactId>
  51. <version>3.1.0</version>
  52. </dependency>
  53. <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
  54. <dependency>
  55. <groupId>org.apache.dubbo</groupId>
  56. <artifactId>dubbo-dependencies-zookeeper</artifactId>
  57. <version>3.1.0</version>
  58. <type>pom</type>
  59. </dependency>
  60. </dependencies>
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. </plugins>
  68. </build>
  69. </project>

首先要看懂这个pom,第一就是<parent>,新建spring boot都是继承了springboot父类的,这里要改成我们自己的parent父类,然后就是因为没有继承那个springboot父类,所以他的一些依赖都要有自己的版本号,我都是从maven里拿的最最最新的,关键最后两个依赖dubbo和zookeeper

 userServiceImpl.java

  1. package com.example;
  2. import org.apache.dubbo.config.annotation.DubboService;
  3. import service.userService;
  4. @DubboService
  5. public class userServiceImpl implements userService {
  6. @Override
  7. public String hello() {
  8. return "提供方";
  9. }
  10. }

 发现就是提供了一个接口的实现方法而已,@DubboService,要用这个注解哦

.yml

  1. server:
  2. port: 9001
  3. dubbo:
  4. application:
  5. name: provider
  6. protocol:
  7. name: dubbo
  8. port: -1
  9. registry:
  10. id: zk-registry
  11. address: zookeeper://192.168.126.132:2181
  12. config-center:
  13. address: zookeeper://192.168.126.132:2181
  14. metadata-report:
  15. address: zookeeper://192.168.126.132:2181

都是啥意思,我是初级没工作的人,我也不知道,从官网copy的,我知道registry是绑定注册中心,哈哈,够用了这个,我绑定的是linux上开启的一个zookeeper,怎么开启,最前面有启动zookeeper的讲解

启动类

@EnableDubbo注解,开启Dubbo ,然后使用slf4j会有点报错,好像是zookeeper里面有个slf4j,然后我们的Lombok也有slf4j,有那么点冲突,可以在zookeeper依赖里剔除我们的slf4j依赖,当然我懒得研究,没多大影响哈哈

cosumer

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <artifactId>parent</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <artifactId>cosumer</artifactId>
  11. <properties>
  12. <java.version>1.8</java.version>
  13. </properties>
  14. <dependencies>
  15. <!-- interface模块的坐标,这样我们就能调用interface模块的一些方法-->
  16. <dependency>
  17. <artifactId>interface</artifactId>
  18. <groupId>org.example</groupId>
  19. <version>1.0-SNAPSHOT</version>
  20. </dependency>
  21. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. <version>2.7.3</version>
  26. </dependency>
  27. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. <version>2.7.3</version>
  32. </dependency>
  33. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <version>2.7.3</version>
  38. <scope>test</scope>
  39. </dependency>
  40. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  41. <dependency>
  42. <groupId>org.projectlombok</groupId>
  43. <artifactId>lombok</artifactId>
  44. <version>1.18.24</version>
  45. <scope>provided</scope>
  46. </dependency>
  47. <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
  48. <dependency>
  49. <groupId>org.apache.dubbo</groupId>
  50. <artifactId>dubbo-spring-boot-starter</artifactId>
  51. <version>3.1.0</version>
  52. </dependency>
  53. <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
  54. <dependency>
  55. <groupId>org.apache.dubbo</groupId>
  56. <artifactId>dubbo-dependencies-zookeeper</artifactId>
  57. <version>3.1.0</version>
  58. <type>pom</type>
  59. </dependency>
  60. </dependencies>
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. </plugins>
  68. </build>
  69. </project>

首先要看懂这个pom,第一就是<parent>,新建spring boot都是继承了springboot父类的,这里要改成我们自己的parent父类,然后就是因为没有继承那个springboot父类,所以他的一些依赖都要有自己的版本号,我都是从maven里拿的最最最新的,关键最后两个依赖dubbo和zookeeper

controller.java

  1. package com.example.controller;
  2. import org.apache.dubbo.config.annotation.DubboReference;
  3. import org.springframework.web.bind.annotation.PostMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import service.userService;
  6. @RestController
  7. public class userController {
  8. @DubboReference
  9. private userService userService;
  10. @PostMapping("/login")
  11. public String login() {
  12. return "我拿到了"+userService.hello();
  13. }
  14. }

 @DubboReference注解,就是自动装配的意思,不能用@Autowire,它是去Spingboot容器里面找实现类,我们的实现类了在提供方里,它找不到的

yml

  1. server:
  2. port: 9000
  3. dubbo:
  4. application:
  5. name: cosumer
  6. protocol:
  7. name: dubbo
  8. port: -1
  9. registry:
  10. id: zk-registry
  11. address: zookeeper://192.168.126.132:2181
  12. config-center:
  13. address: zookeeper://192.168.126.132:2181
  14. metadata-report:
  15. address: zookeeper://192.168.126.132:2181

都是啥意思,我是初级没工作的人,我也不知道,从官网copy的,我知道registry是绑定注册中心,哈哈,够用了这个,,我绑定的是linux上开启的一个zookeeper,怎么开启,最前面有启动zookeeper的讲解

启动类

 @EnableDubbo注解,开启Dubbo ,然后使用slf4j会有点报错,好像是zookeeper里面有个slf4j,然后我们的Lombok也有slf4j,有那么点冲突,可以在zookeeper依赖里剔除我们的slf4j依赖,当然我懒得研究,没多大影响哈哈

结果

首先Linux上启动zookeeper--启动provider项目---启动cosumer项目-----测试RPC(远程调用)结果

 

 

 

 怎么样,是不是简单易懂,当然,这只是最基础的东西,工作了可能才知道怎么的使用,到时候可能就难咯。不过,我想说的是,要把基础搞懂了,你懂我意思吗。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/284219
推荐阅读
相关标签
  

闽ICP备14008679号