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
配置说明
- spring.cloud.loadbalancer.retry.enabled:配置是否开启负载均衡的重试机制。2.0.2版本默认为开启状态。
- <clientname>.ribbon.ConnectTimeout:配置ribbon连接超时时间。<clientname>为服务实例名,指定了,只在当前服务实例请求下生效,也可以不指定,表示全局设置。
- <clientname>.ribbon.ReadTimeout:配置ribbon的请求超时时间。
- <clientname>.ribbon.OkToRetryOnAllOperations:true,对所有操作请求都进行重试。
- <clientname>.ribbon.MaxAutoRetriesNextServer:切换多少次实例进行重试机制。
- <clientname>.ribbon.MaxAutoRetries:对当前实例进行重试的次数。
- 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信息。至此,具备负载均衡和熔断机制的消费端搭建完成。