zookeeper安装
官网下载稳定版
下载到Linux的目录/usr/local/zookeeper下
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz
解压
//解压
tar -zxvf apache-zookeeper-3.7.1-bin.tar.gz
//解压后删除掉,没用了
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)
//进入配置文件目录
cd apache-zookeeper-3.7.1-bin/conf/
//复制一份
cp zoo_sample.cfg ./zoo.cfg
进入bin目录启动zookeeper
./zkServer.sh start
dubbo
dubbo+springboot+zookeeper
先了解下这个注解
第一步:
首先说明下,这个父-子工程结构,我的目的只是说我的提供方和消费者能够进行本地模块间的调用interface模块的接口,现实开发我不知道是怎么样子的(我是无业大4游民,实习生都还没人要,快哭了)
创建好我们的一个目录结构,消费者和提供方一定要是springboot工程,实际开发就是在不同电脑上,要联网进行RPC(远程调用)
parent
pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>interface</module>
<module>provider</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
interface
userService.java
package service;
public interface userService {
public String hello();
}
他的作用就是提供个接口
provider
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>provider</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- interface模块的坐标,这样我们就能调用interface模块的一些方法-->
<dependency>
<artifactId>interface</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>3.1.0</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
首先要看懂这个pom,第一就是<parent>,新建spring boot都是继承了springboot父类的,这里要改成我们自己的parent父类,然后就是因为没有继承那个springboot父类,所以他的一些依赖都要有自己的版本号,我都是从maven里拿的最最最新的,关键最后两个依赖dubbo和zookeeper
userServiceImpl.java
package com.example;
import org.apache.dubbo.config.annotation.DubboService;
import service.userService;
@DubboService
public class userServiceImpl implements userService {
@Override
public String hello() {
return "提供方";
}
}
发现就是提供了一个接口的实现方法而已,@DubboService,要用这个注解哦
.yml
server:
port: 9001
dubbo:
application:
name: provider
protocol:
name: dubbo
port: -1
registry:
id: zk-registry
address: zookeeper://192.168.126.132:2181
config-center:
address: zookeeper://192.168.126.132:2181
metadata-report:
address: zookeeper://192.168.126.132:2181
都是啥意思,我是初级没工作的人,我也不知道,从官网copy的,我知道registry是绑定注册中心,哈哈,够用了这个,我绑定的是linux上开启的一个zookeeper,怎么开启,最前面有启动zookeeper的讲解
启动类
@EnableDubbo注解,开启Dubbo ,然后使用slf4j会有点报错,好像是zookeeper里面有个slf4j,然后我们的Lombok也有slf4j,有那么点冲突,可以在zookeeper依赖里剔除我们的slf4j依赖,当然我懒得研究,没多大影响哈哈
cosumer
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cosumer</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- interface模块的坐标,这样我们就能调用interface模块的一些方法-->
<dependency>
<artifactId>interface</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>3.1.0</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
首先要看懂这个pom,第一就是<parent>,新建spring boot都是继承了springboot父类的,这里要改成我们自己的parent父类,然后就是因为没有继承那个springboot父类,所以他的一些依赖都要有自己的版本号,我都是从maven里拿的最最最新的,关键最后两个依赖dubbo和zookeeper
controller.java
package com.example.controller;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import service.userService;
@RestController
public class userController {
@DubboReference
private userService userService;
@PostMapping("/login")
public String login() {
return "我拿到了"+userService.hello();
}
}
@DubboReference注解,就是自动装配的意思,不能用@Autowire,它是去Spingboot容器里面找实现类,我们的实现类了在提供方里,它找不到的
yml
server:
port: 9000
dubbo:
application:
name: cosumer
protocol:
name: dubbo
port: -1
registry:
id: zk-registry
address: zookeeper://192.168.126.132:2181
config-center:
address: zookeeper://192.168.126.132:2181
metadata-report:
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(远程调用)结果
怎么样,是不是简单易懂,当然,这只是最基础的东西,工作了可能才知道怎么的使用,到时候可能就难咯。不过,我想说的是,要把基础搞懂了,你懂我意思吗。