一、名词解释(来源百度百科):
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
二、项目搭建步骤:
下载完成后解压,然后打开配置拷贝一份zoo_sample.cfg,修改名称为zoo.cfg,打开zoo_.cfg,修改数据保存路径,如下图修改为本地可用路径,没有就创建对应的文件夹目录:
开启zookeeper服务,如下图:
?至此,Zookeeper启动成功。
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) 测试结果如下:
|