Hystrix案例

新建提供者cloud-provider-hystrix-payment8001模块:
引入POM文件

<!--eureka-client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
    <groupId>com.krisswen.cloud</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>${project.version}</version>
</dependency>
**编写配置文件** server: port: 8001 spring: application: name: cloud-payment-service #服务名称 eureka: client: # 注册进 Eureka 的服务中心 register-with-eureka: true # 检索 服务中心 的其它服务 fetch-registry: true service-url: # 设置与 Eureka Server 交互的地址 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ **编写启动类** @SpringBootApplication @DiscoveryClient public class HystrixPaymentApplication { public static void main(String[] args) { SpringApplication.run(HystrixPaymentApplication.class,args); } } **编写service** @Service public class PaymentService {
/**
 * 可以正常访问的方法
 * @param id
 * @return
 */
public String paymentInfo_Ok(Integer id){
    return "线程池:" + Thread.currentThread().getName() + "  ,paymentInfo_OK,id:" + id;
}

/**
 超时访问的方法
 */
public String paymentInfo_Timeout(Integer id){
    int interTime = 3;
    try{
        TimeUnit.SECONDS.sleep(interTime);
    }catch (Exception e){
        e.printStackTrace();
    }
    return "线程池:" + Thread.currentThread().getName() + "  ,paymentInfo_Timeout,id:" + id + "耗时" + interTime + "秒钟";
}

}
编写controller
@RestController
@Slf4j
public class PaymentController {

@Autowired
PaymentService paymentService;

@Value("${server.port}")
private String serverPort;

@GetMapping("/payment/hystrix/{id}")
public String paymentInfo_OK(@PathVariable("id")Integer id){
    log.info("paymentInfo_OKKKKOKKK");
    return paymentService.paymentInfo_Ok(id);
}

@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_Timeout(@PathVariable("id")Integer id){
    log.info("paymentInfo_timeout");
    return paymentService.paymentInfo_Timeout(id);
}

}
// localhost:8001/payment/hystrix/1 localhost:8001/payment/hystrix/timeout/1
启动测试
启动测试,两个方法都可以正常被访问。
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值