一、安装并启动zookeeper
参考资料:https://blog.csdn.net/tlk20071/article/details/52028945
二、创建springboot-dubbo项目
项目结构
项目名称为springboot-dubbo,包含springboot-dubbo-consumer、springboot-dubbo-provider两个模块。
第一步:新建springboot-dubbo项目
File -> New -> Project -> Spring Initializr
如下图:输入Group、Artifact等信息,Type选择Maven Pom
然后一直点击Next,最后Finish即可。
最后项目生成后结构只有一个pom.xml文件,无src目录,如下图
第二步:新建springboot-dubbo-provider模块
完整pom.xml如下:
<?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>com.lnjecit</groupId> <artifactId>springboot-dubbo-provider</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <dubbo-spring-boot-starter.version>0.2.0</dubbo-spring-boot-starter.version> </properties> <dependencies> <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.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties配置如下:
server.port=8080
# Dubbo Config properties
dubbo.application.name=springboot-dubbo-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.registry.address=zookeeper://localhost:2181
dubbo.scan.basePackages=com.lnjecit.service.impl
定义接口:UserService
package com.lnjecit.service; import java.util.List; /** * @author lnj * createTime 2018-11-05 20:55 **/ public interface UserService { List<String> query(); }
定义接口实现类:UserServiceImpl
package com.lnjecit.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.lnjecit.service.UserService; import java.util.ArrayList; import java.util.List; /** * @author lnj * createTime 2018-11-05 20:56 **/ @Service public class UserServiceImpl implements UserService { @Override public List<String> query() { List<String> userList = new ArrayList<>(); userList.add("李白"); userList.add("杜甫"); userList.add("白居易"); return userList; } }
启动类SpringbootDubboProviderApplication :
package com.lnjecit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootDubboProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringbootDubboProviderApplication.class, args); } }
运行UserApplication类,启动服务。
第三步:新建springboot-dubbo-consumer模块
完整pom.xml如下:
<?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>com.lnjecit</groupId> <artifactId>springboot-dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-dubbo-consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.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> <dubbo-spring-boot-starter.version>0.2.0</dubbo-spring-boot-starter.version> </properties> <dependencies> <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.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>com.lnjecit</groupId> <artifactId>springboot-dubbo-provider</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
定义UserController类:
package com.lnjecit.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.lnjecit.service.UserService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author lnj * createTime 2018-11-05 21:09 **/ @RequestMapping("user") @RestController public class UserController { @Reference private UserService userService; @GetMapping("list") public List<String> list() { return userService.query(); } }
启动类SpringbootDubboConsumerApplication :
package com.lnjecit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringbootDubboConsumerApplication.class, args); } }
运行SpringbootDubboProviderApplication类,启动服务。
测试
提供者和消费者服务启动后,在浏览器中输入:http://localhost:8081/user/list,可看到如下信息
项目地址
https://github.com/linj6/springboot-learn/tree/master/springboot-dubbo
参考资料
https://juejin.im/post/5b21d6696fb9a01e4e5e6e63