关于SpringCloud的微服务结构及微服务远程调用
作者:夏志121 时间:2021-11-06 20:11:45
一、微服务结构
微服务这种方案需要技术框架来落地,全球的互联网公司都在积极尝试自己的微服务落地技术。在国内最知名的就是SpringCloud和阿里巴巴的Dubbo。
微服务技术对比:
Dubbo | SpringCloud | SpringCloudAlibaba | |
注册中心 | zookeeper、Redis | Eureka、Consul | Nacos、Eureka |
服务远程调用 | Dubbo协议 | Feign (http协议) | Dubbo、Feign |
配置中心 | 无 | SpringCloudfig | SpringCloudfig、Nacos |
服务网关 | 无 | SpringCloudGateway、Zuul | SpringCloudGateway、Zuul |
服务监控保护 | dubbo-admin、功能弱 | Hystix | Sentinel |
二、微服务远程调用
根据订单id查询订单功能:
需求:根据订单id查询订单的同时,把订单所属的用户信息一起返回
实现步骤:
1、注册RestTemplate
在order-service的OrderApplication中注册RestTemplate
@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
/**
* 创建RestTemplate并注入Spring容器
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
2、服务远程调用RestTemplate
修改order-service中的OrderService的queryOrderById方法:
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
//2、利用RestTemplate发送http,查询用户
//2.1 url路径
String url = "http://localhost:8081/user/"+order.getUserId();
//2。1 发送http请求,实现远程调用
User user = restTemplate.getForObject(url,User.class);
//3.封装user到Order
order.setUser(user);
// 4.返回
return order;
}
}
微服务调用方式:
基于RestTemplate发起的http请求实现远程调用
http请求做远程调用时与语言无关的调用,只要知道对方的ip、端口、接口路径、请求参数即可
提供者及消费者:
服务提供者:一次业务中,被其他微服务调用的服务(提供接口给其他微服务)
服务消费者:一次业务中,调用其他微服务的服务 (调用其他的微服务提供的接口)
服务调用关系:
服务提供者:暴露接口给其他微服务调用
服务消费者:调用其他微服务提供的接口
提供者与消费者角色是相对的
一个服务可以同时是服务提供者和服务消费者
来源:https://blog.csdn.net/m0_61961937/article/details/127712139
标签:SpringCloud,微服务,结构,调用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springBoot Junit测试用例出现@Autowired不生效的解决
2023-01-24 12:57:59
![](https://img.aspxhome.com/file/2023/0/73120_0s.png)
浅谈Java封装、继承、多态特性
2023-10-09 13:59:35
![](https://img.aspxhome.com/file/2023/6/67626_0s.png)
Java文本文件操作方法实例详解
2021-12-14 10:03:21
java中文转全拼工具类分享
2022-02-07 09:50:57
Java实现储存对象并按对象某属性排序的几种方法示例
2022-05-04 18:05:57
java 中HashCode重复的可能性
2021-09-13 17:38:05
LeetCode程序员面试题之无重复字符的最长子串
2021-09-12 05:26:36
ElasticSearch添加索引代码实例解析
2023-11-21 03:41:04
![](https://img.aspxhome.com/file/2023/3/59543_0s.png)
IDEA利用自带Axis工具和wsdl文件反向生成服务端客户端代码图文详解
2021-12-06 20:02:12
![](https://img.aspxhome.com/file/2023/2/73032_0s.jpg)
Maven项目修改JDK版本全过程
2021-07-19 12:13:29
![](https://img.aspxhome.com/file/2023/3/62653_0s.png)
SpringBoot整合Echarts实现用户人数和性别展示功能(详细步骤)
2023-02-22 00:31:59
![](https://img.aspxhome.com/file/2023/0/66530_0s.png)
Java全面分析面向对象之继承
2023-11-23 11:55:59
![](https://img.aspxhome.com/file/2023/5/59735_0s.png)
Java之OutputStreamWriter流案例详解
2023-11-11 13:03:55
IDEA java出现无效的源发行版14解决方案
2021-06-25 08:50:33
![](https://img.aspxhome.com/file/2023/2/62172_0s.png)
MyEclipse2018中安装Mybatis generator插件的实现步骤
2022-02-17 03:47:37
![](https://img.aspxhome.com/file/2023/9/63649_0s.png)
ShardingSphere数据分片算法及测试实战
2023-11-28 02:23:03
![](https://img.aspxhome.com/file/2023/8/59768_0s.png)
一次排查@CacheEvict注解失效的经历及解决
2023-11-13 12:07:23
![](https://img.aspxhome.com/file/2023/0/59200_0s.png)
Java语言实现二叉堆的打印代码分享
2021-11-27 23:00:15
![](https://img.aspxhome.com/file/2023/8/64008_0s.jpg)
Java编程swing组件JLabel详解以及使用示例
2023-12-15 22:28:50
java中关于深拷贝的几种方式总结
2023-12-13 17:39:41
![](https://img.aspxhome.com/file/2023/4/62054_0s.png)