locust 2.41.6
dubbo 客户端 dubbo-python2
代码如下:
class MyDubboClient:
def init(self,environment):
self.environment = environment
self.client = None
def connect(self):
print("开始连接 Dubbo 服务...")
self.client = DubboClient(
interface = "xxxxxxx",
version = "",
dubbo_version = "2.7.8",
host = "xxx.xxx.xxx.xxx:xxxxx")
print("client:",self.client)
print("Dubbo 服务连接成功.")
print("environment:",self.environment)
print(dir(self.environment))
print("environment.events:",self.environment.events)
print("environment.events:", self.environment.available_user_classes)
def call(self,method, args):
res = ""
try:
res = self.client.call(method=method, args=[args],timeout=3)
print(f"接口调用成功,返回结果: {res}")
except Exception as e:
exception = e
print(f"接口调用失败,错误信息: {exception}")
if exception:
self.environment.events.request.fire(
request_type="dubbo",
request_method="call",
name=method,
exception=exception,
response_time="response_time",
response_length=len(str(exception)),
)
else:
self.environment.events.request.fire(
request_type="dubbo",
request_method="call",
name=method,
response_time="response_time",
response_length=len(res),
)
class MyDubboUser(User):
abstract = True
wait_time = between(1, 3)
def init(self, environment):
super().init(environment)
self.my = MyDubboClient(environment)
class ApiUser(MyDubboUser):
def on_start(self):
self.my.connect()
@task(1)
def aabbcc(self):
XXXXXRequest = Object(
"com.xxxxxxxxx"
)
startTime = Object("java.time.LocalDateTime")
startTime["date"] = Object("java.time.LocalDate")
startTime["date"]["year"] = 2025
startTime["date"]["month"] = 9
startTime["date"]["day"] = 1
startTime["time"] = Object("java.time.LocalTime")
startTime["time"]["hour"] = 23
startTime["time"]["minute"] = 59
startTime["time"]["second"] = 59
startTime["time"]["nano"] = 0
XXXXXRequest["startTime"] = startTime
endTime = Object("java.time.LocalDateTime")
endTime["date"] = Object("java.time.LocalDate")
endTime["date"]["year"] = 2025
endTime["date"]["month"] = 9
endTime["date"]["day"] = 1
endTime["time"] = Object("java.time.LocalTime")
endTime["time"]["hour"] = 23
endTime["time"]["minute"] = 59
endTime["time"]["second"] = 59
endTime["time"]["nano"] = 0
XXXXXRequest["endTime"] = endTime
method = "abc_method"
res = self.my.call(method=method, args=XXXXXRequest)
print(f"接口调用成功,返回结果: {res.get('code')}, 返回数据: {res.get('data')}")
if name == "main":
print(run_single_user(ApiUser))
首先, 不使用 locust 的时候 使用 dubbo-python2 编写的客端调方法是能够返回值的。
增加 locust 后 会卡在 res = self.client.call(method=method, args=[args],timeout=3) 这步。 timeout 超时 后会报出 Socket Read timed out. (read timeout=3). (单独使用客户端调用后很快能返回)
不加 timout 后 等待一段时间后会打印
[2025-10-20 18:54:33,313] 电脑用户/ERROR/dubbo: 'xxx.xxx.xxx.xxx:xxxxx'
[2025-10-20 18:54:33,348] 电脑用户/ERROR/dubbo: 'xxx.xxx.xxx.xxx:xxxxx'
[2025-10-20 18:54:33,380] 电脑用户/ERROR/dubbo: 'xxx.xxx.xxx.xxx:xxxxx'
[2025-10-20 18:54:33,415] 电脑用户/ERROR/dubbo: 'xxx.xxx.xxx.xxx:xxxxx'