前言:
必需学会SpringBoot基础知识
简介:
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。
工具:
JDK8
apache-maven-3.5.2
IntelliJ IDEA 2017.3 x64
一、断路器简介
Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的; 参考图片: http://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_clients
简单来说: 在架构里面那个服务器挂了, 就会返回报错.
二、准备工作
本篇是基于第一篇为基础, 首先, 启动 eureka-server:8761 和 eureka-client 项目的dev:8762
三、在ribbon使用断路器
3.1 pom.xml 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
3.2 启动类追加@EnableHystrix注解开启Hystrix
package com.lwc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix; /**
* @author Eddie
*/
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication { public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}
}
3.3 Service改造, 追加功能
package com.lwc.service; /**
* @author eddie.lee
* @Package com.lwc.service
* @ClassName RibbonService
* @description
* @date created in 2018-03-26 19:55
* @modified by
*/
public interface RibbonService { String ribbonService(String name); /**
* Hystrix 熔断 返回错误信息
*/
String hystrixError(String name);
}
package com.lwc.service.impl; import com.lwc.service.RibbonService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; /**
* @author eddie.lee
* @Package com.lwc.service.impl
* @ClassName RibbonServiceImpl
* @description
* @date created in 2018-03-26 19:57
* @modified by
*/
@Service
public class RibbonServiceImpl implements RibbonService { @Autowired
private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hystrixError")
@Override
public String ribbonService(String name) {
final String url = "http://eureka-client/eureka/client?name=" + name;
return restTemplate.getForObject(url, String.class);
} @Override
public String hystrixError(String name) {
return "hi, " + name + " ,sorry, hystrix error!";
}
}
3.4 浏览器查看 A:成功, B:失败 (先是启动相关服务, 请求一次地址, 然后在关闭client服务器, 在请求一次地址)
A: http://localhost:8764/eureka/ribbon?name=eddie
hi eddie, i am from port:8762
B: http://localhost:8764/eureka/ribbon?name=eddie
hi, eddie ,sorry, hystrix error!
四、Feign中使用断路器
4.1 pom.xml添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
4.2 *.yml添加开启断路器
如果是properties文件,请在文件中加入:
feign.hystrix.enabled=true
如果是yml文件,请在文件中加入:
feign:
hystrix:
enabled: true
4.3 FeignClientService 改造, 添加 fallback 错误返回
package com.lwc.service; import com.lwc.config.FeignClientServiceHystric;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; /**
* @author eddie.lee
* @Package com.lwc.service
* @ClassName FeignClientService
* @description
* @date created in 2018-03-27 10:43
* @modified by
*/
@FeignClient(value = "eureka-client", fallback = FeignClientServiceHystric.class)
public interface FeignClientService { @GetMapping("/eureka/client")
String feignToClientByName(@RequestParam("name") String name); }
package com.lwc.config; import com.lwc.service.FeignClientService;
import org.springframework.stereotype.Component; /**
* @author eddie.lee
* @Package com.lwc.config
* @ClassName FeignClientServiceHystric
* @description
* @date created in 2018-03-27 15:21
* @modified by
*/
@Component
public class FeignClientServiceHystric implements FeignClientService { @Override
public String feignToClientByName(String name) {
return "Hystric Error, Sorry " + name;
} }
4.4 浏览器查看 A:成功, B:失败 (先是启动相关服务, 请求一次地址, 然后在关闭client服务器, 在请求一次地址)
A: http://localhost:8765/eureka/feign?name=eddie
hi eddie, i am from port:8762
B: http://localhost:8765/eureka/feign?name=eddie
Hystric Error, Sorry eddie
五、Hystrix Dashboard (断路器:Hystrix 仪表盘)
eureka-ribbon 或者 eureka-feign 改造, 添加仪表盘
5.1 pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
5.2 启动类添加注解
@EnableHystrix@EnableHystrixDashboard
5.3 浏览器查看
地址: http://localhost:8765/hystrix.stream
输入: http://localhost:8765/hystrix.stream
Title:{name}
测试: 请求 http://localhost:8765/eureka/feign?name=eddie 就会在仪表盘出现对应数据, 比如次数
5.4 错误
提示: Unable to connect to Command Metric Stream.
解决: 查看是否缺少依赖,或者注解
六、参考资料
eureka-feign
http://localhost:8765/eureka/feign?name=eddie
eureka-ribbon
http://localhost:8764/eureka/ribbon?name=eddie
标签
4-1, 4-2
源码下载
https://github.com/eddie-code/SpringCloudDemo#springclouddemo