Kite的学习历程SpringCloud之OpenFeign的客户端的创建使用
2020-06-29 17:09
741 查看
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客户端的创建!
相关文章推荐
- Spring系列学习之Spring Cloud OpenFeign声明性HTTP REST客户端
- Kite的学习历程SpringCloud之Config配置客户端Client获取服务端信息
- springcloud openfeign客户端
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 声明式HTTP客户端 - Spring Cloud OpenFeign
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Kite的学习历程SpringCloud之Seata安装
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
- Spring-Cloud学习笔记(二)RestTemplate和Feign的使用方式以及部分注解的使用
- Kite的学习历程SpringCloud之Zipkin链路监控
- Kite的学习历程SpringCloud之Sentinel监控热点规则介绍
- Kite的学习历程SpringCloud之Hystrix服务降级
- 微服务框架Spring Cloud介绍 Part4: 使用Eureka, Ribbon, Feign实现REST服务客户端