• 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中的数据了

  • 相关阅读:
    stream流
    【VRP问题】基于蚁群算法求解配送路径最短问题附matlab代码
    【软件设计师21天-考点整理】1)计算机系统构成及硬件基础知识
    Rust语言之多线程
    如何在 Vue.js 中引入原子设计?
    Home Assistant在windows环境安装
    【计算机硬件组成】基础知识(必备)
    Spring系列之beanFactory与ApplicationContext
    手写一个摸鱼神器:使用python手写一个看小说的脚本,在ide中输出小说内容,同事直呼“还得是你”
    网络练习题带答案
  • 原文地址:https://blog.csdn.net/m0_51134729/article/details/126108272