SpringCloud-Hystrix服务降级

x33g5p2x  于2021-12-18 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(235)

服务降级处理 就服务高并发的时候来关闭一些服务来应对 某一个 服务 在 之后在恢复回来
降级处理是在客户端实现的,与服务端没有关系 服务降级和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挂掉了";
    }

}

相关文章