1. 程式人生 > >第四篇: 熔斷器(Ribbon+Feign)(Greenwich版本)

第四篇: 熔斷器(Ribbon+Feign)(Greenwich版本)

一、增加Ribbon的熔斷功能

1.1、改造service-ribbon的pom.xml 增加hystrix依賴後<dependencies>節點如下所示:

<dependencies>
        <!-- ribbon-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

        <!-- eureka discovery -->
        <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>
    </dependencies>

1.2 在啟動類ServiceRibbonApplication增加@EnableHystrix註解開啟Hystrix

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * 通過@EnableDiscoveryClient向服務中心註冊;並且向程式的ioc注入一個bean
 * @author xiaobu
 */
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrix
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }


    /***
     * @author xiaobu
     * @date 2018/11/6 11:32
     * @return org.springframework.web.client.RestTemplate
     * @descprition restTemplate;並通過@LoadBalanced註解表明這個restRemplate開啟負載均衡的功能。
     * @version 1.0
     */
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

1.3在ClientService上方法上增加@HystrixCommand註解表明該方法支援熔斷功能

package com.example.service;

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;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2018/11/6 11:34
 * @descrption
 */
@Service
public class ClientService {
    @Autowired
    RestTemplate restTemplate;

    /***
     * @author xiaobu
     * @date 2018/11/6 11:42
     * @param name 名字  @HystrixCommand 這個表明加上了熔斷器的功能
     * @return java.lang.String
     * @descprition  直接用的程式名替代了具體的url地址,
     * 在ribbon中它會根據服務名來選擇具體的服務例項,
     * 根據服務例項在請求的時候會用具體的url替換掉服務名
     * @version 1.0
     */
    @HystrixCommand(fallbackMethod ="error" )
    public String clientService(String name){
        return restTemplate.getForObject("http://eureka-client/test?name=" + name, String.class);
    }

    /**
     * @author xiaobu
     * @date 2018/11/7 11:27
     * @param name 名字
     * @return java.lang.String
     * @descprition  error要與  @HystrixCommand(fallbackMethod ="error" )的方法名要相對應
     * @version 1.0
     */
    public String error(String name){
        return "hi "+name+",this service is  unavailable";
    }

}

 8003服務則不可用則效果如下:

二、增加Feign的熔斷功能。

2.1 feign的熔斷功能預設是關閉的,所以在配置檔案中開啟。

eureka.client.service-url.defaultZone=http://localhost:8001/eureka/
spring.application.name=service-feign
server.port=8005
#開啟feign的熔斷功能
feign.hystrix.enabled=true

2.2 改造FeignService

package com.example.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2018/11/6 14:24
 * @description V1.0 定義個feign介面 @FeignClient("服務名") 來確定調哪個服務
 */
@FeignClient(value = "eureka-client",fallback = FeignHystrixServiceImpl.class)
public interface FeignService {
    /**
     * @author xiaobu
     * @date 2018/11/6 14:34
     * @param name 名字
     * @return java.lang.String
     * @descprition value為test則是呼叫 eureka-client的test的方法
     * RequestMapping(value="/test",method = RequestMethod.GET)與GetMapping(value="/test")等價
     * RequestParam.value() was empty on parameter 0 第一個引數不能為空
     * @version 1.0
     */

    //@RequestMapping(value="/test",method = RequestMethod.GET)
    @GetMapping(value="/test")
    String testFromClient(@RequestParam(value = "name") String name);
}

2.3增加實現類:

package com.example.service;

import org.springframework.stereotype.Component;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2018/11/7 11:34
 * @description V1.0
 */
@Component
public class FeignHystrixServiceImpl implements FeignService {
    @Override
    public String testFromClient(String name) {
        return "sorry "+name+",this service is unavailable";
    }
}

OK。