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类。
packagecn.itcast.order.clients;importcn.itcast.feign.pojo.User;importorg.springframework.cloud.openfeign.FeignClient;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;@FeignClient(value ="userservice")publicinterfaceUserClient{@GetMapping("/user/{id}")UserfindById(@PathVariable("id")Long id);}
分析:(基于SpringMVC)
- 服务名称:userservice
- 请求方式:GET
- 请求路径:/user/{id}
- 请求参数:id
- 返回值类型:User
4.编写业务(service)
packagecn.itcast.order.service;importcn.itcast.feign.clients.UserClient;importcn.itcast.feign.pojo.Order;importcn.itcast.feign.pojo.User;importcn.itcast.order.mapper.OrderMapper;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;@ServicepublicclassOrderService{@AutowiredprivateOrderMapper orderMapper;@AutowiredprivateUserClient userClient;publicOrderqueryOrderByUserId(Long orderId){//1.根据id查询订单Order order = orderMapper.findById(orderId);//2.查询userUser user = userClient.findById(order.getUserId());//4.封装查询结果
order.setUser(user);return order;}}
5.查询(controller)
packagecn.itcast.order.web;importcn.itcast.feign.pojo.Order;importcn.itcast.order.service.OrderService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("order")publicclassOrderController{@AutowiredprivateOrderService orderService;@GetMapping("/{orderId}")publicOrderqueryOrderById(@PathVariable("orderId")Long orderId){// 1.查询订单return orderService.queryOrderByUserId(orderId);}}
版权归原作者 酒徒ᝰ. 所有, 如有侵权,请联系我们删除。