本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3

前言

本文基于前两篇文章eureka-server和eureka-client的实现。

参考

1 Ribbon工程搭建

1.1 创建spring boot工程:eureka-ribbon

1.2 pom.xml所需要依赖的jar包

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

1.3 添加application.yml信息

application.yml

spring:
application:
name: eureka-ribbon
server:
port: 8901 eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

1.4 启动类添加相关注解@EnableDiscoveryClient

package spring.cloud.demo.eurekaribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix; @EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication { public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
} }

@EnableDiscoveryClient启动eureka服务发现相关配置

1.5 创建应用配置类RestTemplateConfig

package spring.cloud.demo.eurekaribbon.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@Configuration
public class RestTemplateConfig { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} }

@LoadBalanced:实现负载均衡,默认轮询。

Ribbon自带的负载规则

  1. RoundRobinRule:系统默认的规则,通过简单的轮询服务列表来选择服务器,其他的规则在很多情况下,仍然使用RoundRobinRule。
  2. AvailablilityFilteringRule:该各种会忽略以下服务器:

无法连接的服务器:在默认情况下,如果3次连接失败,该服务器将会被置为“短路”的状态,该状态将持续30秒,如果再次连接失败,“短路”状态的持续时间将会以几何级增加。可以通过修改niws.loadbalance..connerctionFailureCountThreshold属性来配置连接失败的次数。

并发数过高的服务器:如果连接到该服务器的并发数过高,也会被这个规则忽略,可以通过修改.ribbon.ActiveConnectionLimit属性来设定最高并发数。

  1. WeightedResponseTimeRule:为每个服务器赋予一个权重值,服务器的响应时间越长,该权重值就越少,这个规则会随机选择服务器,这个权重值有可以能会决定服务器的选择。
  2. ZoneAvoidanceRule:该规则以区域、可用服务器为基础,进行服务器选择。使用Zone对服务器进行分类,可以理解为机架或者机房。
  3. BestAvailiableRule:忽略“短路”的服务器,并选择并发数较低的服务器。
  4. RandomRule:随机选择可用服务器。
  5. RetryRule:含有重试的选择逻辑,如果使用RoundRobinRule。

application.yml增加配置:

#RoundRobinRule:系统默认的规则,通过简单的轮询服务列表来选择服务器,其他的规则在很多情况下,仍然使用RoundRobinRule
eureka-client: #对应的服务client的name
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

1.6 创建EurekaRibbonService

package spring.cloud.demo.eurekaribbon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@Service
public class EurekaRibbonService { @Autowired
RestTemplate restTemplate; public String sayHello() {
String message;
try {
message = restTemplate.getForObject("http://eureka-client/info", String.class);
} catch (RestClientException e) {
message = e.getMessage();
}
return message;
}
}

http://eureka-client/info, 其中eureka-client为服务提供者对应的spring.application.name

1.7 创建服务消费者控制类:EurekaRibbonConntroller

package spring.cloud.demo.eurekaribbon.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.demo.eurekaribbon.service.EurekaRibbonService; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@RestController
public class EurekaRibbonConntroller { @Autowired
private EurekaRibbonService eurekaRibbonService; @RequestMapping("/syaHello")
public String syaHello() {
String message = eurekaRibbonService.sayHello();
return "ribbon result: " + message;
}
}

1.8 启动服务

前题保证eureka-server和eureka-client已经正常启动。然后启动eureka-ribbon服务。

在浏览器输入http://localhost:8901/syaHello,如下图所示:

多次刷新后可以看到浏览器显示的是结果中端口是变化的。

结语

至此,一个简单的单点Ribbon服务消费者就搭建完成。

彩蛋

Hystrix Ribbon实现断路器

场景:假如在生产环境中,访问量很大的情况下,那么就会产生很多请求阻塞的情况,然后服务器的内存消耗就会陡增,严重情况下会导致系统的崩溃,也就是常见的雪崩。为了避免这种情况,熔断保护机制就迎刃而生。在访问不通的情况下,要及时作出响应,而不是等待超时。

pom.xml增加相关依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

EurekaRibbonApplication增加注解:@EnableHystrix

package spring.cloud.demo.eurekaribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix; @EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication { public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
} }

修改EurekaRibbonService

package spring.cloud.demo.eurekaribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@Service
public class EurekaRibbonService { @Autowired
RestTemplate restTemplate; @HystrixCommand(
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000"),
@HystrixProperty(name = "execution.isolation.strategy",value = "THREAD")},
fallbackMethod = "syaHelloFailure")
public String sayHello() {
String message;
try {
message = restTemplate.getForObject("http://eureka-client/info", String.class);
} catch (RestClientException e) {
message = e.getMessage();
}
return message;
} public String syaHelloFailure() {
System.out.println("error come in ");
String message = "网络繁忙, 请稍后再试";
return message;
}
}

演示流程

停掉其中一台服务,多次访问http://localhost:8901/syaHello会出现如下图情况,



可以看出,当出现服务访问不通的情况,会返回对应的错误信息。

总结

本文简单实现了ribbon做为消费者的搭建过程,并假如了Hystrix熔断机制。

代码地址

gitHub地址


《Srping Cloud 2.X小白教程》目录


转载请注明出处,

  • 联系方式:4272231@163.com

spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)的更多相关文章

  1. spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  2. spring cloud 2.x版本 Eureka Client服务提供者教程

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1 创建eureka client 1.1 新建Srping boot工程:eureka-c ...

  3. spring cloud 2.x版本 Zuul路由网关教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  4. spring cloud 2.x版本 Gateway动态路由教程

    摘要 本文采用的Spring cloud为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的几篇Spring cloud Gateway文章的实现. 参考 Gatew ...

  5. spring cloud 2.x版本 Gateway路由网关教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  6. spring cloud 2.x版本 Config配置中心教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...

  7. spring cloud 2.x版本 Gateway自定义过滤器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  8. Spring Cloud官方文档中文版-服务发现:Eureka服务端

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR3/#spring-cloud-eureka-server 文中例子我做了一些 ...

  9. Spring Cloud官方文档中文版-服务发现:Eureka客户端

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...

随机推荐

  1. Kubernetes 系列(一):本地k8s集群搭建

    我们需要做以下工作: (1)安装VMware,运行CentOs系统,一个做master,一个做node. (2)安装K8s. (3)安装docker和部分镜像会需要访问外网,所以你需要做些网络方面的准 ...

  2. Oracle 存储过程判断语句正确写法和时间查询方法

    判断语句:if 条件 then   if  条件  then ************;   elsif  条件  then  ************;   elsif 条件  then ***** ...

  3. 记录使用echarts的graph类型绘制流程图全过程(一)-x,y位置的计算

    先说下本次案例业务需求,输入2个节点,获取数据后绘制出2个节点间的路径,之前使用的是网状图,但是网状图的效果不佳,需要转换成流程图的模式: 那么如何在不修改数据的情况下,实现类似效果尼? 看了下ech ...

  4. 列表 元祖 range

    1.列表 list 存放一些数据的容器 比如 衣柜 书包 作用:存储一些数据,数据量比较大 可以下标 可以切片 可以步长 和字符串的完全一样 lst = [1,2,3] print(lst) #[1, ...

  5. 最近学到的Git知识,大厂的Git机制还是很方便的

    本文首发于微信公众号:程序员乔戈里 转载请注明:https://blog.csdn.net/WantFlyDaCheng/article/details/102538508 一.两次的 git com ...

  6. logback颜色

    官网日志连接:https://logback.qos.ch/manual/layouts.html#coloring 举例 logging.pattern.console=%red(%d{yyyy-M ...

  7. 02-16 k近邻算法

    目录 k近邻算法 一.k近邻算法学习目标 二.k近邻算法引入 三.k近邻算法详解 3.1 k近邻算法三要素 3.1.1 k值的选择 3.1.2 最近邻算法 3.1.3 距离度量的方式 3.1.4 分类 ...

  8. ES6 —— entries(),keys()和values()

    ES6 提供三个新的方法 —— entries(),keys()和values() —— 用于遍历数组.它们都返回一个遍历器对象,可以用for...of循环进行遍历,唯一的区别是keys()是对键名的 ...

  9. Python3程序设计指南:01 过程型程序设计快速入门

    大家好,从本文开始将逐渐更新Python教程指南系列,为什么叫指南呢?因为本系列是参考<Python3程序设计指南>,也是作者的学习笔记,希望与读者共同学习. .py文件中的每个估计都是顺 ...

  10. Numpy数组操作

    """ Numpy 数组操作 修改数组形状 函数 描述 reshape 不改变数据的条件下修改形状 flat 数组元素迭代器 flatten 返回一份数组拷贝,对拷贝所做 ...