spring cloud eureka 服务之间调用_SpringCloud微服务架构开发实战:实现微服务熔断机制

实现微服务的熔断机制

我们在上节已经基本了解了如何将Hystrix 集成进应用。我们也通过一个简单的例子,知道了如何通过Hystrix技术实现自己的断路器。总的来说,使用Hystrix是非常简单的。

本节我们将基于Hystrix技术来改造天气预报系统,使我们的服务在调用核心数据服务时,能够启用熔断机制,从而保护应用。

我们将要修改之前的天气预报微服务msa-weather- report- eureka feign-gateway,由于该服务分别依赖了天气数据API微服务msa-weather-data -eureka及城市数据API微服务msa-weather-city-eure-ka,所以,在调用这两个服务过程中,假如调用失败,就启用断路器。

新的天气预报微服务命名为msa-weather-eport-eureka-feign-gateway-hytrix。

更改配置

要使用Hystrix,最简单的方式莫过于添加Hystrix依赖。

dependencies {//添加Spring Cloud Starter Netflix Hystrix依赖compile ('org. springframework. cloud: spring-cloud- starter-netflix-hystrix')}

使用Hystrix

要启用Hystrix,最简单的方式就是在应用的根目录的Application类上添加org.springframe-work.cloud.client.circuitbreaker. EnableCircuitBreaker注解。

package com. waylau. spring. cloud. weather;import org. springframework.boot . SpringAppl ication;import org. springf r amework . boot . autoconfigure . Spr ingBootApplication;import org. spr ing f ramework. cloud. client. circuitbreaker . Enable CircuitBreaker;import org. springframework.cloud.client. discovery . EnableDiscoveryClient;import org. springframework.cloud. netflix. feign . EnableFeignClients;/**主应用程序.@since 1.0.0 2017年11月12日* @author Way Lau*/@Spr ingBootApplication@EnableDiscoveryClient@EnableFeignClients@EnableCircuitBreakerpublic class Application {public static void main(String[] args) {SpringAppl ication. run (Application.class, args) ;}}

实现断路器

Feign内建支持了对于Hystrix回调函数的支持。我们只需要在@FeignClient注解的fllback 中声明要回调的类。

package com.waylau. spring.cloud. weather .service;import java.util.List;import org . springf ramework. cloud. netflix. feign. FeignClient;import org. springframework. web .bind. annotation. GetMapping;import org. springf ramework. web .bind. annotation. PathVariable;import com.waylau. spring.cloud.weather.vo.city;import com. waylau. spring.cloud.weather .vo.WeatherResponse;/**★访问数据的客户端.* @since 1.0.0 2017年11月6日Cauthor Way Lau*/@FeignClient (name= "msa-weather-eureka-client-zuul", fallback=DataClientFallback.class)public interface DataClient {/**获取城市列表* @returnathrows Exception*/@GetMapping("/city/cities")List listCity() throws Exception;*根据城市ID查询天气数据@param cityId .★@return@GetMapping ("/data/weather/cityId/ {cityId}")WeatherResponse getDataByCityId (@PathVariable("cityId") StringcityId) ;}

在上述代码中,回调指向了DataClientFallback 类,该类是一个 Spring bean,实现了DataClient接口的方法。DataClientFallback 中的方法,即为断路器需要返回的执行方法。

package com. waylau. spring. cloud . weather . service;import java.util.ArrayList;import java.util.List;import org.springframework. stereotype. Component;import com. waylau.spring.cloud. weather. vo.City;import com. waylau. spring.cloud . weather . vo. WeatherResponse;/**DataClient Fallback.@since 1.0.0 2017年11月13日@author Way Lau@Componentpublic class DataClientFallback implements DataClient {@Overridepublic List listCity() throws Exception {List cityList = null;cityList = new ArrayList<>() ;City city = new City() ;city .setCityId ("101280601");city. setCityName ("深圳") ;cityList.add(city) ;city = new City();city. setCityId("101280301") ;city. setCityName ("惠州");cityList.add(city) ;return cityList;}@Overridepublic WeatherResponse getDataByCityId(String cityId) {return new WeatherResponse () ;}}

其中:

●listCity方法:在调用城市数据API微服务时需要实现断路器。在城市数据API微服务失败时,,我们就响应默认的城市列表给客户端;

●getDataByCityld方法:在调用天气数据API微服务时需要实现断路器。在调用天气数据API微服务失败时,我们就响应默认的null给客户端。

修改report.html页面

...

空气质量指数:

当前温度:

温馨提示:

p class="card-text" th:text="${ forecast.date}">周五p class="card-text" th: text="$ { forecast. type}">晴转多云

高温28C

P class="card-text" th:text="${ forecast.low}">低温21CKp class="card-text" th:text="$ {forecast. fengxiang}">无持续风向微风

你可能感兴趣的:(spring,cloud,eureka,服务之间调用,spring,cloud,实现pdf,spring,cloud微服务,spring,cloud微服务实战,pdf)