注: 需要引入eureka, ribbon, hystrix, actuator, dashboard, web依赖。
4.0.0
com.example
ribbon
0.0.1-SNAPSHOT
jar
ribbon
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
Finchley.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
server:
port: 8761
spring:
application:
name: ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8760/eureka/
package com.example.ribbon;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
System.out.println("ribbon has started!");
}
@Bean
@LoadBalanced // 该注解开启Ribbon的负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
/**
* 在2.0版本下需要在启动类中添加如下配置才可以访问断路器仪表盘
* 访问短路监控url为:http://localhost:8761/hystrix
* 在页面填写路径: http://localhost:8761/hystrix.stream
* @return
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
package com.example.ribbon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping(value = "/sayHello")
public String sayHello(@RequestParam String name) {
return this.helloService.sayHello(name);
}
}
package com.example.ribbon;
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;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloError")
public String sayHello(String name) {
// eurekaclient对应的服务名称 ribbon会自动选择实例并解析为对应的url地址 然后访问俩个客户端对应的controller
return this.restTemplate.getForObject("http://eurekaclient/sayHello?name=" + name, String.class);
}
// hystrix的回退方法
public String helloError(String name) {
return "hello " + name + ", sorry i am down...";
}
}
注: ribbon使用hystrix的回退方法与服务提供接口类在同一个类中。 如果访问失败,会调用回退方法。
通过@HystrixCommand注解的fallbackMethod指定回退方法。
注: 看到该页面说明启动成功。
点击Monitor Stream显示如下:
关闭eurekaclient和eurekaclient2俩个客户端,然后继续调用,会返回回退方法中的结果:
此时, 断路器监控页面显示如下:
此时,将俩个客户端重新开启,经过一定次数的访问之后,断路器会关闭。
注: 需要引入eureka, feign, openfeign, actuator, dashboard, web依赖。 feign默认集成了hystrix。
4.0.0
com.example
feign
0.0.1-SNAPSHOT
jar
feign
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
Finchley.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-feign
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-netflix-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8760/eureka/
server:
port: 8762
spring:
application:
name: feign
feign:
hystrix:
enabled: true
package com.example.feign;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
System.out.println("feign has started!");
}
/**
* 在2.0版本下需要在启动类中添加如下配置才可以访问断路器仪表盘
* 访问url为:http://localhost:8761/hystrix
* 在页面填写路径: http://localhost:8761/hystrix.stream
*
* @return
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
package com.example.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SayHelloController {
@Autowired
private SayHelloService sayHelloService;
@GetMapping(value = "/sayHello")
public String sayHello(@RequestParam String name) {
return this.sayHelloService.sayHello(name);
}
}
注: feign默认集成hystrix, 通过@FeignClient注解的fallback指定回退类。
package com.example.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* eurekaclient为提供服务的名称
*/
@FeignClient(value = "eurekaclient", fallback = SayHelloServiceHystrix.class)
@Service
public interface SayHelloService {
@GetMapping(value = "/sayHello")
String sayHello(@RequestParam(value = "name") String name);
}
注: feign使用hystrix通过使用@FeignClient注解的fallback属性指定回退类。
2.2.6 hystrix回退类
package com.example.feign;
import org.springframework.stereotype.Component;
/**
* SayHelloService接口的Hystrix回退类 实现SayHelloService接口即可
*/
@Component
public class SayHelloServiceHystrix implements SayHelloService {
@Override
public String sayHello(String name) {
return "hello " + name + ", sorry i am down...";
}
}
注: feign使用hystrix的回退方法在服务提供接口的实现类中,通过覆盖指定方法即可。 如果访问失败,会调用回退方法。
2.2.7 启动查看
如上。 注意要把访问端口改为8762即可。
如上。注意要把访问端口改为8762即可。
github路径:https://github.com/1956025812/springcloud
代码已提交,欢迎大家参考