Version: Next
Hystrix 支付模块构建
预备工作
Eureka Server 7001
恢复单机版,改 YAML 实现
cloud-provider-hystrix-payment8001
pom
- 核心实际上就是
hystrix
依赖
pom.xml
<dependencies>
<dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>org.example</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>
<scope>test</scope>
</dependency>
<!-- hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
YAML
- 端口号
- 应用名
- 把自己放到哪个注册中心
application.yaml
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-payment
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/ # 配置在本机器 host 文件里
主启动
com.bsx.springcloud.PaymentHystrixMain8001
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}
业务类
Service 接口与实现类
- 定义两个方法
- 正常方法
- 会产生问题的方法
接口
public interface PaymentService {
String paymentInfo_OK(Integer id);
String paymentInfo_TimeOut(Integer id);
}
实现类
@Service
public class PaymentServiceImpl implements PaymentService {
@Override
public String paymentInfo_OK(Integer id) {
return "线程池:" + Thread.currentThread().getName() +
"payment_OK, id:" + id + "\tO(∩_∩)O哈哈~";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
int timeNum = 3;
// 强行睡3秒钟
try {
TimeUnit.SECONDS.sleep(timeNum);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "线程池:" + Thread.currentThread().getName() +
"payment_TimeOut, id:" + id + "\tO(∩_∩)O哈哈~ 耗时 " + timeNum + " 秒钟";
}
}
Controller
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@Resource
private PaymentService paymentService;
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id) {
return paymentService.paymentInfo_OK(id);
}
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
return paymentService.paymentInfo_TimeOut(id);
}
}
测试
启动
Eureka Server 7001
、启动cloud-provider-hystrix-payment8001
访问
localhost:8001/payment/hystrix/ok/2
线程池:http-nio-8001-exec-1payment_OK, id:2 O(∩_∩)O哈哈~访问
localhost:8001/payment/hystrix/timeout/2
线程池:http-nio-8001-exec-2payment_TimeOut, id:2 O(∩_∩)O哈哈~ 耗时 3 秒钟