一、目前用最多的项目
作用 | 用得最多 | 成功的替代项目 | 正在推进替代项目 |
---|---|---|---|
服务发现 | 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 | 高 | 毫秒级 | 十万 | 有待加强 | 待加强 |