springcloud微服务(十六) - Hystrix支付微服务构建

一、创建module-cloud-provider-hystrix-payment8002

二、修改pom.xml

<dependencies>
        <dependency>
            <groupId>otc.orientsec</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </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>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
<!--虽然hystrix一般配置在客户端,服务端也可以 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

三、修改application.yml

server:
  port: 8002

spring:
  application:
    name: cloud-payment-hystrix-service

eureka:
  client:
    #表示是否将自己注册进eurekaServer,默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true,才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

  instance:
    instance-id: payment-hystrix-8002
    prefer-ip-address: true

四、主启动类

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

五、业务类

5.1 service


@Service
public class HystrixPaymentService {

    public String paymentInfo_ok(Integer id)
    {
        return "线程池:"+Thread.currentThread().getName()+"  paymentInfo_ok,id="+id+"\t"+"成功";
    }

    //模拟业务流程长,耗时长
    public String paymentInfo_timeout(Integer id)  {
        int time=5;
        try {
            TimeUnit.SECONDS.sleep(time);
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        return "线程池:"+Thread.currentThread().getName()+"  paymentInfo_timeout,id="+id+"\t"+"超时";
    }
}

5.2 controller

@RestController
public class HystrixPaymentController {
    @Autowired
    private HystrixPaymentService hystrixPaymentService;

    @GetMapping("/payment/info/ok/{id}")
    public String paymentInfo_ok(@PathVariable("id") Integer id)
    {
        return hystrixPaymentService.paymentInfo_ok(id);
    }

    //模拟业务流程长,耗时长
    @GetMapping("/payment/info/timeout/{id}")
    public String paymentInfo_timeout(@PathVariable("id") Integer id)  {
        return hystrixPaymentService.paymentInfo_timeout(id);
    }
}

六、测试

两个服务都能正常应答,但timeout应答耗时5秒。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值