Spring Cloud之统一fallback接口

每个方法都配备一个fallback方法

不利于开发的

用类的方式

并且整个方法都是在同一个线程池里面的

 

主要对于client的修改:

 

 

 pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.toov5</groupId>
		<artifactId>parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>toov5-api-order-service-impl</artifactId>

	<dependencies>
		<dependency>
           <groupId>com.toov5</groupId>
			<artifactId>toov5-api-order-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		
     <dependency>
           <groupId>com.toov5</groupId>
			<artifactId>toov5-api-member-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		
		
	</dependencies>


</project>

 实现类

package com.toov5.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.toov5.api.entity.UserEntity;
import com.toov5.api.feign.MemberServiceFeign;
import com.toov5.api.service.IOrderService;
import com.toov5.base.BaseApiService;
import com.toov5.base.ResponseBase;

@RestController
public class OrderServiceImpl extends BaseApiService implements IOrderService {
    @Autowired
    private MemberServiceFeign memberServiceFeign;

    @RequestMapping("/orderToMmeber")
    public String orderToMember(String name) {
        UserEntity user = memberServiceFeign.getMember(name);
        return user == null ? "没有" : user.toString();
    }

    @RequestMapping("/orderToMemberUserInfo")
    public ResponseBase orderToMemberUserInfo() {
        return memberServiceFeign.getUserInfo();
    }

    @HystrixCommand(fallbackMethod = "orderToMemberUserInfoHystrixFallback")
    @RequestMapping("/orderToMemberUserInfoHystrix")
    public ResponseBase orderToMemberUserInfoHystrix() {
        System.out.println("orderToMemberUserInfoHystrix" + "线程池名称" + Thread.currentThread().getName());
        return memberServiceFeign.getUserInfo();
    }

    public ResponseBase orderToMemberUserInfoHystrixFallback() {
        return setResultSuccess("请稍后再试~");
    }

    @RequestMapping("/orderToMemberUserInfoHystrixdSecond")
    public ResponseBase orderToMemberUserInfoHystrix_demo02() {
        System.out.println("orderToMemberUserInfoHystrix" + "线程池名称" + Thread.currentThread().getName());
        return memberServiceFeign.getUserInfo();
    }

    @RequestMapping("/orderInfo")
    public ResponseBase orderInfo() {
        System.out.println("orderInfo" + "线程池名称" + Thread.currentThread().getName());
        return setResultSuccess();
    }

}

feign

package com.toov5.api.feign;

import org.springframework.cloud.openfeign.FeignClient;

import com.toov5.api.fallback.MemberServiceFallback;
import com.toov5.api.service.IMemberService;
@FeignClient(value="app-toov5-member",fallback = MemberServiceFallback.class)
public interface MemberServiceFeign extends IMemberService {
  
}
 

fallback类

package com.toov5.api.fallback;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import com.toov5.api.entity.UserEntity;
import com.toov5.api.feign.MemberServiceFeign;
import com.toov5.base.BaseApiService;
import com.toov5.base.ResponseBase;

@Component
@RequestMapping("fallback/")   //这个可以防止 容器中有与父类重复的 requestMapping!!!
public class MemberServiceFallback extends BaseApiService implements  MemberServiceFeign {
    
     @Override
    public ResponseBase getUserInfo() {
         return setResultError("来自类的提示:系统错误,请稍后重试!");
    }
     
    @Override
    public UserEntity getMember(String name) {
        // TODO Auto-generated method stub
        return null;
    }

}

启动类:

package com.toov5.api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication(scanBasePackages={"com.toov5.*"})
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class AppOrder {
   public static void main(String[] args) {
    SpringApplication.run(AppOrder.class, args);
}
}

 

 yml

###服务启动端口号
server:
  port: 8020
###服务名称(服务注册到eureka名称)  
spring:
    application:
        name: app-toov5-order
###服务注册到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka

           
###因为该应用为注册中心,不会注册自己
    register-with-eureka: true
###是否需要从eureka上获取注册信息
    fetch-registry: true

    ###设置feign客户端超时时间
ribbon:
###指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。
 ReadTimeout: 5000
###指的是建立连接后从服务器读取到可用资源所用的时间。 
 ConnectTimeout: 5000
  
  #spring cloud 默认开启ribbon 
  
##开启Hystrix断路器  
feign:
  hystrix:
    enabled: true

#### hystrix禁止服务超时时间
#hystrix:  
# command: 
#   default: 
#      execution:   
#       timeout: 
#        enabled: false
    


    

  启动后:

感谢:https://www.jb51.net/article/138758.htm 这篇文章的参考!

 对于报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.toov5.api.feign.MemberServiceFeign' method
public abstract com.toov5.base.ResponseBase com.toov5.api.service.IMemberService.getUserInfo()
to {[/getUserInfo]}: There is already 'memberServiceFallback' bean metho

 

的解决办法在 fallback的类上面加上 

@RequestMapping("fallback/")   //这个可以防止 容器中有与父类重复的 requestMapping!!!

 

posted @ 2018-11-15 23:41  toov5  阅读(3546)  评论(0编辑  收藏  举报