使用SpringBoot+Dubbo搭建微服务笔记( 三 )

六、搭建服务消费者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.czh</groupId> <artifactId>dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo-consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.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>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> <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-project --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>com.czh.dubbo</groupId> <artifactId>dubbo-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>application.properties
#web服务端口server.port=80#消费者名称dubbo.application.name=dubbo-consumer#需要使用到提供者服务的包路径dubbo.scan.base-packages=com.czh.dubbo.consumer.controller#注册中心地址dubbo.registry.address=zookeeper://127.0.0.1:2181控制器PeopleController
package com.czh.dubbo.consumer.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import com.alibaba.dubbo.config.annotation.Reference;import com.czh.dubbo.common.entity.People;import com.czh.dubbo.common.service.PeopleService;@RestControllerpublic class PeopleController { @Reference private PeopleService peopleService; @GetMapping("/people/{name}") public People getPeople(@PathVariable("name") String name) { People people = new People(); people.setName(name); return peopleService.getPeople(people); }}至此,整个项目便搭建完毕,三个项目的项目结构如下

使用SpringBoot+Dubbo搭建微服务笔记

文章插图
 
七、上线运行1、打包运行
安装API项目到本地maven仓库
在dubbo-common项目的pom.xml文件上点击右键,Run as - Maven install
使用SpringBoot+Dubbo搭建微服务笔记

文章插图
 
打包服务提供者与服务消费者
在这两个项目的pom.xml文件执行以下操作:点击右键,Run as - Maven build… 在弹出的选项框中Golas 填写:clean package,如下图
使用SpringBoot+Dubbo搭建微服务笔记

文章插图
 
点击run,打包成功后如下图:
使用SpringBoot+Dubbo搭建微服务笔记

文章插图
 
将两个项目的target文件夹中拿出打好的jar包放到某个文件夹下备用,如下图
使用SpringBoot+Dubbo搭建微服务笔记

文章插图
 
使用java -jar 命令依次运行dubbo-provider 与dubbo-consumer,顺序不能搞错,并且得确保provider启动完成后再启动consumer,否则后期会出现空指针异常 。
至此,我们总共启动了四个服务,分别是zookeeper注册中心、dubbo-admin、dubbo-provider、dubbo-consumer,如下图:


推荐阅读