springCloud Finchley microservice architecture from entry to mastery [eight] circuit breaker Hystrix (feign)

I. Introduction

In the previous section, I wrote an example of ribbon integrating hystrix. This section is ready to implement feign. Because it is relatively simple, only the key code is written. See the github source code for details.

Second, the code implementation

Create a fallback package to store feign's fallback processing class. The package structure is as follows:

write picture description here
UserFeignApi calls the class for the entry

package com.mayi.springcloud.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mayi.springcloud.client.UserFeignClient;

@RestController
public class UserFeignApi {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/listUsersByFeign")
    public String ListUsers(){
        String users = this.userFeignClient.listUsers();
        return users;
    }

}

UserFeignClient is the interface that implements feign

package com.mayi.springcloud.client;

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

import com.mayi.springcloud.fallback.UserFallback;

@FeignClient(name="service-user", fallback=UserFallback.class)
public interface UserFeignClient {

    @GetMapping("/listUsers")
    public String listUsers();

}

UserFallback is a node failure handling class

package com.mayi.springcloud.fallback;

import org.springframework.stereotype.Component;

import com.mayi.springcloud.client.UserFeignClient;

@Component
public class UserFallback implements UserFeignClient{

    @Override
    public String listUsers() {
        // TODO Auto-generated method stub
        return "服务调用失败";
    }

}

service-feign-hystrix-dev.yml in the configuration center

server:
  port: 1001
feign: 
  hystrix: 
    enabled: true

3. Test

Start the following services in sequence

write picture description here

Visit http://localhost:1001/listUsersByFeign , alternately

write picture description here

Manually stop one of the service-user services, when the load is balanced to the node, it will go to the custom fallback class

write picture description here

After a few seconds, the faulty node will be removed and will not be accessed by other callers.

Source address: https://github.com/tianyana/springcloud/tree/master/bussnessservice-user-client-feign-hystrix

Welcome to join the JAVA architect QQ group (initial construction): 618578034

write picture description here

From 2018-5.1 to 2019.5.1, the above public account will publish a complete architecture article according to the route of JAVA senior software architect practical training. The difficulty is from shallow to deep. Personnel learning of primary architecture development (starting after springcloud is updated)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326444457&siteId=291194637