美文网首页
java B2B2C springmvc mybatis电子商务

java B2B2C springmvc mybatis电子商务

作者: IT小跑兵 | 来源:发表于2019-02-15 14:22 被阅读1次

    1、 【microcloud-provider-dept-hystrix-8001】修改 pom.xml 配置文件,追加 Hystrix 配置类:需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码一零三八七七四六二六

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

    2、 【microcloud-provider-dept-hystrix-8001】修改 DeptRest 程序

    package cn.study.microcloud.rest;
    
    import javax.annotation.Resource;
    
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    
    import cn.study.microcloud.service.IDeptService;
    import cn.study.vo.Dept;
    
    @RestController
    public class DeptRest {
        @Resource
        private IDeptService deptService;
        @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
        @HystrixCommand(fallbackMethod="getFallback")    // 如果当前调用的get()方法出现了错误,则执行fallback
        public Object get(@PathVariable("id") long id) {
            Dept vo = this.deptService.get(id) ;    // 接收数据库的查询结果
            if (vo == null) {    // 数据不存在,假设让它抛出个错误
                throw new RuntimeException("部门信息不存在!") ;
            }
            return vo ;
        }
        public Object getFallback(@PathVariable("id") long id) {    // 此时方法的参数 与get()一致
            Dept vo = new Dept() ;
            vo.setDeptno(999999L);
            vo.setDname("【ERROR】Microcloud-Dept-Hystrix");    // 错误的提示
            vo.setLoc("DEPT-Provider");
            return vo ;
        }
        
        
    }
    

    一旦 get()方法上抛出了错误的信息,那么就认为该服务有问题,会默认使用“@HystrixCommand”注解之中配置好的 fallbackMethod 调用类中的指定方法,返回相应数据。

    3、 【microcloud-provider-dept-hystrix-8001】在主类之中启动熔断处理

    package cn.study.microcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    @SpringBootApplication
    @EnableEurekaClient
    @EnableCircuitBreaker
    @EnableDiscoveryClient
    public class Dept_8001_StartSpringCloudApplication {
        public static void main(String[] args) {
            SpringApplication.run(Dept_8001_StartSpringCloudApplication.class, args);
        }
    }
    

    现在的处理情况是:服务器出现了错误(但并不表示提供方关闭),那么此时会调用指定方法的 fallback 处理。java B2B2C springmvc mybatis电子商务平台源码

    相关文章

      网友评论

          本文标题:java B2B2C springmvc mybatis电子商务

          本文链接:https://www.haomeiwen.com/subject/rogpeqtx.html