Eureka服务消费者介绍

  Eureka服务消费者用于发现服务和消费服务,发现服务通过Eureka Client完成,消费服务通过Ribbon完成,以实现负载均衡。在实际项目中,一个服务往往同时是服务消费者与服务提供者,所以都需要注册到注册中心统一管理。同时,本文也将一同介绍Hystrix,服务容错保护熔断技术。以及ribbon负载均衡策略,重试机制,hystrix超时配置等内容。

1、配置pom.xml,注意此处引入的spring retry。网上一些教程未引入该包,导致访问故障节点时,重试机制不生效,直接返回Connection refused连接拒绝信息。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Consumer</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2、入口类初始化RestTemplate,交给Spring管理,使用@LoadBalanced注解,将其交由ribbon管理,做成拦截器,并实现负载均衡等策略。@EnableCircuitBreaker用于开启hystrix的熔断服务。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @EnableCircuitBreaker
@SpringBootApplication
public class ConsumerApplication { @Bean
@LoadBalanced
RestTemplate template()
{
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}

3、定义HystrixServer,具备ribbon负载均衡和hystrix的熔断功能的服务。将RestTemplate注入,使用template.getForObject(url,Object)来请求服务提供者,并将返回的bady转换为Object的类型。使用@HystrixCommand(fallbackMethod = "methodName")来注解方法,指定服务降级后执行的方法。

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class HystrixServer { @Autowired
RestTemplate template; @HystrixCommand(fallbackMethod = "back")
public String getHello()
{
return template.getForObject("http://client/hello", String.class);
} public String back()
{
return "client has some error!";
}
}

4、定义Controller。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class ConsumerContorller { @Autowired
HystrixServer server; @RequestMapping("/hello")
public String sayHello()
{
return server.getHello();
}
}

5、配置application.yml。

spring:
application:
name: consumer
cloud:
loadbalancer:
retry:
enabled: true
server:
port: 11120
eureka:
client:
service-url:
defaultZone: http://localhost:11110/eureka,http://localhost:11111/eureka ribbon:
ConnectTimeout: 250
ReadTimeout: 1000
OkToRetryOnAllOperations: true
MaxAutoRetriesNextServer: 2
MaxAutoRetries: 1
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000

配置说明

  1. spring.cloud.loadbalancer.retry.enabled:配置是否开启负载均衡的重试机制。2.0.2版本默认为开启状态。
  2. <clientname>.ribbon.ConnectTimeout:配置ribbon连接超时时间。<clientname>为服务实例名,指定了,只在当前服务实例请求下生效,也可以不指定,表示全局设置。
  3. <clientname>.ribbon.ReadTimeout:配置ribbon的请求超时时间。
  4. <clientname>.ribbon.OkToRetryOnAllOperations:true,对所有操作请求都进行重试。
  5. <clientname>.ribbon.MaxAutoRetriesNextServer:切换多少次实例进行重试机制。
  6. <clientname>.ribbon.MaxAutoRetries:对当前实例进行重试的次数。
  7. hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:定义熔断机制生效的超时时间。

  根据yml文件中的配置,此处当访问到故障的实例时,会再访问一次(MaxAutoRetries),当访问依旧失败时,会换一个实例访问,如果还不行,再换一个实例(MaxAutoRetriesNextServer),再不行则返回错误信息。

  

6、启动consumer后,访问http://localhost:11120/hello后,会看到轮询返回client1和client2的信息,负载均衡的轮询机制生效。当关掉client2后,在连接超时和重试机制下,一直访问返回client1的信息。当强制关掉client1和client2后,在重试依旧失败和熔断机制的保护作用下,返回服务降级后的client has some error信息。至此,具备负载均衡和熔断机制的消费端搭建完成。

Spring Cloud Netflix之Eureka服务消费者的更多相关文章

  1. SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用

    1.  Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...

  2. Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务

    上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...

  3. spring cloud (四、服务消费者demo_consumer)

    spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...

  4. Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  5. SpringCloud学习笔记(5)----Spring Cloud Netflix之Eureka的服务认证和集群

    1. Eureka服务认证 1. 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  6. 2.Spring Cloud初相识--------Eureka服务注册与消费

    前言: 1.Eureka介绍: Spring Cloud Eureka,使用Netflix Eureka来实现服务注册与发现,它既包含了服务端组件,也包含了客户端组件,并且服务端与客户端均采用Java ...

  7. Spring Cloud 入门教程 - Eureka服务注册与发现

    简介 在微服务中,服务注册与发现对管理各个微服务子系统起着关键作用.随着系统水平扩展的越来越多,系统拆分为微服务的数量也会相应增加,那么管理和获取这些微服务的URL就会变得十分棘手,如果我们每新加一个 ...

  8. Spring Cloud(Dalston.SR5)--Eureka 服务实例健康检查

    默认情况下,Eureka 客户端每隔 30 秒会发送一次心跳给服务器端,告知正常存活,但是,实际环境中有可能出现这种情况,客户端表面上可以正常发送心跳,但实际上服务是不可用的,例如,一个需要访问数据的 ...

  9. Spring Cloud(Dalston.SR5)--Eureka 服务消费

    服务被注册.发布到 Eureka 服务器后,需要有程序去发现他,并且进行调用,称为服务消费,一个服务可能会部署多个实例,调用过程可能涉及负载均衡.服务器查找等问题,这些问题 Netflix 项目已经帮 ...

随机推荐

  1. emacs require和provide

    Emacs的默认配置文件是.emacs,Emacs启动时会读取并执行.emacs中的LISP代码,用户使用.emacs达到自己的Emacs初始化配置目的. 不过单独使用.emacs有一个问题,因为时间 ...

  2. Qt在window下的环境变量PATH的配置

    Qt在window下的环境变量PATH的配置 路劲: C:\Qt\Qt5.6.0\5.6\mingw49_32\bin C:\Qt\Qt5.6.0\Tools\mingw492_32\bin 发布Qt ...

  3. 渗透测试学习 十、 MSsql注入下

    大纲:MySQL介绍及操作 MySQL注入原理 MySQL注入其他操作 一.MySQL介绍及操作 介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle ...

  4. ue4 FString 中文乱码问题

    使用FString出现乱码,最简单的情况,FString Str = "你好"; 这时候就会出现乱码,解决方法是改成这样 FString Str = TEXT("你好&q ...

  5. jacoco统计自动化代码覆盖率

    jacoco统计自动化代码覆盖率 1. 简介 1.1. 什么是Jacoco Jacoco是一个开源的代码覆盖率工具,可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以 ...

  6. Redis windows版本资源与安装

    这里提供一个windows版本的Redis百度云资源 链接: https://pan.baidu.com/s/19JY_d_J87n98OeAHK9qI4A 密码: d6dq 1,GitHub下载地址 ...

  7. CF1178D Prime Graph

    题目链接 题意 构造一张有\(n(3\le n\le 1000)\)个点的无向图(无重边和自环).满足: 边的总数为素数 所有点的度数均为素数 输出方案 solution 如果所有点的度数确定了.那么 ...

  8. 1130不允许连接到MySql server

    连接远程服务器mysql时,报错: 1130-host ... is not allowed to connect to this MySql server 这是因为默认只让localhost的主机连 ...

  9. Paper | Spatially Adaptive Computation Time for Residual Networks

    目录 摘要 故事 SACT机制 ACT机制 SACT机制 实验 发表在2017年CVPR. 摘要 在图像检测任务中,对于图像不同的区域,我们可以分配不同层数的网络予以处理. 本文就提出了一个基于Res ...

  10. 领域驱动设计(DDD)编码实践

    写在前面 Martin Fowler在<企业应用架构模式>一书中写道: I found this(business logic) a curious term because there ...