这里的熔断路由其实类似,他跟踪eureka服务、其他服务等的可用性。这是微服务架构中一个 重要的思想,当我们的服务不能响应服务的访问者的调用时,给出的一种自我保护的功能(如果不这样做失败的请求会因为超时等其他因素拖垮服务提供者)
Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class Application {
public static void main(String[] args) {
SpringApplication.run( Application.class, args );
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
//hiError 指定了返回的方法 hiError(String name)
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}
在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
@SpringBootApplication
@Controller
@EnableHystrix
@EnableHystrixDashboard
public class DashboardApp extends SpringBootServletInitializer {
//我们把所有来自根的请求全部转发到hystirx
@RequestMapping("/")
public String home() {
return "forward:/hystrix";
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DashboardApp.class).web(true);
}
public static void main(String[] args) {
SpringApplication.run(DashboardApp.class, args);
}
}
图片.png
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
server:
port: 8989
management:
port: 8990
turbine:
aggregator:
clusterConfig: USER-SERVICE,RESTAURANT-SERVICE
# clusterNameExpression: new String("default")
appConfig: user-service,restaurant-service
# clusterNameExpression: 'default'
# InstanceMonitor:
# eventStream:
# skipLineLogic: false
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
metadataMap:
instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}
client:
serviceUrl:
defaultZone: ${vcap.services.${PREFIX:}eureka.credentials.uri:http://user:password@localhost:8761}/eureka/
#http://localhost:8761/eureka/
fetchRegistry: true
spring could 的使用就不再叙述了,具体参考这个系列博客https://blog.csdn.net/forezp/article/details/70148833/