SpringCloud-Sentinel和Feign的整合(Day15)

整合Sentinel和Feign

pom


<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-openfeignartifactId>
dependency>

yml

# 激活Sentinel对Feign的支持
feign:
  sentinel:
    enabled: true

Service

package com.gcl.springcloud.service;

import com.gcl.springcloud.entities.CommonResult;
import com.gcl.springcloud.entities.Payment;
import com.gcl.springcloud.service.fallback.PaymentFallbackService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
public interface PaymentService {

    @GetMapping(value = "/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);

}
package com.gcl.springcloud.service.fallback;

import com.gcl.springcloud.entities.CommonResult;
import com.gcl.springcloud.entities.Payment;
import com.gcl.springcloud.service.PaymentService;

public class PaymentFallbackService implements PaymentService {
    @Override
    public CommonResult<Payment> paymentSQL(Long id) {
        return new CommonResult<>(44444,"服务降级返回,---PaymentFallbackService",new Payment(id,"errorSerial"));
    }
}

Controller

@Resource
private PaymentService paymentService;

@GetMapping(value = "/consumer/paymentSQL/{id}")
public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id)
{
    return paymentService.paymentSQL(id);
}

主启动

package com.gcl.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderNacosMain9084 {
    public static void main(String[] args) {
        SpringApplication.run( OrderNacosMain9084.class, args );
    }
}

运行

正常调用

SpringCloud-Sentinel和Feign的整合(Day15)_第1张图片

关闭所有支付模块后调用

SpringCloud-Sentinel和Feign的整合(Day15)_第2张图片
SpringCloud-Sentinel和Feign的整合(Day15)_第3张图片

你可能感兴趣的:(SpringCloud,spring,cloud,spring,boot,微服务)