springCloud全家桶
简单介绍、使用
1、服务注册与发现eureka
介绍:
各服务系统(订单、商品、用户......)将需要对外提供的服务发布至eureka服务器上进行注册;
各eureka客户端系统,可以从eureka服务中心,发现其他服务连接信息,便于服务间调用(实现解耦);
eureka服务器
一般是集群方式部署eureka服务器,保证eureka的高可用;
配置开启@EnableEurekaServer

服务端配置文件

eureka服务使用者——各客户端
配置开启@EnableDiscoveryClient

客户端配置

RestTemplate使用
比如user服务配置跟提供的服务如下


那么在其他服务中调用user服务时


2、各服务间调用负载均衡ribbon


比如goods服务调用用户服务ribbon配置

3、服务间调用,申明式Api,Feign

开启Feign配置
@EnableFeignClients

远程调用

也可以如此使用,这样将remote跟dto抽取出去,可以实现代码复用,服务提供者维护接口依赖。也可做为对外开放接口的文档(如果调用方不想直接使用的话);

4、服务间调用,服务熔断hystrix断路器

服务开启配置
@EnableHystrix


5、微服务网关zuul
对外(web层)提供一个统一的出入口,方便做一些权限校验、安全检查;

配置
@EnableZuulProxy // 开启网关相关功能
@EnableHystrix 开启断路器检验
@EnableDiscoveryClient // 开启服务发现

ZuulFilter使用



zuul调用服务退回

为了高可用性,zuul一般也是采用集群部署的;
然后 在 zuul外部 采用nginx做网关的负载均衡器,就基本实现一个分布式微服务系统的构建了;
网友评论