You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
789/BasicExample/BasicExampleTest.java

132 lines
5.3 KiB

/**
* 基础题测试类
* 测试服务注册中心和断路器功能
*/
public class BasicExampleTest {
// 辅助方法:重复字符
private static String repeatChar(char c, int count) {
StringBuilder sb = new StringBuilder(count);
for (int i = 0; i < count; i++) {
sb.append(c);
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println("=== 基础题测试开始 ===\n");
// 测试服务注册中心
testServiceRegistry();
System.out.println("\n" + repeatChar('-', 50) + "\n");
// 测试断路器
testCircuitBreaker();
System.out.println("\n=== 基础题测试完成 ===");
}
// 测试服务注册中心
private static void testServiceRegistry() {
System.out.println("1. 测试服务注册中心功能");
// 获取服务注册中心实例
ServiceRegistry registry = ServiceRegistry.getInstance();
// 注册服务
System.out.println("\n注册服务实例:");
registry.register("userService", "192.168.1.1", 8080);
registry.register("userService", "192.168.1.2", 8080);
registry.register("orderService", "192.168.1.3", 8081);
// 发现服务
System.out.println("\n发现所有服务:");
System.out.println("已注册服务列表: " + registry.getAllServices());
// 测试服务发现
System.out.println("\n发现特定服务实例:");
java.util.List<ServiceRegistry.ServiceInstance> userInstances = registry.discover("userService");
System.out.println("userService 实例: " + userInstances);
java.util.List<ServiceRegistry.ServiceInstance> orderInstances = registry.discover("orderService");
System.out.println("orderService 实例: " + orderInstances);
// 测试获取随机实例
System.out.println("\n获取随机服务实例:");
for (int i = 0; i < 3; i++) {
ServiceRegistry.ServiceInstance instance = registry.getRandomInstance("userService");
System.out.println("随机选择 userService 实例[" + i + "]: " + instance);
}
// 测试注销服务
System.out.println("\n注销服务实例:");
registry.deregister("userService", "192.168.1.1", 8080);
// 检查注销后的服务
System.out.println("\n注销后的 userService 实例: ");
userInstances = registry.discover("userService");
System.out.println(userInstances);
// 测试不存在的服务
System.out.println("\n测试不存在的服务:");
java.util.List<ServiceRegistry.ServiceInstance> nonExistent = registry.discover("paymentService");
System.out.println("不存在的服务发现结果: " + nonExistent);
}
// 测试断路器
private static void testCircuitBreaker() {
System.out.println("2. 测试断路器功能");
// 创建断路器实例(简化配置以便快速测试)
CircuitBreaker breaker = new CircuitBreaker("testService", 3, 2000, 1);
System.out.println("\n初始状态: " + breaker.getState());
// 测试失败触发开路
System.out.println("\n测试失败触发开路:");
for (int i = 1; i <= 4; i++) {
boolean allowed = breaker.allowRequest();
System.out.println("请求 " + i + " 允许通过: " + allowed + ", 当前状态: " + breaker.getState());
if (allowed) {
// 模拟服务调用失败
breaker.recordFailure();
System.out.println(" 记录失败,失败计数: " + breaker.getFailureCount());
}
}
// 测试开路状态拒绝请求
System.out.println("\n测试开路状态拒绝请求:");
for (int i = 1; i <= 3; i++) {
boolean allowed = breaker.allowRequest();
System.out.println("开路状态请求 " + i + " 允许通过: " + allowed + ", 当前状态: " + breaker.getState());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 等待超时进入半开状态
System.out.println("\n等待超时进入半开状态...");
try {
Thread.sleep(2500); // 等待超过重置时间
} catch (InterruptedException e) {
e.printStackTrace();
}
// 测试半开状态下成功请求
System.out.println("\n测试半开状态下成功请求:");
boolean allowed = breaker.allowRequest();
System.out.println("半开状态请求允许通过: " + allowed + ", 当前状态: " + breaker.getState());
if (allowed) {
// 模拟服务调用成功
breaker.recordSuccess();
System.out.println(" 记录成功,断路器状态: " + breaker.getState());
}
// 测试重置断路器
System.out.println("\n测试重置断路器:");
breaker.reset();
System.out.println("重置后的状态: " + breaker.getState() + ", 失败计数: " + breaker.getFailureCount());
}
}