springcloud以类降级项目架构memberService中代码orderService中代码ordermember

2020-12-07 14:24:59 浏览数 (1)

项目架构

memberService中代码

代码语言:javascript复制
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中代码

代码语言:javascript复制
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

项目结构图

fallBack中代码

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

代码语言:javascript复制
@Component
public class MemberFallBack implements MemberFeign {

    @Override
    public String testMember() {

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

        return "服务降级";
    }

MemberFeign中的代码

代码语言:javascript复制
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

代码语言:javascript复制
@RestController
public class OrderService  {
    @Autowired
    private MemberFeign memberFeign;
    @RequestMapping("test")
    public String test(){
            return memberFeign.testMember();
        }

OrderStar

代码语言:javascript复制
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配置文件

代码语言:javascript复制
###服务启动端口号
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

MemberServiceIpml

代码语言:javascript复制
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

代码语言:javascript复制
@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

代码语言:javascript复制
###服务启动端口号
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

0 人点赞