zookeeper(七)搭建dubbo服务
2020-01-28 本文已影响0人
若琳丶
前言
此次搭建为了方便调试和启动,使用的是windows环境。这里为了方(tou)便(lan),直接使用的Springboot作为启动项目。由于公司项目使用的是xml配置方式,所以此次先用xml配置,再使用注解的方式进行配置。
一、搭建 zookeeper
windows 环境和 Linux 环境所使用的的 zookeeper 包都是相同的,zookeeper 包中自带两种环境的启动程序。就像之前 zk 的启动方式一样。
解压zk包,复制zoo.cfg
image.png
启动 zkServer
在bin目录下,双击 zkServer.cmd
image.png
当然也可以通过 cmd 窗口进行打开
启动 zkCli
我们像之前那样启动一下zkCli测试一下
image.png
可以看到 zk 上的 zookeeper 节点,证明 zkServer 启动成功
二、搭建 dubbo 服务
2.1 项目结构
项目结构
由于此次是练习搭建,所以项目结构大体是按照公司的结构来搞。项目包含三个子模块:
- dubbo-api:包含所有 dubbo 接口和 pojo
- dubbo-order:模拟订单模块,包含订单接口实现
- dubbo-user:模拟用户模块,包含用户接口实现
2.1.1 dubbo-api
image.png
2.1.1.1 首先是 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.dubbo.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
2.1.1.2 pojo
用户信息
package com.dubbo.example.pojo;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfo implements Serializable {
private Long id;
private String name;
}
订单信息
package com.dubbo.example.pojo;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderInfo implements Serializable {
private Long id;
private Long userId;
private String name;
}
2.1.1.3 service interface
userService
package com.dubbo.example.api;
import com.dubbo.example.pojo.UserInfo;
import java.util.List;
public interface UserService {
List<UserInfo> queryUserInfoList();
}
orderService
package com.dubbo.example.api;
import com.dubbo.example.pojo.OrderInfo;
import java.util.List;
public interface OrderService {
List<OrderInfo> queryOrderInfoByUserId();
}
2.1.2 dubbo-order
image.png
2.1.2.1 首先是 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 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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dubbo.example</groupId>
<artifactId>dubbo-order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-order</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.dubbo.example</groupId>
<artifactId>dubbo-api</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>
2.1.2.2 springboot启动类
DubboOrderApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({"classpath:*.xml"})
public class DubboOrderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboOrderApplication.class, args);
}
}
配置引入所有 xml 文件
2.1.2.3 配置文件
application.properties
内容比较简单,只要定义端口号
server.port=8080
dubbo-register.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用名称 -->
<dubbo:application name="dubbo-order"/>
<!-- zookeeper注册中心地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 协议名称(dubbo)和dubbo端口号 -->
<dubbo:protocol name="dubbo" port="20880"/>
</beans>
dubbo-provider.xml
对外开放的 dubbo 服务配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 对外提供接口 orderService -->
<dubbo:service interface="com.dubbo.example.api.OrderService" ref="orderService"
timeout="18000" version="1.0.0"/>
</beans>
dubbo-consumer.xml
作为消费者的dubbo配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 引用dubbo接口 userService -->
<dubbo:reference id="userService" interface="com.dubbo.example.api.UserService"
check="false" version="1.0.0"/>
</beans>
2.1.2.4 orderService 的实现
package com.dubbo.example.impl;
import com.dubbo.example.api.OrderService;
import com.dubbo.example.pojo.OrderInfo;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service("orderService")
public class OrderServiceImpl implements OrderService {
@Override
public List<OrderInfo> queryOrderInfoByUserId() {
OrderInfo zhangsna = new OrderInfo(1L, 1L, "zhangsna");
OrderInfo lisi = new OrderInfo(2L, 1L, "lisi");
OrderInfo wangwu = new OrderInfo(3L, 2L, "wangwu");
List<OrderInfo> list = new ArrayList<>();
list.add(zhangsna);
list.add(lisi);
list.add(wangwu);
return list;
}
}
2.1.2.5 测试 controller
package com.dubbo.example.controller;
import com.alibaba.fastjson.JSON;
import com.dubbo.example.api.OrderService;
import com.dubbo.example.api.UserService;
import com.dubbo.example.pojo.OrderInfo;
import com.dubbo.example.pojo.UserInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {
@Resource
private OrderService orderService;
@Resource
private UserService userService;
@RequestMapping("info")
@ResponseBody
public String queryOrderInfo() {
List<UserInfo> userInfos = userService.queryUserInfoList();
List<OrderInfo> orderInfos = orderService.queryOrderInfoByUserId();
HashMap<Object, Object> map = new HashMap<>();
map.put("users", userInfos);
map.put("orders", orderInfos);
log.info("result: {}", map);
return JSON.toJSONString(map);
}
}
2.1.3 dubbo-user
dubbo-user 与 dubbo-order 的结构相差无几,此处不偷懒,还是贴一下。
image.png
2.1.3.1 pom 文件
<?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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dubbo.example</groupId>
<artifactId>dubbo-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-user</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.dubbo.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.1.3.2 springboot 启动类
package com.dubbo.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({"classpath:*.xml"})
public class DubboUserApplication {
public static void main(String[] args) {
SpringApplication.run(DubboUserApplication.class, args);
}
}
2.1.3.3 配置文件
application.properties
server.port=8081
dubbo-register.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用名称 -->
<dubbo:application name="dubbo-user"/>
<!-- zookeeper注册中心地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 协议名称(dubbo)和dubbo端口号 -->
<dubbo:protocol name="dubbo" port="20881"/>
</beans>
dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 对外提供服务 userService -->
<dubbo:service interface="com.dubbo.example.api.UserService" ref="userService"
timeout="18000" version="1.0.0"/>
</beans>
dubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 引用dubbo接口 orderService -->
<dubbo:reference id="orderService" interface="com.dubbo.example.api.OrderService"
check="false" version="1.0.0"/>
</beans>
2.1.3.4 接口实现
package com.dubbo.example.impl;
import com.dubbo.example.api.UserService;
import com.dubbo.example.pojo.UserInfo;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Override
public List<UserInfo> queryUserInfoList() {
List<UserInfo> list = new ArrayList<>();
list.add(new UserInfo(1L, "zjangsan"));
list.add(new UserInfo(2L, "lisi"));
list.add(new UserInfo(3L, "wangwu"));
return list;
}
}
2.1.3.5 测试 controller
package com.dubbo.example.controller;
import com.alibaba.fastjson.JSON;
import com.dubbo.example.api.OrderService;
import com.dubbo.example.api.UserService;
import com.dubbo.example.pojo.OrderInfo;
import com.dubbo.example.pojo.UserInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@Resource
private OrderService orderService;
@RequestMapping("info")
@ResponseBody
public String getUserInfo() {
List<UserInfo> userInfos = userService.queryUserInfoList();
List<OrderInfo> orderInfos = orderService.queryOrderInfoByUserId();
HashMap<Object, Object> map = new HashMap<>();
map.put("users", userInfos);
map.put("orders", orderInfos);
log.info("result: {}", map);
return JSON.toJSONString(map);
}
}
2.2 测试
访问 http://localhost:8081/user/info,它是 dubbo-user 中的测试接口
image.png
结果是我们理想中的结果,请求到的 orderService 的数据。
我们再看一下 zookeeper 的 Server窗口,发现里面已经注册了我们的服务节点
image.png