Kite的学习历程SpringCloud之OpenFeign的客户端的创建使用
程序员文章站
2022-06-06 14:52:03
...
Kite学习历程的第二十二天
1. OpenFeign的服务调用
其特点:
声明式REST客户端:Feign创建一个用JAX-RS或Spring MVC注释修饰的接口的动态实现
1.1 创建OpenFeign的客户端cloud-consumer-feign-order-81
1.1.1 通过pom.xml引入依赖
相比ribbon多引入了:spring-cloud-starter-openfeign
<?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>demo01cloud</artifactId>
<groupId>cn.kitey.spring</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-order-81</artifactId>
<dependencies>
<!--openfeign依赖的引入-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入自己创建的entities包-->
<dependency>
<groupId>cn.kitey.spring</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</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>
</dependencies>
</project>
1.1.2 创建application.yml配置文件
注册中心还是Eureka注册中心
server:
port: 81
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/
1.1.3 创建主启动类
注意:
添加注解:
@EnableFeignClients:表示这个是OpenFeign的客户端
package cn.kitey.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain81 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain81.class, args);
}
}
1.1.4 创建service接口
注意注解:
- @FeignClient(value = “CLOUD-PAYMENT-SERVICE”):表示连接注册中心服务端的名称
- 其中的方法要与在服务端的方法保持一致,以及getmapping类容也要保持一致
package cn.kitey.springcloud.service;
import cn.kitey.springcloud.entities.CommonResult;
import cn.kitey.springcloud.entities.Payment;
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(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value = "/payment/get/{id}")
CommonResult<Payment> getPaymentById (@PathVariable("id") Long id);
}
1.1.5 创建controller类
- 通过创建接口对象:paymentFeignService
- 调用其getPaymentById 方法,进行服务端的控制
package cn.kitey.springcloud.controller;
import cn.kitey.springcloud.entities.CommonResult;
import cn.kitey.springcloud.entities.Payment;
import cn.kitey.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
public class OrderFeignController {
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
return paymentFeignService.getPaymentById(id);
}
}
1.2 进行测试
启动Eureka的集群注册中心7001,7002,然后启动服务端8001,8002,然后启动81的客户端
Eureka注册中心:
通过访问:
http://localhost:81/consumer/payment/get/4
获取服务端的信息
或者http://localhost:81/consumer/payment/get/3
以上就完成了OpenFeign客户端的创建!
上一篇: PHP展示消息ALERT("")不出来
下一篇: 高考期间开车勿鸣笛
推荐阅读
-
Kite的学习历程SpringCloud之Sentinel监控初步使用
-
C++学习之线程的概念、创建以及使用实例
-
Oracle数据库学习之表的创建与使用(修改表结构)不建议使用
-
十四、SpringCloud学习笔记之Gateway-Predicate的使用
-
Kite的学习历程SpringCloud之基于Nacos的服务提供者的创建
-
Kite的学习历程之SpringCloud之Rest微服务构建之微服务消费者Moudle
-
Kite的学习历程SpringCloud之OpenFeign的客户端的创建使用
-
Kite的学习历程SpringCloud之Gateway网关路由配置
-
Kite的学习历程之SpringCloud之EurekaServer集群配置,服务端集群配置并且使其负载均衡
-
Kite的学习历程SpringCloud之基于Nacos的服务消费者的创建