Spring Cloud微服务实战:手把手带你整合eurekazuulfeignhystrix
转载自:https://www.jianshu.com/p/cab8f83b0f0e
代码实现:https://gitee.com/ccsoftlucifer/springCloud_Eureka_zuul
首先建立一个空的maven工程,作为项目的主工程.
在主工程的基础上,建立modul从工程.
目录的结构是这样的的:
一 .注册中心
首先微服务是一个分布式服务,那么多服务需要协调统一维护肯定需要一个注册中心来维护.所以首先建立一个注册中心模块.
在创建的时候,选择spring Initializr , GroupID对应你的com.xxxx
Artifact对应着项目名.
选择 Cloude Discovery -> Eureka Server
启动类上加上@EnableEurekaServer注解
application.properties中添加注册中心端口号,注册中心名字
#端口号.
server.port=8070
#关闭自我保护.
eureka.server.enable-self-preservation=false
#清理服务器时间间隔[5s]
eureka.server.eviction-interval-timer-in-ms=5000#主机名.
eureka.instance.hostname=localhost
#是否将自己作为客户端注册到Eureka Server[当前模块只是作为Eureka Server服务端所以设为false]
eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册信息[当前是单点的Eureka Server所以不需要同步其它节点的数据]
eureka.client.fetch-registry=false#Eureka Server[查询注册服务]地址.
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
直接访问 Locatlhost:8070就可以打开Eureka 的主界面,查看注册中心状态.当前是没有任何一个连接注册的,所以什么都没有显示.
二.服务提供者
服务的提供者也在主maven工程上创建一个modul,选择spring Initializr .
cloud Discovery -> Eureka Discovery
启动类上添加@EnableDiscoverClient注解.
application.properties中添加如下配置:
#应用名称.
spring.application.name=cloud-provider
#应用端口号.
server.port=8080
#Eureka Server服务器地址.
eureka.client.serviceUrl.defaultZone=http://localhost:8070/eureka/
定义一个controller向外暴露API
@RestController
public class MyController {@RequestMapping(value = "/info", method = RequestMethod.GET)public String info() {try {//休眠2秒,测试超时服务熔断[直接关闭服务提供者亦可]Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "Hello, cloud-provider";}
}
三.服务消费者
服务的提供者也在主maven工程上创建一个modul,选择spring Initializr .
cloud Discovery -> Eureka Discovery
在启动类上添加@EnableFeignClients和@EnableEurekaClient注解\
添加application.properties
#应用名称.
spring.application.name=cloud-consumer
#端口号.
server.port=8081
#Eureka Server服务器地址.
eureka.client.serviceUrl.defaultZone=http://localhost:8070/eureka/#高版本spring-cloud-openfeign请求分为两层,先ribbon控制,后hystrix控制.
#ribbon请求处理的超时时间.
ribbon.ReadTimeout=5000
#ribbon请求连接的超时时间
ribbon.ConnectionTimeout=5000##设置服务熔断超时时间[默认1s]
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000#开启Hystrix以支持服务熔断[高版本默认false关闭],如果置为false,则请求超时交给ribbon控制.
#feign.hystrix.enabled=true
用Feign调用远程服务.
定义服务接口类:
//1.name为被调用的服务应用名称. //2.InfoFallBack作为熔断实现,当请求cloud-provider失败时调用其中的方法. //3.feign配置. @FeignClient(name = "cloud-provider", fallback = InfoFallBack.class, configuration = MyFeignConfig.class) public interface InfoClient {//被请求微服务的地址@RequestMapping("/info")String info(); }
定义熔断类,当远程调用失败的时候调用熔断类方法.
@Component public class InfoFallBack implements InfoClient {@Overridepublic String info() {return "fallback info";} }
定义个性化feign
@Configuration public class MyFeignConfig {/*** feign打印日志等级* @return*/@BeanLogger.Level feignLoggerLeval(){return Logger.Level.FULL;} }
定义服务调用类ConsumerController,通过本地方法入口调用远程服务:
@RestController @Configuration public class ConsumerController {@AutowiredInfoClient infoClient;@RequestMapping(value = "/consumerInfo", method = RequestMethod.GET)public String consumerInfo(){return infoClient.info();} }
访问http://127.0.0.1:8081/consumerInfo 就会找到远程微服务提供 的info
四 .路由Zuul
服务的提供者也在主maven工程上创建一个modul,选择spring Initializr .
cloud Discovery -> Eureka Discovery
启动类上添加@EnableZuulProxy和@EnableEurekaClient
添加application.properties配置
#应用名称. spring.application.name=cloud-zuul #应用端口号. server.port=8071 #Eureka Server服务器地址. eureka.client.serviceUrl.defaultZone=http://localhost:8070/eureka/ #通过指定URL配置了Zuul路由,则配置以下两个超时时间. #zuul.host.connect-timeout-millis=5000 #zuul.host.socket-timeout-millis=5000#zuul使用服务发现的方式[通过serviceId路由服务],得配置ribbon的超时时间. #官网文档已说明:http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#_zuul_timeouts #ribbon请求处理的超时时间. ribbon.ReadTimeout=5000 #ribbon请求连接的超时时间. ribbon.ConnectionTimeout=5000##设置服务熔断超时时间[默认1s] hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000#只要访问以/api/开头的多层目录都可以路由到服务名为cloud-provider的服务上. zuul.routes.cloud-provider=/api/**
定义网关过滤器用来过滤请求信息.
/*** 服务网关过滤器*/ @Component public class AccessFilter extends ZuulFilter {/*** 返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型:* pre:可以在请求被路由之前调用* route:在路由请求时候被调用* post:在route和error过滤器之后被调用* error:处理请求时发生错误时被调用* @return*/@Overridepublic String filterType() {return "pre"; //前置过滤器 }@Overridepublic int filterOrder() {return 0; //过滤器的执行顺序,数字越大优先级越低 }@Overridepublic boolean shouldFilter() {return true;//是否执行该过滤器,此处为true,说明需要过滤 }/*** 过滤器具体逻辑* @return* @throws ZuulException*/@Overridepublic Object run() throws ZuulException {RequestContext ctx = RequestContext.getCurrentContext();HttpServletRequest request = ctx.getRequest();System.out.println(String.format("%s demoFilter request to %s", request.getMethod(), request.getRequestURL().toString()));String username = request.getParameter("username");// 获取请求的参数if(!StringUtils.isEmpty(username)&&username.equals("bright")){//当请求参数username为“bright”时通过ctx.setSendZuulResponse(true);// 对该请求进行路由ctx.setResponseStatusCode(200);ctx.set("isSuccess", true);// 设值,让下一个Filter看到上一个Filter的状态return null;}else{ctx.setSendZuulResponse(false);// 过滤该请求,不对其进行路由ctx.setResponseStatusCode(401);// 返回错误码ctx.setResponseBody("{\"result\":\"username is not correct!\"}");// 返回错误内容ctx.set("isSuccess", false);return null;}} }
访问http://127.0.0.1:8071/api/info 就会被拦截下来,过滤该请求.
访问http://127.0.0.1:8071/api/info?username=bright 就会被路由.调用远程微服务的接口.
转载于:https://www.cnblogs.com/battlecry/p/10722573.html
Spring Cloud微服务实战:手把手带你整合eurekazuulfeignhystrix相关推荐
- Spring Cloud微服务实战:手把手带你整合eurekazuulfeignhystrix(附源码)
Spring Cloud微服务实战:手把手带你整合eureka&zuul&feign&hystrix(附源码) Spring Cloud简介 Spring Cloud是一个基于 ...
- Spring Cloud微服务实战:外卖订餐系统
Spring Cloud微服务实战:外卖订餐系统 项目需求 客户端:针对普通用户, 用户登录.用户退出.菜品订购.我的订单. 后台管理系统:针对管理员, 管理员登录.管理员退出.添加菜品.查询菜品.修 ...
- Spring Cloud微服务实战pdf
下载地址:网盘下载 内容提要 编辑 <Spring Cloud微服务实战>从时下流行的微服务架构概念出发,详细介绍了Spring Cloud针对微服务架构中几大核心要素的解决方案和基础组件 ...
- 疯狂Spring Cloud微服务架构实战
网站 更多书籍点击进入>> CiCi岛 下载 电子版仅供预览及学习交流使用,下载后请24小时内删除,支持正版,喜欢的请购买正版书籍 电子书下载(皮皮云盘-点击"普通下载" ...
- 《Spring Cloud微服务架构实战派》PDF样章
内容摘要: 本书针对Spring Cloud Greenwich.SR2版本+Spring Boot的2.1.x.RELEASE版本.在编写过程中,不仅考虑到在企业任职所需的技能,还考虑到求职面试时可 ...
- Spring Cloud 微服务项目操作实战流程(完结)
Spring Cloud入门项目操作实战流程 Day01~02 〇.Service - 业务服务结构 商品服务 item service,端口 8001 用户服务 user service,端口 81 ...
- Spring Cloud 微服务开发实战
为什么要学习微服务? 1. 求职时增强技术自信. 微服务是非常热门的话题,企业招聘中也越来越多的要求有微服务开发.架构能力,不掌握一些微服务技能找工作时也缺乏亮点,可以说微服务架构已经成为中高级后端开 ...
- 实战系列-Spring Cloud微服务中三把利器Feign、Hystrix、Ribbon
导语 在之前的分享中分享过关于Fegin的底层实现原理,以及Spring Cloud OpenFegin的启动原理.在这次的分享中主要总结一下Spring Cloud 微服务架构的三把利器.对于F ...
- Spring Boot+Eureka+Spring Cloud微服务快速上手项目实战
说明 我看了一些教程要么写的太入门.要么就是写的太抽象.真正好的文章应该是快速使人受益的而不是浪费时间.本文通过一个包括组织.部门.员工等服务交互的案例让刚接触spring cloud微服务的朋友快速 ...
最新文章
- 一天超2000次,阿里如何打响音视频超时空战役?
- 全部python编程语言-编程语言高质量代码的优秀Python工具
- HDU 2001 计算两点间的距离
- jQuery lazyload插件详解和问题解答
- activemq和jms_保证主题,JMS规范和ActiveMQ的消息传递
- signature=1610c03482e0c6557f7ec99f0ceeae85,Vpdes Permit No. Va006557
- halcon 旋转_Halcon视觉软件应该如何系统学习?
- 图像检索(一)--综述
- Silverlight 4 tools
- CAP:Alantany 谈 CAP
- 神经网络 代码python_详细使用Python代码和数学构建神经网络— II
- 计算机法宝,计算机专业英语学习法宝.doc
- 2、Ubuntu下安装Vivado下的下载器驱动 Digilent 版本
- 哔哩哔哩助手:bilibili综合辅助扩展Chrome插件
- linux 输入法成繁体字_寻找Ubuntu中繁体字输入法 | 学步园
- 数据库学习笔记(进阶)
- 在Windows Server 2022系统上安装 Brother MFC-7450打印机驱动
- 前端接收list的情况 (批量添加)出现这个错说明不是传参错误,是解析错误
- Lombok 插件安装、使用
- postgresql10监控-使用EXTENSIONS模式