项目搭建步骤:
1、安装本地Zookeeper注册中心,下载地址:Apache Downloads 官网下载贼慢
这也有
zookeeper-3.8-Java文档类资源-CSDN下载
下载完成后解压,然后打开配置拷贝一份zoo_sample.cfg,修改名称为zoo.cfg,打开zoo_.cfg,修改数据保存路径,如下图修改为本地可用路径,没有就创建对应的文件夹目录:
开启zookeeper服务,如下图:
完整项目源码
springboot+duboo+zookeeper实战-Java文档类资源-CSDN下载
2、打开IDEA开发工具,创建一个基于maven的SpringBoot的父项目,然后基于父项目创建三个子模块module,最终项目结构如下图:
1、打开父项目pom.xml文件,添加刚才创建的三个子模块,如下图完整代码:
注意:<packaging>pom</packaging> 需要手动添加。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>springboot-dubbo-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-api</module>
<module>dubbo-provider</module>
<module>dubbo-customer</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
2、dubbo-api模块
新增服务接口类HelloService,代码如下:
public interface HelloService {
String sayHello(String name);
}
dubbo-api中pom.xml代码如下:
<parent>
<artifactId>springboot-dubbo-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-api</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
3、dubbo-provider模块
新增实现接口类HelloServiceImpl,代码如下:
import com.alibaba.dubbo.config.annotation.Service;
import com.api.service.HelloService;
@Service //发布服务
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
dubbo-provider中DubboProviderApplication启动类添加dubbo注解,代码如下:
@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
dubbo-provider中pom.xml代码如下:
<parent>
<artifactId>springboot-dubbo-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<zookeeper.version>3.4.13</zookeeper.version>
<dubbo.version>0.2.0</dubbo.version>
</properties>
<dependencies>
<dependency>
<artifactId>dubbo-api</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
dubbo-provider中application.yml配置内容如下:
server:
port: 8000
dubbo:
application:
name: dubbo-provider
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://127.0.0.1:2181
注意:
@Service 是dubbo发布服务的注解;
@EnableDubbo 是启动dubbo服务注解;
log4j 和 slf4j-log4j12 需要排除,否则会与zookeeper冲突;
4、dubbo-customer模块
新增控制类HelloController,便于测试,代码如下:
@RestController
public class HelloController {
@Reference //引用服务
private HelloService helloService;
@GetMapping("/sayHello")
private String sayHello(@RequestParam String name){
System.out.println("调用sayHello成功了..." + " name:" + name);
return helloService.sayHello(name);
}
}
dubbo-customer中pom.xml代码如下:
<parent>
<artifactId>springboot-dubbo-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-customer</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<zookeeper.version>3.4.13</zookeeper.version>
<dubbo.version>0.2.0</dubbo.version>
</properties>
<dependencies>
<dependency>
<artifactId>dubbo-api</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
dubbo-customer中application.yml配置内容如下:
server:
port: 9000
dubbo:
application:
name: dubbo-customer
registry:
address: zookeeper://127.0.0.1:2181
注意:@Reference 是dubbo引用服务的注解;
至此,基于dubbo的分布式项目框架就搭建完成了
三、项目消费-服务功能测试:
1)首先运行启动服务提供者dubbo-provider模块
2)然后启动服务消费者dubbo-customer模块
3) 测试结果如下: