本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 java
本文基于前两篇文章eureka-server和eureka-client的实现。 参考git
<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>
复制代码
application.ymlgithub
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/
复制代码
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服务发现相关配置web
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:实现负载均衡,默认轮询。spring
Ribbon自带的负载规则浏览器
- RoundRobinRule:系统默认的规则,经过简单的轮询服务列表来选择服务器,其余的规则在不少状况下,仍然使用RoundRobinRule。
- AvailablilityFilteringRule:该各类会忽略如下服务器:
没法链接的服务器:在默认状况下,若是3次链接失败,该服务器将会被置为“短路”的状态,该状态将持续30秒,若是再次链接失败,“短路”状态的持续时间将会以几何级增长。能够经过修改niws.loadbalance..connerctionFailureCountThreshold属性来配置链接失败的次数。
并发数太高的服务器:若是链接到该服务器的并发数太高,也会被这个规则忽略,能够经过修改.ribbon.ActiveConnectionLimit属性来设定最高并发数。服务器
- WeightedResponseTimeRule:为每一个服务器赋予一个权重值,服务器的响应时间越长,该权重值就越少,这个规则会随机选择服务器,这个权重值有能够能会决定服务器的选择。
- ZoneAvoidanceRule:该规则以区域、可用服务器为基础,进行服务器选择。使用Zone对服务器进行分类,能够理解为机架或者机房。
- BestAvailiableRule:忽略“短路”的服务器,并选择并发数较低的服务器。
- RandomRule:随机选择可用服务器。
- RetryRule:含有重试的选择逻辑,若是使用RoundRobinRule。
application.yml增长配置:网络
#RoundRobinRule:系统默认的规则,经过简单的轮询服务列表来选择服务器,其余的规则在不少状况下,仍然使用RoundRobinRule
eureka-client: #对应的服务client的name
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
复制代码
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并发
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;
}
}
复制代码
前题保证eureka-server和eureka-client已经正常启动。而后启动eureka-ribbon服务。 在浏览器输入http://localhost:8901/syaHello,以下图所示:app
屡次刷新后能够看到浏览器显示的是结果中端口是变化的。
至此,一个简单的单点Ribbon服务消费者就搭建完成。
场景:假如在生产环境中,访问量很大的状况下,那么就会产生不少请求阻塞的状况,而后服务器的内存消耗就会陡增,严重状况下会致使系统的崩溃,也就是常见的雪崩。为了不这种状况,熔断保护机制就迎刃而生。在访问不通的状况下,要及时做出响应,而不是等待超时。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
复制代码
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);
}
}
复制代码
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熔断机制。
转载请注明出处,