前言:在上一章节中我们简单的介绍了关于Hystrix的相关知识点,而本节内容则是关于如果去使用Hystrix构建一个支付微服务。
1、创建一个服务提供者模块,命名为cloud-provider-hystrix-payment8001
(1)在父工程下新建模块
(2)选择模块的项目类型为Maven并选择模块要使用的JDK版本
(3)填写子模块的名称,然后点完成即可完成创建
效果图:
(4)修改cloud-provider-hystrix-payment8001子模块的pom.xml文件,然后reolad一下,下载依赖
例:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud01</artifactId>
<groupId>com.ken.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-hystrix-payment8001</artifactId>
<dependencies>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</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>
<!--lombok插件-->
<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>
<!--引入自己定义的api调用包,可以使用Payment模块的entity-->
<dependency>
<groupId>com.ken.springcloud</groupId>
<artifactId>api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--Eureka Clinet-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2、为子模块添加名为application的yml配置文件(注:yml是官方推荐的配置文件格式,最好使用yml文件而不是properties文件)
效果图:
3、修改application.yml文件配置
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-payment
eureka:
client:
#表示是否将自己注册进Eureka Server里,默认为true
register-with-eureka: true
#是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
#集群
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002
defaultZone: http://eureka7001.com:7001/eureka
4、为cloud-provider-hystrix-payment8001子模块新建一个主启动类,类名输入com.ken.springcloud.PaymentHystrixMain8001,然后创建即可
效果图:
5、编写PaymentHystrixMain8001启动类
package com.ken.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//使用Feign,激活并开启
@EnableFeignClients
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}
6、在com.ken.springcloud包下新建一个名为service的包
效果图:
7、在service包下新建一个名为PaymentService的接口类
效果图:
8、编写PaymentService接口类
package com.ken.springcloud.service;
public interface PaymentService {
String paymentInfoOK(Integer id);
String paymentInfoTimeOut(Integer id);
}
9、在com.ken.springcloud.service包下新建一个名为impl的包,然后在impl包里新建一个名为PaymentServiceImpl的实现类(新建Java类,然后命名为impl.PaymentServiceImpl,创建出来的就是在impl包下的PaymentServiceImpl类)
效果图:
10、编写PaymentServiceImpl类
package com.ken.springcloud.service.impl;
import com.ken.springcloud.service.PaymentService;
import java.util.concurrent.TimeUnit;
public class PaymentServiceImpl implements PaymentService {
@Override
public String paymentInfoOK(Integer id) {
return "线程池:" + Thread.currentThread().getName() + "paymentInfoOK,id: " + id;
}
@Override
public String paymentInfoTimeOut(Integer id) {
int timeNumber = 3;
try {
TimeUnit.SECONDS.sleep(timeNumber);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "线程池:" + Thread.currentThread().getName() + "paymentInfoTimeOut,id: " + id + "耗时" + timeNumber + "秒";
}
}
11、在com.ken.springcloud包下新建一个名为controller的包,然后在impl包里新建一个名为PaymentController的控制类(新建Java类,然后命名为controller.PaymentController,创建出来的就是在controller包下的PaymentController类)
效果图:
12、编写PaymentController类
package com.ken.springcloud.controller;
import com.ken.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Slf4j
@RestController
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${severt.port}")
private String serverPort;
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfoOK(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfoOK(id);
log.info("******result:" + result);
return result;
}
@GetMapping("/payment/hystrix/paymentInfoTimeOut/{id}")
public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfoTimeOut(id);
log.info("******result:" + result);
return result;
}
}
13、 分别启动eureka-server7001、cloud-provider-hystrix-payment8001服务
效果图:
14、检查微服务cloud-provider-hystrix-payment是否注册成功
15 、在浏览器的地址栏里分别输入http://localhost:8001/payment/hystrix/ok/1 、http://localhost:8001/payment/hystrix/timeout/1,通过调用这2个接口查看接口是否正常运行
效果图: