客戶端負載均衡、中間件對比

一、目前用最多的項目

作用 用得最多 成功的替代項目 正在推進替代項目
服務發現 Eureka Consul、Zookeeper Alibaba Nacos
負載均衡器 Ribbon、Nginx、SLB、F5 、A10 SOFARPC zuul、feign、eureka也能實現負載均衡策略、其中核心算法Ribbon
斷路器 Hystrix - Resilience4j、Alibaba Sentinel
聲明式HTTP客戶端 Feign - Retrofit
API網關 Netflix Zuul Gateway -
配置管理 Spring Cloud Config Consul、Zookeeper Alibaba Nacos

二、Ribbon

Ribbon的優缺點:

1、是一個基於 HTTP 和 TCP 的客戶端負載均衡工具。
2、內置的負載均衡算法。
3、實現負載均衡,從一個服務的多臺機器中選擇一臺。
4、容錯能力,異步和響應模型中的多種協議(HTTP,TCP,UDP)支持,緩存和批處理。

三、Ribbon案例

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>ribbon</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>ribbon-core</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>ribbon-loadbalancer</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>io.reactivex</groupId>
    <artifactId>rxjava</artifactId>
    <version>1.0.10</version>
</dependency>
public class RibbonConfiguration {
 
    @Autowired
    IClientConfig ribbonClientConfig;
 
    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new PingUrl();
    }
 
    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new WeightedResponseTimeRule();
    }
}
spring:
  application:
    name: spring-cloud-ribbon
 
server:
  port: 8888
 
ping-server:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:9092,localhost:9999
    ServerListRefreshInterval: 15000
@SpringBootApplication
@RestController
@RibbonClient(
  name = "ping-a-server",
  configuration = RibbonConfiguration.class)
public class ServerLocationApp {
    
    //@ LoadBalanced註冊表明,這個restRemplate是負載均衡的
    @LoadBalanced
    @Bean
    RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
 
    @Autowired
    RestTemplate restTemplate;
 
    @RequestMapping("/server-location")
    public String serverLocation() {
        return this.restTemplate.getForObject(
          "http://ping-server/locaus", String.class);
    }
 
    public static void main(String[] args) {
        SpringApplication.run(ServerLocationApp.class, args);
    }
}
// 服務列表
List<Server> serverList = Lists.newArrayList(new Server("localhost", 8081), new Server("localhost", 8083));
// 構建負載實例
ILoadBalancer loadBalancer = LoadBalancerBuilder.newBuilder().buildFixedServerListLoadBalancer(serverList);
// 調用 5 次來測試效果
for (int i = 0; i < 5; i++) {
    String result = LoadBalancerCommand.<String>builder().withLoadBalancer(loadBalancer).build()
            .submit(new ServerOperation<String>() {
                public Observable<String> call(Server server) {
                    try {
                        String addr = "http://" + server.getHost() + ":" + server.getPort() + "/user/hello";
                        System.out.println(" 調用地址:" + addr);
                        URL url = new URL(addr);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setRequestMethod("GET");
                        conn.connect();
                        InputStream in = conn.getInputStream();
                        byte[] data = new byte[in.available()];
                        in.read(data);
                        return Observable.just(new String(data));
                    } catch (Exception e) {
                        return Observable.error(e);
                    }
                }
            }).toBlocking().first();
    System.out.println(" 調用結果:" + result);
}

四、消息中間件對比

數據可靠性 延遲 單機吞吐 社區 客戶端
ActiveMQ - 萬級 不太活躍 支持全面
RabbitMQ 微秒級 萬級 活躍 支持全面
Kafka 毫秒級 十萬 活躍 支持全面
RocketMQ 毫秒級 十萬 有待加強 待加強
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章