SpringCloud06

feign使用
快速入门
在这里插入图片描述

package cn.itcast.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//@SpringBootApplication
//@EnableDiscoveryClient
//@EnableCircuitBreaker //开启熔断
@SpringCloudApplication //组合注解,相当于@SpringBootApplication,@EnableDiscoveryClient,@EnableCircuitBreaker
@EnableFeignClients //启用feign组件
public class ItcastServiceConsumerApplication {

//    @Bean
//    @LoadBalanced
//    public RestTemplate restTemplate(){
//        return new RestTemplate();
//    }

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

}

package cn.itcast.service.client;

import cn.itcast.service.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@FeignClient("service-provide") //声明一个接口是一个feign接口,指定服务id
public interface UserClient {
    @GetMapping("user/{id}")
    public User qusetyUserById(@RequestParam("id")Long id);
}

package cn.itcast.service.controller;

import cn.itcast.service.client.UserClient;
import cn.itcast.service.pojo.User;
import javafx.beans.DefaultProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Controller
@RequestMapping("consumer/user")
//@DefaultProperties(defaultFallback = "qusetyUserByIdFallback")
public class UserController {
    @Autowired
    private UserClient userClient;
//    @Autowired
//    private RestTemplate restTemplate;

//    @Autowired
//    private DiscoveryClient discoveryClient; //包含了拉取的所有服务信息

    @GetMapping
    @ResponseBody
    @HystrixCommand
    public String qusetyUserById(@RequestParam("id")Long id){
//        if (id == 1){
//            throw new RuntimeException();
//        }
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
//        ServiceInstance instance = instances.get(0);
        return this.userClient.qusetyUserById(id).toString();
    }

    public String qusetyUserByIdFallback(Long id){
        return "服务正忙,稍后再试";
    }
}

熔断
在这里插入图片描述

server:
  port: 80
spring:
  application:
    name: service-consumer
  eureka:
    client:
      service-url:
        defaultZone: http://localhost:10086/eureka
      fetch-registry: true #拉取服务列表,缓存到本地
      registry-fetch-interval-seconds: 5 #拉取列表的间隔时间
service-provider: #服务提供方的服务id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #调用随机算法
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMillisecond: 6000 # 设置hystrix的超时时间为6000ms
feign:
  hystrix:
    enabled: true # 开启Feign的熔断功能
package cn.itcast.service.client;

import cn.itcast.service.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

@Component
public class UserClientFallback implements UserClient {
    @Override
    public User qusetyUserById(Long id){
        User user = new User();
        user.setUserName("服务器正忙,请稍后再试");
        return user;
    }
}

package cn.itcast.service.client;

import cn.itcast.service.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@FeignClient("service-provide", fallback = UserFeignClientFallback.class)
public interface UserClient {
    @GetMapping("user/{id}")
    public User qusetyUserById(@RequestParam("id")Long id);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值