一:安装zookeeper
1.官网 https://zookeeper.apache.org/
2.下载 xxx-bin.tar.gz,创建目录并解压
# 1. 解压
tar -zxvf xxx-bin.tar.gz
# 2. 进入conf 目录
cp zoo_sample.cfg zoo.cfg
# 编辑 zoo.cfg (vim zoo.cfg) 修改 dataDir路径
3.启动/停止
# 进入bin目录 启动
./zkServer.sh start
# 停止
./zkServer.sh stop
# 查看状态
./zkServer.sh status
启动成功
二 springBoot 集成 dubbo
1.创建父工程,引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo服务 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
- 创建dubbo-interface工程
因为dubbo 基于接口注入远程资源,所以接口工程单独创建,dubbo-provider工程、dubbo-consumer 工程需依赖 dubbo-interface工程
编写 dubboService接口
public interface DubboService {
String sayHello();
}
- 创建 dubbo-provicer工程 启动类添加注解@EnableDubbo 将资源注册到dubbo。 并实现 dubboService接口
配置文件
spring:
application:
name: dubbo-provicer
main:
allow-bean-definition-overriding: true
server:
port: 8080
servlet:
context-path: /
dubbo:
application:
name: dubbo-provicer #dubbo的注册的服务名, 唯一性
registry:
address: zookeeper://ip:2181 #注册zookeeper的地址
import com.alibaba.dubbo.config.annotation.Service;
import com.study.UserService;
@Service//注意引入 dubbo 包下注解
public class DubboServiceImpl implements DubboService {
public String sayHello() {
return "hello dubbo hello!~";
}
}
- 创建dubbo-consumer工程,若消费者工程也需要提供资源则启动类添加注解@EnableDubbo(一般不需要) 编写测试类
配置文件
spring:
application:
name: dubbo-consumer
server:
port: 8081
servlet:
context-path: /
dubbo:
application:
name: dubbo-consumer
registry:
address: zookeeper://ip:2181
测试类
import com.alibaba.dubbo.config.annotation.Reference;
import com.study.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/dubbo")
public class UserController {
//注入Service
@Reference // dubbo包下注解
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
String s = userService.sayHello();
return s;
}
}