Ribbon 是 Netflix 发布的云中间层服务开源项目,其主要功能是提供客户侧软件负载均衡算法,将 Netflix 的中间层服务连接在一起。Eureka 是一个 RESTful 服务,用来定位运行在 AWS 域(Region)中的中间层服务。本文介绍 Eureka 和 Ribbon 的集成,附带 Ribbon 自定义负载均衡算法示例。
Ribbon 和 Eureka 的集成,其实也就是让 Ribbon 充当 Eureka 架构中的 Application Client 角色。本文示例基于前边相关博客中的 demo 而写。阅读本文最好参考一下《云中间层服务 - 区域感知负载均衡器 Ribbon》、《Eureka 的 Application Client 客户端的运行示例》。
Why Eureka need Ribbon?
Eureka 附带客户端库,为何还要 Ribbon 呢?
Ribbon 的负载均衡算法、区域感知负载均衡器久经考验,可以直接拿来使用。
Why Ribbon need Eureka?
熟悉 Ribbon 的同学都知道,Ribbon 维护了一个服务器列表,如果服务器有宕机现象,Ribbon 能够自行将其剔除;但如果该服务器故障排除,重新启动,或者增加新的负载节点,我们需要手工调用 Ribbon 的接口将其动态添加进 Ribbon 的服务器列表。这样明显不够尽如人意。如何能够在服务节点启动时,自行添加服务列表?—— Eureka。Eureka 提供了 Application Service 客户端的自行注册的功能。此外,Eureka 的缓存机制能够防止大规模宕机带来的灾难性后果。
下面开始我们的集成。进行以下操作之前,请确保 Eureka Server 已启动,Eureka Application Service 客户端已注册到 Server(参考《Eureka 的 Application Client 客户端的运行示例》)。
1. 加入 ribbon-eureka 依赖包
http://mvnrepository.com/artifact/com.netflix.ribbon/ribbon-eureka 选择合适的版本下载,作者下载的是 ribbon-eureka-0.3.12.jar。
2. 配置的初始化
配置文件基本采用《Eureka 的 Application Client 客户端的运行示例》Eureka Application Client 客户端配置。另外增添以下配置项:
启用客户端负载均衡器并将其配置为 DynamicServerListLoadBalancer 或其子类(这个无须在配置中体现,因为这个是 Eureka 和 Ribbon 集成默认为 true 的)。
将 ServerList 配置为 com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList。
配置服务器(负载均衡节点)的刷新频率(可选。默认是为 30 秒)。
为 Eureka 客户端配置服务器的虚拟地址(VIP 地址),并确保这个地址匹配到服务器(Application Service)注册 Eureka Server 时所用到的那个。
总之就是在《Eureka 的 Application Client 客户端的运行示例》基础上添加了以下配置项:
[plain] view plain copy print?
myclient.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
# refresh every minute
myclient.ribbon.ServerListRefreshInterval=60000
# movieservice is the virtual address that the target server(s) uses to register with Eureka server
myclient.ribbon.DeploymentContextBasedVipAddresses=movieservice
配置文件的初始化仍然采用《Eureka 的 Application Client 客户端的运行示例》中的配置初始化方法:
// Register with Eureka
DiscoveryManager.getInstance().initComponent(
new MyDataCenterInstanceConfig(),
new DefaultEurekaClientConfig());
ApplicationInfoManager.getInstance().setInstanceStatus(
InstanceStatus.UP);
3. 自定义负载均衡算法
负载均衡算法,简单 demo 起见,使用随机算法,就用 ribbon-core 类库里的 com.netflix.loadbalancer.RandomRule 所提供的随机负载算法,拿到侍服主机:
[java] view plain copy print?
// get LoadBalancer instance from configuration, properties file
DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("myclient");
// use RandomRule 's RandomRule algorithm to get a random server from lb 's server list
RandomRule randomRule = new RandomRule();
Server randomAlgorithmServer = randomRule.choose(lb, null);
logger.debug("random algorithm server host:" + randomAlgorithmServer.getHost() + ";port:" + randomAlgorithmServer.getPort());
4. Application Client 网络请求
请求代码和《Eureka 的 Application Client 客户端的运行示例》中的一般无二,在此不再赘述。
5. Application Client 关闭时取消注册
取消代码和《Eureka 的 Application Client 客户端的运行示例》中的一般无二,在此不再赘述。
6. 运行 demo
新建一个项目(不要和 Application Service 的 demo 跑在同一个项目下),现在我们把完整的 Eureka Application Client 和 Ribbon Client 集成的代码整理一下。
[java] view plain copy print?
/*
* Copyright 2012 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.eureka;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.appinfo.MyDataCenterInstanceConfig;
import com.netflix.client.ClientFactory;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryManager;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.Server;
/**
* Sample Eureka client that discovers the service using Eureka and sends
* requests.
*
* @author Karthik Ranganathan
*
*/
public class SampleEurekaRibbonClient {
private static final Logger logger = LoggerFactory
.getLogger(SampleEurekaRibbonClient.class);
public void sendRequestToServiceUsingEureka() {
// Register with Eureka
DiscoveryManager.getInstance().initComponent(
new MyDataCenterInstanceConfig(),
new DefaultEurekaClientConfig());
ApplicationInfoManager.getInstance().setInstanceStatus(
InstanceStatus.UP);
// get LoadBalancer instance from configuration, properties file
DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("myclient");
// show all servers in the list
List<Server> list = lb.getServerList(false);
Iterator<Server> it = list.iterator();
while (it.hasNext()) {
Server server = it.next();
logger.debug("application service host:" + server.getHost() + ";port=" + server.getPort());
}
// use RandomRule 's RandomRule algorithm to get a random server from lb 's server list
RandomRule randomRule = new RandomRule();
Server randomAlgorithmServer = randomRule.choose(lb, null);
logger.debug("random algorithm server host:" + randomAlgorithmServer.getHost() + ";port:" + randomAlgorithmServer.getPort());
// communicate with the server
Socket s = new Socket();
try {
s.connect(new InetSocketAddress(randomAlgorithmServer.getHost(), randomAlgorithmServer.getPort()));
} catch (IOException e) {
logger.error("Could not connect to the server :"
+ randomAlgorithmServer.getHost() + " at port " + randomAlgorithmServer.getPort());
}
try {
logger.debug("Connected to server. Sending a sample request");
PrintStream out = new PrintStream(s.getOutputStream());
out.println("Sample request " + new Date());
String str = null;
logger.debug("Waiting for server response..");
BufferedReader rd = new BufferedReader(new InputStreamReader(
s.getInputStream()));
str = rd.readLine();
if (str != null) {
logger.debug("Received response from server. Communication all fine using Eureka :");
logger.debug("Exiting the client. Demo over..");
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
this.unRegisterWithEureka();
}
public void unRegisterWithEureka() {
// Un register from eureka.
DiscoveryManager.getInstance().shutdownComponent();
}
public static void main(String[] args) {
SampleEurekaRibbonClient sampleEurekaRibbonClient = new SampleEurekaRibbonClient();
sampleEurekaRibbonClient.sendRequestToServiceUsingEureka();
}
}
之后是把配置文件、log4j 文件整理一下,运行 SampleEurekaRibbonClient,日志显示 demo 成功。
参考资料
https://github.com/Netflix/ribbon/wiki/Programmers-Guide
本文来源:http://blog.csdn.net/defonds/article/details/38016301
相关推荐
本案例是一个SpringCloud案例,项目继承了Eureka、Ribbon。
学习springboot服务注册和负载均衡调用很好的一个例子
springcloud高版本的整合:包括eureka注册中心、feign、zuul、ribbon、hystrix、turbine监控,如有需要后续上传sleuth+elk、config服务集群、admin等demo,这些资源都是我亲自编写,绝对可用
本案例是SpringCloud项目案例,集成了Eureka、Ribbon、Feign等。
本案例是SpringCloud项目案例,仅集成了Eureka,后续会持续集成Ribbon等。
随着互联网的迅速发展,传统架构已经无法满足我们持续集成、持续交付的需求。在这种现状下,诞生了微服务。 微服务是当前架构领域的热门话题。笔者有幸参与了全球微服务架构高峰论坛。Spring Cloud是一个微服务架构...
带有Spring WebFlux和Spring Cloud的React式微服务 可以在这里找到详细的描述:
Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡,下载即可部署,最简单的demo
Ribbon(客户端负载均衡)(Eureka 集成) 删除对 *-eureka.properties 的需要 在适当的地方使用 spring boot 值作为默认值 关闭时从 eureka 同步删除服务 动态刷新日志级别 使用 hystrix/ribbon/eureka 集成...
除了Feign自带的注解外它还支持JAX-RS注解,SpringCloud又为Feign增加了对SpringMVC注解的支持,同时为了能够使用和Spring Web中默认使用的相同的httpMessageConverter,SpringCloud集成了Ribbon和Eureka,用来在...
Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。
集成了eureka, ribbon,feign, hystrix熔断器, zuul网关, sleuth zipkin 服务调用链跟踪,actuator 和turbine 聚合监控,和spring cloud config配置中心。工程结构介绍:eureka-server:服务注册中心;eureka-client: ...
Spring Cloud集成Ribbon 、 Spring Cloud集成Zuul Spring Cloud集成Feign Spring Cloud集成Hystrix Spring Cloud Config Spring Cloud Sleuth https://spring.io/projects/spring-cloud-sleuth#overview 非Spring ...
springboot集成cmybaits-freemark项目基本结构demo,集成Eureka微服务的注册发现与消费服务demo,集成Ribbon负载均衡及应用demo,集成hystrix等断路由(雪崩效应)demo......
该项目通过自动配置和绑定到 Spring 环境以及其他 Spring 编程模型惯用语法为 Spring Boot 应用程序提供了 Netflix OSS 集成。 Spring Cloud Netflix 具有声明式 REST 客户端、断路器、服务发现、客户端负载...
3. 服务调用和熔断:Spring Cloud通过集成Ribbon和Hystrix等组件,提供了服务调用和熔断的功能。通过使用这些组件,可以实现服务之间的通信和容错处理,提高系统的稳定性。 4. 配置管理:Spring Cloud提供了统一的...
各个项目进行一个简单的搭建,并使用docker部署,目前包含的项目有Eureka、Ribbon、Feign、Hystrix、Hystrix Dashboard、Turbine聚合监控、Zuul、SpringBootAdmin等Spring Config 、OAuth2未集成进来,但是在我的...
压缩包中有5个服务。2个Eureka相互启动作为...同时还简单的集成了Feign和Hystrix。没有业务代码,只是单纯的搭起架构,适合初学者学习使用。对应搭建教程 https://blog.csdn.net/IT_Java_Roy/article/details/98964590
此文件是springboot跟springcloud的整合,搭建一个eureka的服务注册中心,并有服务提供者跟 ribbon消费者和feign消费者 实现负载均衡代码。博客原文:https://blog.csdn.net/u013083284/article/details/83583479
springcloud技术初阶项,集成eureka,ribbon,feign,熔断器,springconfig......