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. thinkPHP中怎么访问域名直接跳到后台登录页面

    问题: 我想只访问域名就跳到后台登录页面,怎么把地址栏里的路径隐藏掉 答案: 修改配置Common里的conf文件夹里,把默认模块改成“Admin”,默认控制器改成“login”系统默认的默认模块式h ...

  2. html学习之二(常用标签练习)

    <!DOCTYPE html><head> <meta charset="utf-8"> <title>锚点链接</title ...

  3. 1.web2

    听说聪明的人都能找到答案http://123.206.87.240:8002/web2/ 直接查看源码~~~

  4. springboot项目jar包运行

    springboot项目jar包运行 参考 Linux后台运行java的jar包 步骤 进入maven项目中,打包项目. mvn package -Dmaven.test.skip=true 运行ja ...

  5. CF915E Physical Education Lessons 珂朵莉树

    问题描述 CF915E LG-CF915E 题解 \(n \le 10^9\) 看上去非常唬人. 但是这种区间操作的题,珂朵莉树随便跑啊. \(\mathrm{Code}\) #include< ...

  6. OpenStack I18N

    OpenStack I18N 官方文档: https://docs.openstack.org/oslo.i18n/latest/user/usage.html https://docs.openst ...

  7. 【2019.8.7 慈溪模拟赛 T2】环上随机点(ran)(自然算法)

    简单声明 我是蒟蒻不会推式子... 所以我用的是乱搞做法... 大自然的选择 这里我用的乱搞做法被闪指导赐名为"自然算法",对于这种输入信息很少的概率题一般都很适用. 比如此题,对 ...

  8. -bash:vi:command not find 问题解决

    Linux命令行输入命令执行后报“bash:vi:command not found”. 这是由于系统PATH设置问题,PATH没有设置对,系统就无法找到精确命令了. 1.在命令行中输入:export ...

  9. Python之爬虫-校花网

    Python之爬虫-校花网 #!/usr/bin/env python # -*- coding:utf-8 -*- import re import requests # 拿到校花网主页的内容 re ...

  10. Windows Azure Virtual Machine (38) 跨租户迁移使用托管磁盘的Azure虚拟机

    <Windows Azure Platform 系列文章目录> 背景介绍: (1)我们建议使用Azure Manage Disk托管磁盘来创建Azure虚拟机 (2)使用托管磁盘的好处是, ...