-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
javen.shi
committed
Jul 14, 2018
1 parent
57adc3b
commit c3321af
Showing
15 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
18 changes: 18 additions & 0 deletions
18
...feign-hystrix/src/main/java/cn/org/zhixiang/CloudDemoConsumerFeignHystrixApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...-demo-consumer-feign-hystrix/src/main/java/cn/org/zhixiang/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
cloud-demo-consumer-feign-hystrix/src/main/java/cn/org/zhixiang/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...demo-consumer-feign-hystrix/src/main/java/cn/org/zhixiang/feign/HystrixClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...emo-consumer-feign-hystrix/src/main/java/cn/org/zhixiang/feign/HystrixClientFallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
cloud-demo-consumer-feign-hystrix/src/main/java/cn/org/zhixiang/feign/UserFeignClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
} |
14 changes: 14 additions & 0 deletions
14
cloud-demo-consumer-feign-hystrix/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
24 changes: 24 additions & 0 deletions
24
...o-consumer-hystrix/src/main/java/cn/org/zhixiang/CloudDemoConsumerHystrixApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
cloud-demo-consumer-hystrix/src/main/java/cn/org/zhixiang/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
cloud-demo-consumer-hystrix/src/main/java/cn/org/zhixiang/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
cloud-demo-consumer-hystrix/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters