六、搭建服务消费者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); }}至此,整个项目便搭建完毕,三个项目的项目结构如下
文章插图
七、上线运行1、打包运行
安装API项目到本地maven仓库
在dubbo-common项目的pom.xml文件上点击右键,Run as - Maven install
文章插图
打包服务提供者与服务消费者
在这两个项目的pom.xml文件执行以下操作:点击右键,Run as - Maven build… 在弹出的选项框中Golas 填写:clean package,如下图
文章插图
点击run,打包成功后如下图:
文章插图
将两个项目的target文件夹中拿出打好的jar包放到某个文件夹下备用,如下图
文章插图
使用java -jar 命令依次运行dubbo-provider 与dubbo-consumer,顺序不能搞错,并且得确保provider启动完成后再启动consumer,否则后期会出现空指针异常 。
至此,我们总共启动了四个服务,分别是zookeeper注册中心、dubbo-admin、dubbo-provider、dubbo-consumer,如下图:
推荐阅读
- 在JavaScript中使用getter和setter可行吗
- 搭建nginx+tomcat+Java的负载均衡环境
- 公道杯使用技巧
- 2020关于C1驾照的使用新规,赶紧看看
- 使用手机离不开充电器,那充电器及USB接口你了解多少呢?
- EMUI:你真的会使用双卡手机吗?
- 使用苹果手机时,这些需要注意的点,你都知道吗?
- 小米刷机后使用不习惯,想要恢复原系统?可以采用这种方式来恢复
- 骨灰级果粉总结出来的使用iphone小技巧,总有你还不知道的
- 如何判断手机正在使用的充电器是不是原装的?