Skip to content

Commit

Permalink
Hystrix使用
Browse files Browse the repository at this point in the history
  • Loading branch information
javen.shi committed Jul 14, 2018
1 parent 57adc3b commit c3321af
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
<br>
<a href="https://zhixiang.org.cn/#/blog/read/7c4e3ff7-786c-47fb-bdc3-d0856c8415ff">如何使用高可用的Eureka </a>
<br>
<a href="https://zhixiang.org.cn/#/blog/read/4f908c52-4ab9-4261-8189-e76a9fdbcc14">Hystrix使用 </a>
<br>
33 changes: 33 additions & 0 deletions cloud-demo-consumer-feign-hystrix/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>cn.org.zhixiang</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-demo-consumer-feign-hystrix</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--注意此处的依赖是SpringBoot2.0以后专用的,如果您使用的SpringBoot版本低于2.0请使用spring-cloud-starter-feign-->
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cn.org.zhixiang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class CloudDemoConsumerFeignHystrixApplication {

public static void main(String[] args) {
SpringApplication.run(CloudDemoConsumerFeignHystrixApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cn.org.zhixiang.controller;

import cn.org.zhixiang.domain.User;
import cn.org.zhixiang.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
private UserFeignClient userFeignClient;

@GetMapping("/getUser/{id}")
public User getUser(@PathVariable Long id){
return userFeignClient.getUser(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cn.org.zhixiang.domain;

public class User {

private long id;
private String name;
private int age;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cn.org.zhixiang.feign;

import cn.org.zhixiang.domain.User;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class HystrixClientFactory implements FallbackFactory<UserFeignClient> {

private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFactory.class);

@Override
public UserFeignClient create(Throwable cause) {
HystrixClientFactory.LOGGER.info("the provider error is: {}", cause.getMessage());
return new UserFeignClient() {
@Override
public User getUser(Long id) {
User user = new User();
user.setName("王五");
return user;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cn.org.zhixiang.feign;

import cn.org.zhixiang.domain.User;
import org.springframework.stereotype.Component;

@Component
public class HystrixClientFallback implements UserFeignClient {
@Override
public User getUser(Long id) {
User user = new User();
user.setName("王五");
return user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.org.zhixiang.feign;

import cn.org.zhixiang.domain.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


//@FeignClient(name = "provider-demo", fallback = HystrixClientFallback.class)
@FeignClient(name = "provider-demo", fallbackFactory = HystrixClientFactory.class)
public interface UserFeignClient {

@GetMapping (value = "/user/getUser/{id}")
public User getUser(@PathVariable("id") Long id);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spring:
application:
name: consumer-demo-feign

server:
port: 8089

eureka:
client:
register-with-eureka: true
service-url:
defaultZone: http://root:root@localhost:8761/eureka

feign.hystrix.enabled: true
34 changes: 34 additions & 0 deletions cloud-demo-consumer-hystrix/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>cn.org.zhixiang</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-demo-consumer-hystrix</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.org.zhixiang;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class CloudDemoConsumerHystrixApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(CloudDemoConsumerHystrixApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cn.org.zhixiang.controller;

import cn.org.zhixiang.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
private RestTemplate restTemplate;

@GetMapping("/getUser/{id}")
@HystrixCommand(fallbackMethod = "getUserFallback")
public User getUser(@PathVariable Long id){
return restTemplate.getForObject("http://provider-demo/user/getUser/"+id,User.class);
}
public User getUserFallback(Long id) {
User user = new User();
user.setName("王五");
return user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cn.org.zhixiang.domain;

public class User {

private long id;
private String name;
private int age;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}
13 changes: 13 additions & 0 deletions cloud-demo-consumer-hystrix/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
spring:
application:
name: consumer-demo

server:
port: 8088

eureka:
client:
register-with-eureka: true
service-url:
defaultZone: http://root:root@localhost:8761/eureka
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<module>cloud-demo-provider-2</module>
<module>cloud-demo-consumer-feign</module>
<module>cloud-demo-eureka-high</module>
<module>cloud-demo-consumer-hystrix</module>
<module>cloud-demo-consumer-feign-hystrix</module>
</modules>

<build>
Expand Down

0 comments on commit c3321af

Please sign in to comment.