文章目录
- 一、会员服务
-
- [1.1 根据手机号查询会员信息](#1.1 根据手机号查询会员信息)
-
- [1.1.1 传递普通参数](#1.1.1 传递普通参数)
- [1.1.2 实现代码(统一返回字符串类型数据)](#1.1.2 实现代码(统一返回字符串类型数据))
- [1.2 添加会员](#1.2 添加会员)
-
- [1.2.1 传递 对象参数](#1.2.1 传递 对象参数)
- [1.2.2 实现代码](#1.2.2 实现代码)
- [1.3 根据日期统计会员数](#1.3 根据日期统计会员数)
-
- [1.3.1 传递 字符串列表](#1.3.1 传递 字符串列表)
- [1.3.2 实现代码](#1.3.2 实现代码)
- 二、预约设置服务
-
- [2.1 添加预约设置](#2.1 添加预约设置)
-
- [2.1.1 传递参数](#2.1.1 传递参数)
- [2.1.2 实现代码](#2.1.2 实现代码)
- [2.2 按月统计预约设置信息](#2.2 按月统计预约设置信息)
-
- [2.2.1 传递参数](#2.2.1 传递参数)
- [2.2.2 实现代码](#2.2.2 实现代码)
- [2.3 根据日期修改预约设置数量](#2.3 根据日期修改预约设置数量)
-
- [2.3.1 传递参数](#2.3.1 传递参数)
- [2.3.2 实现代码](#2.3.2 实现代码)
- 三、用户服务
-
- [3.1 根据用户名查询用户信息](#3.1 根据用户名查询用户信息)
-
- [3.1.1 传递参数](#3.1.1 传递参数)
- [3.1.2 实现代码](#3.1.2 实现代码)
- 四、现有代码存在的问题
一、会员服务
1.1 根据手机号查询会员信息
1.1.1 传递普通参数
yacas
dubbo> ls -l MemberService
com.itheima.pojo.Member findByTelephone(java.lang.String) #com.itheima.pojo 包名
Api接口文档的定义:Member findByTelephone(String telephone)
参数:
字符串格式手机号。唯一
返回值:
成功:返回 会员的信息内容。string类型 包裹的 字典数据。
失败:返回 null
1.1.2 实现代码(统一返回字符串类型数据)
统一返回字符串类型数据:因为不同的编程语言对接,但是都有字符串类型。
python
# 导包
from dubboclient import DubboClient
# 1、创建DubboClient对象。---创建 DubboClient 实例,指定 IP、port
dubbo_clt = DubboClient("211.103.136.244", 6502)
# 服务名可以去接口文档中找
# 2、调用服务接口---使用 DubboClient 实例 调用 invoke 方法,传入 服务名、方法名、实参
resp = dubbo_clt.invoke("MemberService", "findByTelephone", "13020210001")
# 3、查看 响应结果
print("响应结果 =", resp)
print("数据类型 =", type(resp)) # 数据类型 = <class 'str'>
1.2 添加会员
1.2.1 传递 对象参数
yacas
dubbo> ls -l MemberService
void add(com.itheima.pojo.Member)
Api接口文档的定义:void add(Member member)
参数:
1. 自定义类 做 参数,根据接口文档,组织 "字典" 格式数据传参
2. 给字典增加 {键k:"class" , 值v:指明 类 对应的完整 包名和类名}
ls -l MemberService 可以查看完整包名和类名。
区分自定义类: 包名不以"java."开头。一般采用:com.公司名.项目名.类名
返回值:
成功:返回 null
失败:返回 Failed
1.2.2 实现代码
python
# 导包
from dubboclient import DubboClient
# 创建实例
dubbo_clt = DubboClient("211.103.136.244", 6502)
data = {"id": 8897, "name": "杜甫", "phoneNumber": "13020241739"}
# 如果class在 原字典 中存在,覆盖原有数据;不存在,添加。
data["class"] = "com.itheima.pojo.Member"
# 远程调用
resp = dubbo_clt.invoke("MemberService", "add", data)
# 打印结果
print("响应结果 =", resp)
print("数据类型 =", type(resp))
1.3 根据日期统计会员数
1.3.1 传递 字符串列表
yacas
dubbo> ls -l MemberService
java.util.List findMemberCountByMonths(java.util.List)
文档API接口定义:List<Integer> findMemberCountByMonths(List<String> months)
参数:
1. 字符串列表。用字符串表示年、月,用"."衔接
如:["2021.3", "2021.9"]
返回值:
成功:返回列表,对应参数设置的月份的会员数。
失败:Failed
1.3.2 实现代码
python
# 导包
from dubboclient import DubboClient
# 创建 dubboclient 的实例。传入 IP、port
dubbo_clt = DubboClient("211.103.136.244", 6502)
list = ["2021.12"]
# 使用实例,调用 invoke方法,传入 服务名、方法名、实参
resp = dubbo_clt.invoke("MemberService", "findMemberCountByMonths", list)
# 查看 响应数据
print("响应结果 =", resp)
print("数据类型 =", type(resp))
二、预约设置服务
2.1 添加预约设置
2.1.1 传递参数
yacas
dubbo> ls -l OrderSettingService
void add(java.util.List)
接口定义:void add(List<OrderSetting> list)
参数:
1. 字典列表。字典有 orderDate 和 number 两个字段。
如:[{"orderDate":"2021-09-20 16:45:12","number":20}]
2. 日期格式:"2021-09-20 16:45:12",必须包含时分秒,否则失败。
返回值:
成功:null
失败:Failed
2.1.2 实现代码
python
# 导包
from dubboclient import DubboClient
# 创建 dubboclient 的实例。传入 IP、port
dubbo_clt = DubboClient("211.103.136.244", 6502)
list = [{"orderDate": "2022-04-29 11:11:12", "number": 66}]
# 使用实例,调用 invoke方法,传入 服务名、方法名、实参
resp = dubbo_clt.invoke("OrderSettingService", "add", list)
# 查看 响应数据
print("响应结果 =", resp)
print("数据类型 =", type(resp))
2.2 按月统计预约设置信息
2.2.1 传递参数
yacas
dubbo> ls -l OrderSettingService
java.util.List getOrderSettingByMonth(java.lang.String)
接口定义:List getOrderSettingByMonth(String date)
参数:
字符串,如:"2021-09"
返回值:
成功:返回字符串类型数据,字符串内容为列表
失败:Failed
2.2.2 实现代码
python
# 导包
from dubboclient import DubboClient
# 创建 dubboclient 的实例。传入 IP、port
dubbo_clt = DubboClient("211.103.136.244", 6502)
date = "2022-12"
# 使用实例,调用 invoke方法,传入 服务名、方法名、实参
resp = dubbo_clt.invoke("OrderSettingService", "getOrderSettingByMonth", date)
# 查看 响应数据
print("响应结果 =", resp)
print("数据类型 =", type(resp))
2.3 根据日期修改预约设置数量
2.3.1 传递参数
yacas
dubbo> ls -l OrderSettingService
void editNumberByDate(com.itheima.pojo.OrderSetting)
接口定义:void editNumberByDate(OrderSetting orderSetting)
参数:
1. 自定义类,用 字典 根据接口文档组织数据
2. 需要使用 class 指定参数对象的类型
如:{"orderDate":"2021-10-13 21:04:33","number":15, "class":"com.itheima.pojo.OrderSetting"}
3. 日期格式为:"2021-10-13 21:04:33",必须包含时分秒
返回值:
成功:null
失败:Failed
2.3.2 实现代码
python
# 导包
from dubboclient import DubboClient
# 创建 dubboclient 的实例。传入 IP、port
dubbo_clt = DubboClient("211.103.136.244", 6502)
data = {"orderDate": "2022-04-02 21:04:33", "number": 115, "class": "com.itheima.pojo.OrderSetting"}
# 使用实例,调用 invoke方法,传入 服务名、方法名、实参
resp = dubbo_clt.invoke("OrderSettingService", "editNumberByDate", data)
# 查看 响应数据
print("响应结果 =", resp)
print("数据类型 =", type(resp))
三、用户服务
3.1 根据用户名查询用户信息
3.1.1 传递参数
yacas
dubbo> ls -l UserService
com.itheima.pojo.User findByUsername(java.lang.String)
接口定义:User findByUsername(String username)
参数:字符串类型,如:'admin'
返回值:
用户存在:返回用户信息
用户不存在:返回null
3.1.2 实现代码
python
# 导包
from dubboclient import DubboClient
# 创建 dubboclient 的实例。传入 IP、port
dubbo_clt = DubboClient("211.103.136.244", 6502)
username = "admin"
# 使用实例,调用 invoke方法,传入 服务名、方法名、实参
resp = dubbo_clt.invoke("UserService", "findByUsername", username)
# 查看 响应数据
print("响应结果 =", resp)
print("数据类型 =", type(resp))
四、现有代码存在的问题
1、代码高度冗余
2、每个方法在调用时,除了关心 参数、返回值以外,还必须 指定 服务名、方法名。
3、在调用时,人为的分辨哪些方法需要额外指定 class 键,及对应值。
4、返回的数据统一为 string 类型。不方便实现断言。
通过 封装 解决上述问题!目标:
- 调用方法时,只需给出 参数,获取返回值即可。不需要关心服务名、方法、class 等。
- 方法调用结束,返回具体的数据类型(字典、列表、空、失败)。