首页 技术 正文
技术 2022年11月23日
0 收藏 513 点赞 3,719 浏览 5606 个字

dubbo 实现简易分布式服务

服务器需要搭建zookeeper环境

zookeeper端口2181

还需要有java环境

1.需求

某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;

我们现在 需要创建两个服务模块进行测试

模块 功能
订单服务web模块 创建订单等
用户服务service模块 查询用户地址等

测试预期结果

订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。

2.工程架构

3.创建项目

3.1 公共接口层 ego-interface

简单maven项目即可

1.修改pom.xml文件

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.vsersion}</version>
</dependency>
</dependencies>

2.创建实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserAddress implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String userId;
private String userAddress;
}

3.创建公共接口

UserService
public interface UserService {
/**
* 查询用户的所有地址
* @return
*/
public List<UserAddress> queryAllAddress(Integer id);
}
OrderService
public interface OrderService {
/**
* 根据用户id 查找订单
* @param uid 用户id
* @return 订单
*/
public List<UserAddress> initOrder(Integer uid);
}

3.2 用户服务模块 ego-user-service-provider (服务提供者)

1.修改pom.xml文件

<dependencies>
<!-- 这里是公共接口层哦-->
<dependency>
<groupId>com.hgzy</groupId>
<artifactId>03-ego-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.7</version>
</dependency> <dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
</dependencies>

2.创建服务实体类

@Service
public class UserServiceImpl implements UserService {
private static List<UserAddress> addresses=new ArrayList<UserAddress>();
static {
addresses.add(new UserAddress(1,"1","湖南省株洲市荷塘区湖南化工职业技术学院"));
addresses.add(new UserAddress(2,"2","湖南省永州市祁阳县"));
} @Override
public List<UserAddress> queryAllAddress(Integer id) {
return addresses;
}
}

3.applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 声明应用程序的名称 -->
<dubbo:application name="ego-user-service-provider"/>
<!--指定注册中心的地址 -->
<dubbo:registry
address="zookeeper://服务器IP地址:2181"/>
<!--使用dubbo协议,将服务暴露在20880端口 -->
<dubbo:protocol name="dubbo" port="20880"/><!-- 声明要暴露的实现类的对象-->
<bean id="userService"
class="com.hgzy.service.UserServiceImpl"/> <!-- 进行服务暴露 -->
<dubbo:service interface="com.hgzy.service.UserService"
ref="userServiceImpl"/>
</beans>

4.测试服务

TestProvider
public class TestProvider {
public static void main(String[] args) throws IOException {
ApplicationContext ac=
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("服务提供者启动成功");
System.in.read();
}
}

3.3 订单服务(服务消费者) ego-order-service-consumer

1.修改pom.xml文件

<dependencies>
<!-- 这里是公共接口层哦-->
<dependency>
<groupId>com.hgzy</groupId>
<artifactId>03-ego-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.7</version>
</dependency> <dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
</dependencies>

2.创建服务实体类

@Service
public class OrderServiceImpl implements OrderService { @Reference
private UserService userService; public void setUserService(UserService userService) {
this.userService = userService;
} @Override
public List<UserAddress> initOrder(Integer uid) {
return userService.queryAllAddress(uid);
}
}

3.applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 声明服务名称-->
<dubbo:application name="02-ego-order-service-consumer"/>
<!-- 指定注册中心地址-->
<dubbo:registry
address="zookeeper://服务器ip地址:2181"/>
<!-- 生成远程调用对象-->
<dubbo:reference
id="userService" interface="com.hgzy.service.UserService"/><!-- 创建订单对象-->
<bean id="orderService" class="com.hgzy.service.OrderServiceImpl">
<property name="userService" ref="userService"/>
< </bean>
</beans>

测试类

public class TestConsumer {
public static void main(String[] args) throws IOException {
ApplicationContext ac=
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
OrderService orderService = (OrderService) ac.getBean(OrderService.class);
for (UserAddress userAddress : orderService.initOrder(1)) {
System.out.println(userAddress);
}
System.in.read();
}
}

微信扫一扫

支付宝扫一扫

本文网址:https://www.zhankr.net/140856.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:7,097
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:4,454
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:566 阅读:5,265
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:5,147
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:6,314
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,859