1、搭建 zookeeper 之前要确保当前的机器已经有 JDK 环境。
2、将 conf 目录下 zoo_sample.cfg 文件重命名为 zoo.cfg
主要分为下面几步:
项目结构:
dubbo-interface 后面被打成 jar 包,它的作用只是提供接口。
File->New->Module... ,然后选择 Maven类型的项目,其他的按照提示一步一步走就好。
package com.fengx.service; /** * @author: Fengx * @date: 2021-07-21 * @description: 服务接口 **/ public interface HelloService { String sayHello(String name); }
点击右边的 Maven Projects 然后选择 install ,这样 jar 包就打好了。
主要分为下面几步:
项目结构:
创建一个 SpringBoot 项目,注意勾选上 web 模块。
需要引入 dubbo 、zookeeper以及接口的相关依赖 jar 包。注意将本项目和 dubbo-interface 项目的 dependency 依赖的 groupId 和 artifactId 改成自己的。dubbo 整合spring boot 的 jar 包在这里找dubbo-spring-boot-starter。zookeeper 的 jar包在 Maven 仓库 搜索 zkclient 即可找到。
<?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> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.fengx</groupId> <artifactId>dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo-provider</name> <description>Demo project for Spring Boot</description> <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>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.fengx</groupId> <artifactId>dubbo-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--引入dubbo的依赖--> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- 引入zookeeper的依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置很简单,这主要得益于 springboot 整合 dubbo 专属的@EnableDubboConfiguration
注解提供的 Dubbo 自动配置。
# 配置端口 server.port=8333 spring.dubbo.application.name=dubbo-provider spring.dubbo.application.registry=zookeeper://127.0.0.1:2181
注意: @Service
注解使用的时 Dubbo 提供的而不是 Spring 提供的。另外,加了Dubbo 提供的 @Service
注解之后还需要加入
package com.fengx.dubboprovider.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.fengx.service.HelloService; import org.springframework.stereotype.Component; /** * @author: Fengx * @date: 2021-07-21 * @description: 服务接口实现 **/ @Component @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello " + name; } }
注意:不要忘记加上 @EnableDubboConfiguration
注解开启Dubbo 的自动配置。
package com.fengx.dubboprovider; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 服务提供者 */ @SpringBootApplication // 开启dubbo的自动配置 @EnableDubboConfiguration public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); } }
主要分为下面几步:
项目结构:
创建一个 SpringBoot 项目,注意勾选上 web 模块。
<?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> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.fengx</groupId> <artifactId>dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo-consumer</name> <description>Demo project for Spring Boot</description> <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>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--引入接口项目jar包--> <dependency> <groupId>com.fengx</groupId> <artifactId>dubbo-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--引入dubbo的依赖--> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- 引入zookeeper的依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置很简单,这主要得益于 springboot 整合 dubbo 专属的@EnableDubboConfiguration
注解提供的 Dubbo 自动配置。
# 配置端口 server.port=8330 spring.dubbo.application.name=dubbo-consumer spring.dubbo.application.registry=zookeeper://127.0.0.1:2181
package com.fengx.dubboconsumer.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.fengx.service.HelloService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author: Fengx * @date: 2021-07-21 * @description: 调用远程服务 **/ @RestController public class HelloController { @Reference private HelloService helloService; @RequestMapping("/hello") public String hello() { String hello = helloService.sayHello("world"); System.out.println(helloService.sayHello("fengx")); return hello; } }
package com.fengx.dubboconsumer; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboConsumerApplication.class, args); } }
浏览器访问 http://localhost:8330/hello 页面返回 Hello world,控制台输出 Hello fengx,和预期一致,使用SpringBoot+Dubbo 搭建第一个简单的分布式服务实验成功!
参考:https://github.com/CodingDocs/springboot-guide/blob/master/docs/advanced/springboot-dubbo.md