• 12.OpenFeign 实例(springcloud)


    1.OpenFeign 简介
    Feign 声明性 ( 注解 ) Web 服务 客户端 。它使编写 Web 服务客户端更加容易。 要使用 Feign 请创建一个接口并对其进行注解 。它具有可插入注解支持,包括 Feign 注解和 JAX-RS 注解。Feign 还支持可插拔编码器和解码器。 Spring Cloud 添加了对 Spring MVC 注解的支持 ,并支持使用 HttpMessageConverters Spring Web 中默认使用的注解。 Spring Cloud 集成了 Ribbon Eureka 以及 Spring Cloud LoadBalancer ,以 在使用 Feign 时提供负载平衡 http 客户端
    Feign 是一个 远程调用 的组件 ( 接口,注解 ) http 调用的
    Feign 集成了 ribbon
    ribbon 里面集成了 eureka
    3.OpenFeign 快速入门
    3.1 本次调用的设计图

    3.2 启动一个 eureka-server 服务,这里不重复演示,参考 eureka文档
    3.3 先创建 provider-order-service ,选择依赖

    3.4 provider-order-service 修改配置文件
     
    1. server:
    2. port: 8080
    3. spring:
    4. application:
    5. name: order-service
    6. eureka:
    7. client:
    8. service-url:
    9. defaultZone: http://192.168.174.133:8761/eureka

    3.5 provider-order-service 修改启动类增加一个访问接口
    1. package com.it.controller;
    2. import org.springframework.web.bind.annotation.GetMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class OrderController {
    6. @GetMapping("doOrder")
    7. public String doOrder(){
    8. return "汉堡-可乐";
    9. }
    10. }

    3.6 provider-order-service 启动测试访问
    1. package com.it;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    5. @SpringBootApplication
    6. @EnableEurekaClient
    7. public class OrederService01Application {
    8. public static void main(String[] args) {
    9. SpringApplication.run(OrederService01Application.class, args);
    10. }
    11. }

    3.7 再创建 consumer-user-service ,选择依赖
    3.8 consumer-user-service 修改配置文件
    1. server:
    2. port: 8081
    3. spring:
    4. application:
    5. name: user-service
    6. eureka:
    7. client:
    8. service-url:
    9. defaultZone: http://192.168.174.133:8761/eureka

    3.9 consumer-user-service 创建一个接口(重点)
    1. package com.it.feign;
    2. import org.springframework.cloud.openfeign.FeignClient;
    3. import org.springframework.web.bind.annotation.GetMapping;
    4. /**
    5. * @FeignClient(value = "order-service")
    6. * value:就是提供者的应用名称
    7. */
    8. @FeignClient(value = "order-service")
    9. public interface UserOrderFeign {
    10. @GetMapping("doOrder")
    11. public String doOrder();
    12. }

    3.10 consumer-user-service 创建 controller
    1. package com.it.controller;
    2. import com.it.feign.UserOrderFeign;
    3. import com.netflix.discovery.converters.Auto;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. @RestController
    8. public class UserControler {
    9. @Autowired
    10. public UserOrderFeign userOrderFeign;
    11. @GetMapping("userDoOrder")
    12. public String userDoOrder(){
    13. System.out.println("有用户进来了");
    14. //发起一个远程调用
    15. String s = userOrderFeign.doOrder();
    16. return s;
    17. }
    18. }

    3.11 consumer-user-service 修改启动类
    1. package com.it;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    5. import org.springframework.cloud.openfeign.EnableFeignClients;
    6. @SpringBootApplication
    7. @EnableEurekaClient
    8. @EnableFeignClients
    9. public class UserService02Application {
    10. public static void main(String[] args) {
    11. SpringApplication.run(UserService02Application.class, args);
    12. }
    13. }

    3.12 启动调用测试

    3.13 本次调用总结
    consumer-user-service--- /userDoOrder --- 》通过 feign 调用 /doOrder ---
    provider-order-service 下单成功
    3.14 调用超时设置

    1. server:
    2. port: 8081
    3. spring:
    4. application:
    5. name: user-service
    6. eureka:
    7. client:
    8. service-url:
    9. defaultZone: http://192.168.174.133:8761/eureka
    10. #feign 只是帮你封装了远程调用的功能,底层还是ribbon,所以修改默认等待时间需要修改ribbon的时间
    11. ribbon:
    12. ReadTimeout: 3000 #给3s的超时时间
    13. ConnectTimeout: 3000 #连接服务的超时时间

  • 相关阅读:
    【限时免费】20天拿下华为OD笔试之【单调栈】2023Q1A-天然蓄水池【欧弟算法】全网注释最详细分类最全的华为OD真题题解
    centos7安装mysql8
    腾讯云我的世界mc服务器多少钱一年?
    一个简单HTML5期末考核大作业,学生个人html静态网页制作代码
    将数据、代码、栈放入不同的段
    linux服务器上的matplotlib中文字体乱码问题(使用ttf文件)
    第一章 计算机网络概论
    MySQL数据库进阶篇
    MySQL数据库 #4
    【系列】经典算法题 :排序算法空间
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/127800994