springcloud中Feign服务器调用-Feign与熔断Hystrix配置讲解

1>.Feign的作用:

减少负载(ribbon)与熔断(Hystrix)的配置,用于管理调用服务器。

代替了RestTemplate。

2>.配置:

1.引入pom.xml坐标


            org.springframework.cloud
            spring-cloud-starter-openfeign

2.在启动类添加注解@EnableFeignClients开启Feign服务。

3.创建一个接口,接口上面添加@FeignClient("SERVICE-PROVIDER")参数为服务名。具体方法与controller的方法相似。*注意改接口对一个的方法是服务方提供的接口方法。

package alian.Interface;

import alian.domain.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
@FeignClient("SERVICE-PROVIDER")
public interface UserControllerFeign{

    @GetMapping("user/{id}")
    public User findUserbyId(@PathVariable("id") int id);
}

对应的controller的接口方法。

package alian.controller;

import alian.Interface.UserControllerFeign;
import alian.domain.User;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework. web.client.RestTemplate;
import sun.security.jca.GetInstance;

import java.util.List;

@Controller
@RequestMapping("consumer/user")
public class ComsumorController {


    @Autowired
    private UserControllerFeign userControllerFeign;
    @GetMapping("{id}")
    @ResponseBody

    public String findUserbyId(@PathVariable("id") int id)
    {
        /*
        *
        *
        *
        * 4.通过Feign整合
        *
        *
        * */
        return userControllerFeign.findUserbyId(id).toString();
    }
}

3>.熔断的整合(Hystrix)

步骤:

1.开启熔断(Feign默认关闭熔断)在application.yml修改开启配置

feign:
  hystrix:
    enabled: true

2.实例化上述的接口并添加到IOC容器中去(添加@Component)。

package alian.Interface;

import alian.domain.User;
import org.springframework.stereotype.Component;

@Component
public class UserControllerFeignImpl implements UserControllerFeign {
    @Override
    public User findUserbyId(int id) {
        User user = new User();
        user.setName("服务器繁忙-请稍后重试");
        return user;
    }
}

3.在接口上面的注解改成如下所示:

@FeignClient(value = "SERVICE-PROVIDER",fallback = UserControllerFeignImpl.class)

例子如下:

package alian.Interface;

import alian.domain.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
@FeignClient(value = "SERVICE-PROVIDER",fallback = UserControllerFeignImpl.class)
public interface UserControllerFeign{

    @GetMapping("user/{id}")
    public User findUserbyId(@PathVariable("id") int id);
}

 

你可能感兴趣的:(springcloud,springboot)