SpringCloud-Hystrix服务降级

服务降级处理 就服务高并发的时候来关闭一些服务来应对 某一个 服务 在 之后在恢复回来
降级处理是在客户端实现的,与服务端没有关系 服务降级和spring的Aop 切面 里的通知 类似

1)

UserServiceFeign这个是公共的 消费者调用的 ,实现FallbackFactory的类下面UserServiceFallback会用到

pom 依赖

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

主启动类
添加@EnableHystrix 开启熔断注解

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

}

这个** @FeignClient(value = “MICROSERVICECLOUD-DEPT”,fallbackFactory =UserServiceFallback.class) **这个是你的微服务名 加上你的 UserServiceFallback的类 当你的服务宕机 或者异常来展示提示是信息

package com.xxx.service;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xxx.user.User;


// 全部信息
 @FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory =UserServiceFallback.class)
public interface UserServiceFeign {

	
	@RequestMapping(value = "/list/userlist",method = RequestMethod.GET)
	public List<User> userlist();
	
	
	
	@RequestMapping(value="user/getId/{userId}",method=RequestMethod.GET)
	public User getId(@PathVariable("userId")Integer userId);
	
}

要建立一个 接口 实现FallbackFactory的类
注意这个注解@Component 必须添加

UserServiceFallback

package com.xxx.service;

import java.util.List;

import org.apache.commons.collections.Factory;
import org.springframework.stereotype.Component;

import com.xxx.user.User;

import feign.hystrix.FallbackFactory;

@Component   //这个
public class UserServiceFallback implements FallbackFactory<UserServiceFeign> {

	@Override
	public UserServiceFeign create(Throwable arg0) {
		// TODO Auto-generated method stub
		return new UserServiceFeign() {
			
			@Override
			public List<User> userlist() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public User getId(Integer userId) {
				// TODO Auto-generated method stub
				//返回提示信息
				return  new User(00,"该"+userId+"没有对应的信息,该服务已关闭 服务降级",0,0);
			}
		};
			
	
	}

}

下面就是消费者 的 feign.yml 添加

feign:
hystrix:
enabled: true

server:
  port: 8084
 

feign: 
  hystrix: 
    enabled: true
  
  
eureka: 
  client: 
    register-with-eureka: false 
    service-url: 
       defaultZone: http://euerka7001.com:7001/eureka/,http://euerka7002.com:7002/eureka/,http://euerka7003.com:7003/eureka/     
 
  
    
                    

下面 就是 启动项目
eureka 注册中(7001,7002,7003)提供者(8081)消费者(microservicecloud-consumer-dept-feign)一共无五个项目
在这里插入图片描述
打开页面访问一下
正常 的访问 是可以的

在这里插入图片描述
接下来 我 把我的提供者关闭 在访问一遍
会发现 我在UserServiceFallback 类定义的提示信息 展示出来了
在这里插入图片描述
注解的使用:
单个方法使用 添加

@HystrixCommand(fallbackMethod = "tesy",commandProperties = {
               @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")
       }
       )

注解

  @GetMapping(value = "getALL/{id}")
       @HystrixCommand(fallbackMethod = "tesy",commandProperties = {
               @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")
       }
       )
        public String  getAll(@PathVariable("id") Long id){
           try {
               TimeUnit.SECONDS.sleep(5);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }

           return  "当前调用的是"+serverPort+"id是:"+id;

        }

全局配置服务降级
通过@DefaultProperties(defaultFallback=“返回的可控方法名”)注解完成

@DefaultProperties(defaultFallback = "test")

在有走服务降级的方法 添加 @HystrixCommand注解

@RestController
@DefaultProperties(defaultFallback = "test")
public class HystrixController {

    @Autowired
    private HystrixService hystrixService;




    @GetMapping(value = "select/{id}")
    public  String  select(@PathVariable("id") Long id){
        return  hystrixService.select(id);
    }


    @GetMapping(value = "getALL/{id}")
    @HystrixCommand
    public String  getAll(@PathVariable("id")Long id){

        return  hystrixService.getAll(id);

    }



    public String test(){

        return "不是我的问题哎,可能是8084挂掉了";
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

  T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值