springcloud中feign组件使用

1.添加依赖

<!--        feign组件-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

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();
    }
}

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);
}

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;
    }
}

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值