HyStrix主要负责日志收集。同时是一个很棒的断路器。
1、HystrixServer
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine; import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication @EnableCircuitBreaker @EnableEurekaServer @EnableHystrixDashboard @EnableTurbine public class HystrixServer { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } public static void main(String[] args) { new SpringApplicationBuilder(HystrixServer.class).web(true).run(args); } }
2、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-hystrix-server</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-hystrix-server</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
3、application.properties
spring.application.name=hystrix-server server.port=7000 security.user.name=user security.user.password=hystrix eureka.instance.leaseRenewalIntervalInSeconds=5 eureka.instance.metadataMap.zone = zone1 eureka.server.enable-self-preservation=false eureka.server.eviction-interval-timer-in-ms=5000 eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.preferSameZoneEureka=true #turbine.instanceUrlSuffix=/hystrix.stream turbine.aggregator.clusterConfig=default turbine.appConfig=hystrix-client01,hystrix-client02 #eureka.instance.metadata-map.management.port=${management.port:7000} #management.port=7000
4、HystrixClient01
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @SpringBootApplication @EnableEurekaClient @RestController @EnableCircuitBreaker public class HystrixClient01 { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } @RequestMapping("/") public String hello() { return "It works"; } @RequestMapping("/invokeA") //@HystrixCommand(fallbackMethod = "invoekD", //commandProperties = {@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")}) @HystrixCommand(fallbackMethod = "invoekD") public String invokeA() { if(Math.random()>0.2) { throw new RuntimeException("Hystrix Test A thrown"); } else { return "Hystrix Test A"; } } @RequestMapping("/invokeB") @HystrixCommand(fallbackMethod = "invoekB") public String invokeB() { if(Math.random()>0.5) { throw new RuntimeException("Hystrix Test B thrown"); } else { return "Hystrix Test B"; } } @RequestMapping("/invokeD") public String invoekD() { return "Hystrix Test D"; } public static void main(String[] args) { new SpringApplicationBuilder(HystrixClient01.class).web(true).run(args); } }
5、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-hystrix-client01</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-hystrix-client01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</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-eureka</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
6、application.properties
spring.application.name=hystrix-client01 server.port=7001 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10
7、HystrixClient02
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; //import org.springframework.cloud.netflix.turbine.EnableTurbine; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @SpringBootApplication @EnableEurekaClient @RestController @EnableCircuitBreaker //@EnableTurbine public class HystrixClient02 { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } @RequestMapping("/") public String hello() { return "It works"; } @RequestMapping("/invokeA") @HystrixCommand(fallbackMethod = "invoekD") public String invokeA() { if(Math.random()>0.2) { throw new RuntimeException("Hystrix Test A thrown"); } else { return "Hystrix Test A"; } } @RequestMapping("/invokeB") @HystrixCommand(fallbackMethod = "invoekB") public String invokeB() { if(Math.random()>0.5) { throw new RuntimeException("Hystrix Test B thrown"); } else { return "Hystrix Test A"; } } @RequestMapping("/invokeD") public String invoekD() { return "Hystrix Test D"; } public static void main(String[] args) { new SpringApplicationBuilder(HystrixClient02.class).web(true).run(args); } }
8、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-hystrix-client02</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-hystrix-client02</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!--dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
9、application.properties
spring.application.name=hystrix-client02 server.port=7002 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10
10、可以在下面的地址,查看日志输出
http://172.16.172.63:7001/hystrix.stream