项目架构

springcloud以类降级-LMLPHP

memberService中代码

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public interface MemberService {
    @RequestMapping(value = "testMember",method = RequestMethod.GET)
     public String testMember();
    @RequestMapping(value = "test",method = RequestMethod.GET)
    public String test();

orderService中代码

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public interface OrderService {

    @RequestMapping(value = "testOrder",method = RequestMethod.GET)
    public String testOrder();
}

order

    项目结构图

springcloud以类降级-LMLPHP

fallBack中代码

    注意:该类实现的接口是feign中的接口


@Component
public class MemberFallBack implements MemberFeign {

    @Override
    public String testMember() {

        return "服务降级";
    }
    @Override
    public String test(){

        return "服务降级";
    }

MemberFeign中的代码

import com.bdqn.fallBack.MemberFallBack;
import com.bdqn.service.MemberService;
import org.springframework.cloud.openfeign.FeignClient;

@FeignClient(name = "member",fallback = MemberFallBack.class)
public interface MemberFeign extends MemberService {
}

orderService


@RestController
public class OrderService  {
    @Autowired
    private MemberFeign memberFeign;
    @RequestMapping("test")
    public String test(){
            return memberFeign.testMember();
        }

OrderStar


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

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

application.yml配置文件

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


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

###开启Hystrix断路器
feign:
  hystrix:
    enabled: true


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

member

springcloud以类降级-LMLPHP

    

MemberServiceIpml


import com.bdqn.service.MemberService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberServiceIpml implements MemberService {
    @RequestMapping(value = "testMember",method = RequestMethod.GET)
    public String testMember() {
        try {
            Thread.sleep(1500);
        }catch (Exception e){}
        return "会员接口";
    }

    @Override
    public String test() {
            try {
            Thread.sleep(1500);
        }catch (Exception e){}
        return "会员接口";
    }
}

MemberStar


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

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

application.yml

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


    ###因为该应用为注册中心,不会注册自己
    register-with-eureka: true
    ###是否需要从eureka上获取注册信息
    fetch-registry: true
###开启Hystrix断路器
feign:
  hystrix:
    enabled: true
05-30 16:34