新建提供者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
启动测试
启动测试,两个方法都可以正常被访问。