Castle的SpringCloud学习:Eureka+Feign/Ribbon+Hystrix
springcloud示例,从零开始搭建SpringCloud的微服务应用实践,springboot版本为2.2.2.RELEASE。
按照顺序搭建,从上往下依赖
新建一个Maven项目springcloud_first
注册中心(Eureka)
- 使用Spring Initializr创建注册中心Moduleeureka-server
勾选Spring Cloud Discovery -> Eureka Server
- pom中可以去掉spring-boot-starter-web和spring-boot-starter-tomcat(暂时未发现任何区别,不清楚为什么默认会添加)。
- 在resources下创建配置文件application.yml(没有使用默认的application.properties,个人觉得 .yml的可读性高些)
- 在启动类EurekaServerApplication添加注解 @EnableEurekaServer表明自己是服务注册中心
- 启动服务后,浏览器访问http://localhost:8761/成功
提供者(Eureka)
前2步和注册中心一致,不做累述
- 使用Spring Initializr创建注册中心Moduleeureka-server
勾选Spring Cloud Discovery -> Eureka Server - pom中可以去掉spring-boot-starter-web和spring-boot-starter-tomcat。
- 在resources下创建配置文件application.yml
- 在启动类EurekaClientApplication添加注 @EnableEurekaServer表明自己是服务提供者;添加注解 @RestController,用于获取返回值。
- 启动服务后,浏览器访问http://localhost:8762/welcome?name=Castle成功
- 并且在注册中心发现服务
修改端口后分别运行(cmd中 使用 java -jar XXX.jar),即可在注册中心找到多个提供者(仅端口不同)
消费者(Feign)
-
使用Spring Initializr创建服务消费者Module,勾选Spring Cloud Discovery -> Eureka Server,Spring Cloud Routing -> OpenFeign
-
在resources下创建配置文件application.yml
-
在启动类添加注解 @EnableFeignClients表明自己是服务消费者。
-
添加接口类 WelcomeRemote
-
添加调用控制类 WelcomeController
注意idea会提示注解bean失败(听说运行没问题),2种解决方法:
1.修改idea设置:
1) ctrl+alt+s(打开setting快捷键)
2) Inspections
3) 在右侧搜索框中找spring core
4) 将Autowiring for Bean class设置为Waring
2.接口类添加注解@Repository -
启动服务后,浏览器访问http://localhost:8765/welcome?name=Castle成功
并且注册中心找到新建的消费者此时,在有多个提供者的情况下,会采用默认的轮询来负载均衡
消费者(ribbon)
- 使用Spring Initializr创建服务消费者Module,勾选Spring Cloud Discovery -> Eureka Server,Spring Cloud Routing -> Ribbon
- 在resources下创建配置文件application.yml,参考[消费者(Feign)],修改端口(8766)以共存。
- 在启动类装载 RestTemplate Bean对象
- 添加接口实现等类
-
添加服务接口类 WelcomeService
-
添加服务接口实现类 WelcomeServiceImpl
注意uri拼接上有提供者的名称“eureka-client”
-
添加服务调用控制类 WelcomeController
-
- 启动服务后,浏览器访问http://localhost:8766/welcome?name=Castle成功,并且注册中心找到新建的消费者。
因为端口号没有冲突,和feign共存
此时,在有多个提供者的情况下,会采用默认的轮询来负载均衡
断路器(Hystrix)
- 在ribbon-consumer中添加hystrix依赖
<!--hystrix断路器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
- 接口实现类WelcomeServiceImpl中加入相关注解和断路器调用方法
- 在启动类RibbonConsumerApplication添加注解 @EnableHystrix
- 关闭一个提供者,多次访问 http://localhost:8766/welcome?name=Castle,观测断路器生效。
对比一下直接访问原提供者
网关(Zuul)
-
使用Spring Initializr创建注册中心Modulezuul-gateway
勾选Spring Cloud Routing -> Zuul -
在resources下创建配置文件application.yml
此处使用了serviceId,所以必须依赖Eureka,否则可以选择url方式,原因:
通过url映射的方式对于Zuul并不是特别友好,Zuul需要知道所有服务的地址才能完成所有的映射配置。而实际上,服务名与服务实例地址的关系在eureka server中已经存在了,所以只需要将Zuul注册到eureka server上去发现其他服务,我们就可以完成对serviceId的映射。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
-
在启动类 ZuulGatewayApplication添加注 @EnableZuulProxy表明自己是网关。
-
新建过滤器 TokenFilter
-
启动服务后,浏览器访问http://localhost:8767/welcome?name=Castle&token=Caslte成功;再访问缺失token的地址http://localhost:8767/welcome?name=Castle&token=失败(这一定是故意的)。
当然,在注册中心也发现刚定义好的网关服务。
PS.Markdown格式控制有点问题,查看有问题可以访问本文原址。