SpringCloud——Feign远程调用1

CSDN话题挑战赛第2期
参赛话题:学习笔记

请添加图片描述
个人名片:

博主酒徒ᝰ.
个人简介沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本篇励志三人行,必有我师焉。

请添加图片描述
本项目基于B站黑马程序员Java《SpringCloud微服务技术栈》,SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式

【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】 点击观看

一、Feign替代RestTemplate

下面是我们之前的访问方式,需要输入url地址值,这种方式在之后地址值很长的时候就不在好用,可读性差,维护难。

String url = "http://userservice/user/" + userId;
User user = restTemplate.getForObject(url, User.class);

由于之前的方式难以维护,于是我们使用Feign来代替。
使用方式:

1.引入依赖

在orderservice中引入feign依赖

<!--feign客户端依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.开启Feign

在order-service中添加@EnableFeignClients注解

package cn.itcast.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

3.编写Feign客户端

创建clients模块,在其中创建UserClient类。

package cn.itcast.order.clients;

import cn.itcast.feign.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "userservice")
public interface UserClient {
    
    
    @GetMapping("/user/{id}")
    User findById(@PathVariable("id") Long id);
}

分析:(基于SpringMVC)

  • 服务名称:userservice
  • 请求方式:GET
  • 请求路径:/user/{id}
  • 请求参数:id
  • 返回值类型:User

4.编写业务(service)

package cn.itcast.order.service;

import cn.itcast.feign.clients.UserClient;
import cn.itcast.feign.pojo.Order;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
    
    

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserClient userClient;


    public Order queryOrderByUserId(Long orderId) {
    
    
        //1.根据id查询订单
        Order order = orderMapper.findById(orderId);
        //2.查询user
        User user = userClient.findById(order.getUserId());
        //4.封装查询结果
        order.setUser(user);

        return order;
    }
}

5.查询(controller)

package cn.itcast.order.web;

import cn.itcast.feign.pojo.Order;
import cn.itcast.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@RequestMapping("order")
public class OrderController {
    
    

   @Autowired
   private OrderService orderService;

    @GetMapping("/{orderId}")
    public Order queryOrderById(@PathVariable("orderId") Long orderId) {
    
    
        // 1.查询订单
        return orderService.queryOrderByUserId(orderId);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_65144570/article/details/127138942