前言:在上一章节中我们创建了服务提供者模块,而本节内容则是创建服务消费者模块。
1、创建一个服务提供者模块,命名为cloud-consumer-feign-hystrix-order80
(1)在父工程下新建模块
(2)选择模块的项目类型为Maven并选择模块要使用的JDK版本
(3)填写子模块的名称,然后点完成即可完成创建
效果图:
(4)修改cloud-consumer-feign-hystrix-order80子模块的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-consumer-feign-hystrix-order80</artifactId>
<dependencies>
<!--OpenFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--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: 8080
eureka:
client:
#表示是否将自己注册进Eureka Server里,默认为true
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
4、为cloud-consumer-feign-hystrix-order80子模块新建一个主启动类,类名输入com.ken.springcloud.OrderHystrixMain80,然后创建即可
效果图:
5、编写OrderHystrixMain80主启动类
package com.ken.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
//使用Feign,激活并开启
@EnableFeignClients
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class, args);
}
}
6、在com.ken.springcloud包下新建一个名为service的包
效果图:
7、在service包下新建一个名为PaymentHystrixService的接口类
效果图:
8、编写PaymentHystrixService接口类
package com.ken.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
//这里@FeignClient里写的是指定要访问的微服务的名称,表示通过FeignClient去Eureka上面找名称为CLOUD-PROVIDER-HYSTRIX-PAYMENT的微服务的接口
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {
//指明要调用的CLOUD-PROVIDER-HYSTRIX-PAYMENT的微服务的接口,这里调用的是PaymentController类里的/payment/hystrix/ok/{id}接口
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfoOK(@PathVariable("id") Integer id);
//指明要调用的CLOUD-PROVIDER-HYSTRIX-PAYMENT的微服务的接口,这里调用的是PaymentController类里的/payment/hystrix/timeout/{id}接口
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfoTimeOut(@PathVariable("id") Integer id);
}
9、在com.ken.springcloud包下新建一个名为controller的包
效果图:
10、在controller包下新建一个名为OrderHystrixController的控制类
效果图:
11、编写OrderHystrixController类
package com.ken.springcloud.controller;
import com.ken.springcloud.service.PaymentHystrixService;
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;
@RestController
public class OrdertHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfoOK(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfoOK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfoTimeOut(id);
return result;
}
}
12、分别启动eureka-server7001、cloud-provider-hystrix-payment8001、cloud-consumer-feign-hystrix-order80
效果图:
13、测试在浏览器的地址栏里分别输入http://localhost:8001/payment/hystrix/ok/1 、http://localhost:8001/payment/hystrix/timeout/1,通过调用这2个接口查看接口是否正常运行
效果图: