1.zookeeper环境
1.1下载zookeeper,修改conf目录下的zoo_sample.cfg文件名为zoo.cfg
1.2启动即可
2.springboot整合Dubbo
2.1服务提供端
2.1.1pom文件
<?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>dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.1.2application.yml文件
server:
port: 8080
dubbo:
application:
name: boot-dubbo-service-provider
registry:
address: 127.0.0.1:2181
protocol: zookeeper
protocol:
name: dubbo
port: 20880
2.1.3主类
加@EnableDubbo
2.1.4service实现
引入的是alibaba的@Service注解
import com.alibaba.dubbo.config.annotation.Service;
import org.example.interf.DemoService;
import org.springframework.stereotype.Component;
@Component
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String test() {
return "666";
}
}
2.1.5启动
2.2服务消费端
2.2.1pom文件
与2.1.1的pom文件内容基本相同
2.2.2application.yml文件
与2.1.2的application.yml文件基本相同,修改端口8081
2.2.3主类
加@EnableDubbo
2.2.4service实现
import com.alibaba.dubbo.config.annotation.Reference;
import org.example.interf.DemoService;
import org.example.service.ConsumerService;
import org.springframework.stereotype.Service;
@Service
public class ConsumerServiceImpl implements ConsumerService {
@Reference//引用远程提供者服务
DemoService demoService;
@Override
public String consumer() {
return demoService.test();
}
}
2.2.5启动测试
2.3注意的问题
2.3.1版本
springboot版本与dubbo版本
2.3.2两个服务的接口的包名类名一致
一般把接口与实体类单独作为一个模块引入