一、创建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秒。