如何在 java 中实施负载均衡器
How to implement Load Balancer in java
我想实现一种模式,如果错误百分比超过阈值,该模式会自动或手动停止对外部服务 (i.e.port) 的所有请求一段时间。我在同一台机器上有两个服务器实例 运行,端口不同(例如:2401、2402)。
现在的要求是,如果端口 2401 超过了错误百分比阈值,那么我想停止对这个端口 (2401) 的所有请求一段时间,然后路由到另一个端口 (2402)。我不确定哪种算法适合这个。
我阅读了一些文章,但没有在 Java 代码中获得有关负载均衡器实现的完整信息。
提前致谢,
萨提什
@Svetlin 完全正确,你可以用 hystrix 来实现。这是一个示例代码。根据您的要求进行调整。
@HystrixCommand(fallbackMethod = "fallBackForProductDetail", groupKey = "CircuitBreaker", commandKey = "frontend-productdetail", threadPoolKey = "frontend-productdetail",
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),//Time before which this call is supposed to complete. if not throw exception. this is Optional
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), // Number of requests before which the cicuit is open
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "1200000"),//20 minutes circuit will be open
},
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "180000")// 3 minutes rolling window. i.e the errors calculated for every 3 minute window.
})
public String callProductDetail(.....){
// call server1
}
// Return type and arguments should be exactly same as the method for wich this is fallback. (With an optional Throwable argument to catch exception)
public String fallBackForProductDetail(...){
// call server2
}
现在来解释这个行为。当对 server1 的请求失败时,计数器递增,调用转到回退方法 (fallBackForProductDetail) 并在回退方法内执行代码。相同的行为一直持续到达到阈值(在本例中为 5)。达到阈值后,控件甚至不进入 main 方法(callProductDetail),直接进入 fallback 方法。这发生了 sleepWindowInMilliseconds
(在这种情况下为 20 分钟)。
希望对您有所帮助。
我想实现一种模式,如果错误百分比超过阈值,该模式会自动或手动停止对外部服务 (i.e.port) 的所有请求一段时间。我在同一台机器上有两个服务器实例 运行,端口不同(例如:2401、2402)。
现在的要求是,如果端口 2401 超过了错误百分比阈值,那么我想停止对这个端口 (2401) 的所有请求一段时间,然后路由到另一个端口 (2402)。我不确定哪种算法适合这个。
我阅读了一些文章,但没有在 Java 代码中获得有关负载均衡器实现的完整信息。
提前致谢, 萨提什
@Svetlin 完全正确,你可以用 hystrix 来实现。这是一个示例代码。根据您的要求进行调整。
@HystrixCommand(fallbackMethod = "fallBackForProductDetail", groupKey = "CircuitBreaker", commandKey = "frontend-productdetail", threadPoolKey = "frontend-productdetail",
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),//Time before which this call is supposed to complete. if not throw exception. this is Optional
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), // Number of requests before which the cicuit is open
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "1200000"),//20 minutes circuit will be open
},
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "180000")// 3 minutes rolling window. i.e the errors calculated for every 3 minute window.
})
public String callProductDetail(.....){
// call server1
}
// Return type and arguments should be exactly same as the method for wich this is fallback. (With an optional Throwable argument to catch exception)
public String fallBackForProductDetail(...){
// call server2
}
现在来解释这个行为。当对 server1 的请求失败时,计数器递增,调用转到回退方法 (fallBackForProductDetail) 并在回退方法内执行代码。相同的行为一直持续到达到阈值(在本例中为 5)。达到阈值后,控件甚至不进入 main 方法(callProductDetail),直接进入 fallback 方法。这发生了 sleepWindowInMilliseconds
(在这种情况下为 20 分钟)。
希望对您有所帮助。