• springcloud中feign组件使用


    1.添加依赖

    
    <dependency>
       <groupId>org.springframework.cloudgroupId>
       <artifactId>spring-cloud-starter-openfeignartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.在启动类中添加@EnableFeignClients注解

    package com.xiaoyu;
    
    import com.alibaba.cloud.nacos.loadbalancer.ConditionalOnLoadBalancerNacos;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @MapperScan("com.xiaoyu.mapper")
    @EnableDiscoveryClient
    @EnableFeignClients
    public class OrderApplication {
        public static void main(String[] args) {
            SpringApplication.run(OrderApplication.class, args);
        }
        @Bean//添加nacos客户端Bean
        @LoadBalanced//负载均衡注解
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    3.创建调用接口

    package com.xiaoyu.service;
    
    import com.xiaoyu.entity.Product;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    @FeignClient("service-product")
    public interface ProductService {
        @GetMapping("/product/{pid}")
        Product getById(@PathVariable("pid") Long pid);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.Controller中调用接口中的方法

    package com.xiaoyu.controller;
    
    import com.alibaba.cloud.nacos.loadbalancer.ConditionalOnLoadBalancerNacos;
    import com.xiaoyu.entity.Order;
    import com.xiaoyu.entity.Product;
    import com.xiaoyu.service.OrderService;
    import com.xiaoyu.service.ProductService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.discovery.DiscoveryClient;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import java.net.URI;
    import java.util.List;
    import java.util.Random;
    
    @Slf4j
    @RestController
    @RequestMapping("/order")
    public class OrderController {
        @Autowired
        private ProductService productService;
        @GetMapping("/prod/{pid}")
        public Order order(@PathVariable("pid") Long pid) {
    //        -----------这里是Feign----------
            Product product = productService.getById(pid);
            log.info("product = " + product);
            Order order = Order.builder().uid(1L).username("张三").pid(product.getId()).pname(product.getName()).price(product.getPrice()).number(1).build();
    //        orderService.save(order);
            return order;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    在浏览器输入http:localhost:8073/order/prod/pid就可以访问到service-product中的数据了

  • 相关阅读:
    国内最牛的Java面试八股文合集,不接受反驳 我这该死的魅力
    Python:实现perfect cube完全立方数算法(附完整源码)
    VisualStudio运行程序,点击应用程序时,弹出多个个窗体问题
    LED灯实验--汇编
    ISIS-ISIS高级特性—05
    Kafka概论
    单片机中文编程器手机版:功能解析与用户体验
    英特尔 Linux Vulkan 驱动程序的首席开发人员离职;JDK 18 功能集被冻结,进入 Rampdown 第一阶段;Ubuntu 禁用 os-prober | 开源日报
    mysql基础部分第一次复习(1-8章,到聚合函数)
    Linux基础知识——docker教程
  • 原文地址:https://blog.csdn.net/m0_51134729/article/details/126108272