Spring Cloud总结
restTemplate
- 消费者模块编写restTemplate配置类,即可在控制层调用提供者模块
// 配置类
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
// 控制层通过restTemplate
@RestController
public class OrderController {
// 被调用者的ip+端口
public static final String PAYMENT_URL = "http://localhost:8001";
// 注入
@Resource
private RestTemplate restTemplate;
// 访问http://localhost:80/consumer/payment/create,即可访问到被调用者的接口
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
// 参数为:被调用者的ip+端口+接口名,参数,返回值
return restTemplate.postForObject(PAYMENT_URL+"/payment/create", payment, CommonResult.class);
}
}
ribbon
- 用于服务调用和服务调用时的负载
// nginx实现负载均衡属于集中式LB,即在服务的消费方和提供方提供LB设施,当消费方发请求调提供方时,消费方的请求会先进入nginx,通过nginx转发分配到提供方集群
// Ribbon属于进程内LB,消费方自己选择调用哪个提供方
// 当消费者模块调用提供者模块集群时,在消费者模块自定义规则类;自定义的规则类不能放在@ComponentScan所扫描的当前包及其子包下
@Configuration
public class MySelfRule {
@Bean
public IRule myRule(){
return new RandomRule();
}
}
// 在消费者模块的启动类添加注解,指定要负载的集群和自定义的负载规则;前提是这个集群和当前消费者模块已经注册到服务注册中心
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = MySelfRule.class)
openFeign
- 用于服务调用,在消费者模块的业务层编写接口,接口中是提供者模块的控制层方法,消费者模块的控制层调用该接口
// 消费者模块通过openFeign调用提供者模块中的接口方法
// 在消费者模块引入openFeign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
// 启动类添加注解
@EnableFeignClients
// 新建feign接口
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") // 指向服务提供者的服务名
public interface PaymentFeignService {
// 直接将提供者要调用的方法复制到该接口即可
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
// 控制层
@RestController
public class OrderFeignController {
// 注入接口
@Resource
private PaymentFeignService paymentFeignService;
// 通过访问当前模块:http://localhost:80/consumer/payment/get/{id}
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
return paymentFeignService.getPaymentById(id);
}
}
gateWay
- 需新建一个module作为网关:用于反向代理,隐藏微服务的IP;用于负载均衡,访问网关,由网关进行请求转发;用于过滤,只有指定规则的请求才能进来
<!--引入依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
# yml中配置服务注册中心,将该网关模块注册进服务注册中心
# yml中配置网关转发的地址
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh # 路由的ID
uri: http://localhost:8001 # 匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** # 断言,路径相匹配的进行路由
# 访问:http://localhost:9527/payment/get/3 即可访问:http://localhost:8001/payment/get/3
- gateWay网关实现负载均衡
#
spring:
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh # 路由的ID
uri: lb://cloud-payment-service # 匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** # 断言,路径相匹配的进行路由
stream
- 用于消息中间件切换
sleuth
- 集成了zipkin:启动jar,在微服务中配置zipkin,访问后即可在zipkin中查看请求链路
nacos
- 用于服务注册、配置中心,安装应用即可
sentinel
- 用于限流、降级:只需启动jar包即可,配置限流规则和降级规则会设置熔断时间
seata
- 用于解决分布式事务问题,需安装应用
Spring Cloud总结的更多相关文章
- spring/spring boot/spring cloud开发总结
背景 针对RPC远程调用,都在使用dubbo.dubbox等,我们也是如此.由于社区暂停维护.应对未来发展,我们准备尝试新技术(或许这时候也不算什么新技术了吧),选择使用了spring ...
- 转 Netflix OSS、Spring Cloud还是Kubernetes? 都要吧!
Netflix OSS.Spring Cloud还是Kubernetes? 都要吧! http://www.infoq.com/cn/articles/netflix-oss-spring-cloud ...
- spring cloud 学习研究- spring-cloud-microservice-example
spring cloud + docker 微服务架构 http://www.open-open.com/lib/view/open1437363835818.html 实例项目 https://gi ...
- Spring Cloud集成相关优质项目推荐
Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消 ...
- spring boot分布式技术,spring cloud,负载均衡,配置管理器
spring boot分布式的实现,使用spring cloud技术. 下边是我理解的spring cloud的核心技术: 1.配置服务器 2.注册发现服务器eureka(spring boot默认使 ...
- Spring Cloud 配置服务
Spring Cloud 配置服务 1. 配置服务简介 产生背景: 传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规 ...
- Microservices Reference Architecture - with Spring Boot, Spring Cloud and Netflix OSS--转
原文地址:https://www.linkedin.com/pulse/microservices-reference-architecture-spring-boot-cloud-anil-alle ...
- 综合使用spring cloud技术实现微服务应用
在之前的章节,我们已经实现了配置服务器.注册服务器.微服务服务端,实现了服务注册与发现.这一章将实现微服务的客户端,以及联调.实现整个spring cloud框架核心应用. 本文属于<7天学会s ...
- Spring cloud实现服务注册及发现
服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务. 本文属于<7天学会spring cloud系列& ...
- 使用spring cloud实现分布式配置管理
<7天学会spring cloud系列>之创建配置管理服务器及实现分布式配置管理应用. 本文涉及到的项目: 开源项目:http://git.oschina.net/zhou666/spri ...
随机推荐
- CSAPP:datalab实验记录
CSAPP:datalab实验记录 bitXor /* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Lega ...
- 手机端web网页布局经验总结(持续更新中)
1. 首先,在网页代码的头部,加入一行viewport元标签,我们一般是不让用户手动的去改变页面的大小的. <meta name="viewport" content=&qu ...
- npm WARN checkPermissions Missing write access to ......解决方法
npm安装出错 npm WARN checkPermissions Missing write access to ...... 解决方法: 删除本地node_modules文件夹,之后再次 npm ...
- 面试官:展开说说,Spring中Bean对象是如何通过注解注入的?
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 章节目录(手写Spring,让你了解更多) [x] 第 01 章:开篇介绍,我要带你撸 Spr ...
- Mybatis学习笔记-分页
为何要分页 减少数据处理量 便于前端展示数据 使用Limit分页 语法结构 SELECT * FROM user LIMIT startIndex,pageSize; SELECT * FROM us ...
- Mybatis学习笔记-复杂查询
多个学生,对应一个老师 对于学生而言,关联:多个学生关联一个老师[多对一] 对于老师而言,集合:一个老师,有多个学生[一对多] 复杂查询环境搭建 数据库搭建 CREATE TABLE `teacher ...
- 串、KMP模式匹配算法
串是由0个或者多个字符组成的有限序列,又名叫字符串. 串的比较: 串的比较是通过组成串的字符之间的编码来进行的,而字符的编码指的是字符在对应字符集中的序号. 计算机中常用的ASCII编码,由8位二进制 ...
- C++ //构造函数的分类及调用 //分类 // 按照参数分类 无参构造函数(默认构造) 有参构造函数 //按照类型分类 普通构造 拷贝构造
1 //构造函数的分类及调用 2 //分类 3 // 按照参数分类 无参构造函数(默认构造) 有参构造函数 4 //按照类型分类 普通构造 拷贝构造 5 6 #include <iostream ...
- Java on Visual Studio Code的更新 – 2021年7月
Nick zhu, Senior Program Manager, Developer Division at Microsoft 大家好,欢迎来到 7 月版的 Visual Studio Code ...
- PTA 朋友圈 (25 分) 代码详解 (并查集)
1.题目要求: 某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友" ...