使用Spring Boot+Dubbo+Zookeeper搭建第一个分布式项目(详细介绍如何搭建分布式项目,下篇)
一、概述
- 上一篇讲解了搭建定义接口项目和服务提供者项目,这篇主要讲解如何搭建服务消费者项目。
二、搭建步骤
步骤一:创建服务消费者项目,创建一个名为 hello-dubbo-service-user-consumer
的Spring Boot项目,该项目只负责定义接口,创建好项目后如下图所示。
步骤二:在pom.xml下添加以下配置。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--状态检查的依赖,(健康监控)配置和使用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.czd</groupId>
<artifactId>hello-dubbo-service-user-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
步骤三:在resources目录下创建application.yml配置文件,并且添加以下内容。
# Spring boot application
spring:
application:
#原则上和项目名称一致
name: hello-dubbo-service-user-consumer
server:
port: 9090
# UserService service version
user:
service:
version: 1.0.0
# Dubbo Config properties
dubbo:
scan:
basePackages: com.czd.hellodubboserviceuserconsumer.controller
# ApplicationConfig Bean
application:
#原则上和项目名称一致,并且一般id和name是一样的
id: hello-dubbo-service-user-consumer
name: hello-dubbo-service-user-consumer
protocol:
id: dubbo
name: dubbo
port: 12345
# RegistryConfig Bean
registry:
id: zookeeper
address: zookeeper://192.168.43.86:2181?backup=192.168.43.86:2182,192.168.43.86:2183
# Dubbo Endpoint (default status is disable)
endpoints:
dubbo:
enabled: true
management:
# Enables Dubbo All Endpoints
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
# Dubbo Health
health:
dubbo:
status:
# StatusChecker Name defaults (default : "memory", "load" )
#状态检查器名称默认值(默认值:“内存”、“加载”)
defaults: memory
endpoints:
web:
exposure:
include: "*"
步骤四:在com.czd.hellodubboserviceuserprovider包下创建一个controller包,并且在此包下创建UserServiceController类。
【1】建好后如下图所示:
【2】UserServiceController类代码如下:
package com.czd.hellodubboserviceuserconsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.czd.hello.dubbo.service.user.api.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author czd
*/
@RestController
public class UserServiceController {
@Reference(version = "${user.service.version}")
private UserService userService;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String sayHello(){
return userService.sayHello();
}
}
三、测试项目
前提:这个是在Zookeeper集群的环境下运行的,所以想要运行成功这个分布式项目,首先得搭建好Zookeeper集群,可以看我的另外一篇博客是如何搭建Zookeeper集群的《在Linux系统下基于Docker安装Zookeeper集群》。
测试:
- 先运行 hello-dubbo-service-user-provider 项目
- 再运行 hello-dubbo-service-user-consumer项目
- 打开网址:localhost:9090/hello
注意:我这里端口好改成9090了,如果想要更改tomcat端口好可以去hello-dubbo-service-user-consumer项目的application.yml中的server.port进行更改。
运行效果如图所示:
这样子使用Spring Boot+Dubbo+Zookeeper搭建第一个分布式项目就成功了!