Skip to content

Commit e56ede3

Browse files
FeignClient的fallbackFactory属性
1 parent 6aaca8b commit e56ede3

File tree

14 files changed

+289
-70
lines changed

14 files changed

+289
-70
lines changed

eureka-client/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@
5353
</dependency>
5454

5555

56+
<dependency>
57+
<groupId>org.projectlombok</groupId>
58+
<artifactId>lombok</artifactId>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>com.alibaba</groupId>
63+
<artifactId>fastjson</artifactId>
64+
<version>1.2.54</version>
65+
</dependency>
66+
5667
</dependencies>
5768

5869
<dependencyManagement>
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package com.example.demo.controller;
22

3-
import org.springframework.web.bind.annotation.GetMapping;
4-
import org.springframework.web.bind.annotation.RestController;
3+
import com.alibaba.fastjson.JSONObject;
4+
import com.example.demo.pojo.User;
5+
import org.springframework.web.bind.annotation.*;
56

67
@RestController
78
public class UserController {
89

9-
@GetMapping("/user/name")
10-
public void getUserName( ) throws Exception {
11-
System.out.println("==================================================================>getUserName( )");
12-
throw new Exception("getUserNameException");
10+
@PostMapping("/user/name/{id}")
11+
public JSONObject getUserNameById(@PathVariable("id") Integer id ) throws Exception {
12+
System.out.println("==================================================================>getUserNameById(),id为:"+id);
13+
//直接抛异常,是为了测试服务熔断和降级。
14+
throw new Exception("getUserNameByIdException");
1315
}
1416

15-
17+
@PostMapping("/user")
18+
public User getUserById(@RequestParam("id") Integer id ) throws Exception {
19+
System.out.println("==================================================================>getUserById(),id为:"+id);
20+
throw new Exception("getUserByIdException");
21+
}
1622

1723
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.demo.pojo;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@AllArgsConstructor
10+
@NoArgsConstructor
11+
@Builder
12+
public class User {
13+
private Integer id;
14+
private String name;
15+
16+
17+
}

eureka-consumer-feign/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@
5252
<groupId>org.projectlombok</groupId>
5353
<artifactId>lombok</artifactId>
5454
</dependency>
55+
56+
<dependency>
57+
<groupId>com.alibaba</groupId>
58+
<artifactId>fastjson</artifactId>
59+
<version>1.2.54</version>
60+
</dependency>
5561
</dependencies>
5662

63+
64+
5765
<!--指定springCloud版本-->
5866
<dependencyManagement>
5967
<dependencies>

eureka-consumer-feign/src/main/java/com/example/demo/controller/DcController.java renamed to eureka-consumer-feign/src/main/java/com/example/demo/controller/ConsumerDcController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Created by lenovo on 九月
1010
*/
1111
@RestController
12-
public class DcController {
12+
public class ConsumerDcController {
1313

1414
@Autowired
1515
private DcClient dcClient;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.demo.controller;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import com.example.demo.feignInterface.user.UserFeignClient;
5+
import com.example.demo.pojo.User;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
@RestController
10+
public class ConsumerUserController {
11+
12+
@Autowired
13+
private UserFeignClient userFeignClient;
14+
15+
@PostMapping("/consumer/user/name/{id}")
16+
public JSONObject getUserName(@PathVariable("id") Integer id){
17+
return userFeignClient.getUserNameById(id);
18+
}
19+
20+
@PostMapping("/consumer/user")
21+
public User getUser(@RequestParam("id") Integer id){
22+
return userFeignClient.getUserById(id);
23+
}
24+
}
25+
26+

eureka-consumer-feign/src/main/java/com/example/demo/controller/UserController.java

-21
This file was deleted.

eureka-consumer-feign/src/main/java/com/example/demo/feignInterface/dc/ClientHystrixFallbackFactory.java

-15
This file was deleted.

eureka-consumer-feign/src/main/java/com/example/demo/feignInterface/dc/DcClient.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
* 使用@FeignClient注解。其中的value指定这个接口所要调用的服务名称,fallback指定hystrix的回调类
1212
* 接口中定义的各个函数使用Spring MVC的注解就可以来绑定服务提供方的REST接口
1313
*/
14-
//@FeignClient(value = "eureka-client",fallback = ClientHystrix.class)
15-
@FeignClient(value = "eureka-client",fallbackFactory = ClientHystrixFallbackFactory.class)
14+
@FeignClient(value = "eureka-client",fallback = ClientHystrix.class)
1615
public interface DcClient {
1716
@GetMapping(value = "/dc")
1817
String consumer();

eureka-consumer-feign/src/main/java/com/example/demo/feignInterface/dc/DcClientFallbackFactory.java

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
package com.example.demo.feignInterface.user;
22

3+
import com.alibaba.fastjson.JSONObject;
4+
import com.example.demo.pojo.User;
35
import lombok.extern.slf4j.Slf4j;
46
import org.springframework.stereotype.Component;
57

68
@Component
7-
@Slf4j
89
public class UserClientFallbackFactory implements UserFeignClient {
910

11+
/**
12+
* 服务熔断
13+
* @param id
14+
* @return
15+
*/
1016
@Override
11-
public String getUserName() {
12-
log.debug("=======================>UserClientFallbackFactoryTest");
13-
return "UserClientFallbackFactoryTest";
17+
public JSONObject getUserNameById(Integer id) {
18+
System.out.println("=======================>UserClientFallbackFactoryTest");
19+
JSONObject resultJson = new JSONObject();
20+
resultJson.put("errCode", "0404" );
21+
String description="查询id为"+id+"的用户,服务异常,暂时熔断";
22+
resultJson.put("description", description );
23+
return resultJson;
1424
}
25+
26+
27+
@Override
28+
public User getUserById(Integer id) {
29+
System.out.println("=======================>UserClientFallbackFactoryTest");
30+
//直接返回id为-1的用户
31+
User user = new User();
32+
user.setId(-1);
33+
return user;
34+
}
35+
1536
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.example.demo.feignInterface.user;
22

3+
import com.alibaba.fastjson.JSONObject;
4+
import com.example.demo.pojo.User;
35
import org.springframework.cloud.netflix.feign.FeignClient;
4-
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
59

610

711
/**
@@ -10,6 +14,9 @@
1014
@FeignClient(value = "eureka-client",fallbackFactory = UserHystrixFallbackFactory.class)
1115
public interface UserFeignClient {
1216

13-
@GetMapping(value = "/user/name")
14-
String getUserName();
17+
@PostMapping(value = "/user/name/{id}")
18+
JSONObject getUserNameById(@PathVariable("id") Integer id);
19+
20+
@PostMapping(value = "/user")
21+
User getUserById(@RequestParam("id") Integer id);
1522
}

eureka-consumer-feign/src/main/java/com/example/demo/feignInterface/user/UserHystrixFallbackFactory.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ public class UserHystrixFallbackFactory implements FallbackFactory<UserFeignCli
1313

1414
@Override
1515
public UserFeignClient create(Throwable cause) {
16-
log.info("fallback reason was: {} ", cause.getMessage());
16+
System.out.println("====================================》fallback reason was: " + cause.getMessage());
1717

1818
return new UserClientFallbackFactory();
19-
//也可以不写UserClientFallbackFactory类,直接写成以下形式
20-
// return new UserFeignClient() {
19+
//也可以不写UserClientFallbackFactory类,直接用匿名对象写成以下形式
20+
// return new UserFeignClient(Integer id) {
2121
// @Override
22-
// public String getUserName() {
23-
// return "降级信息";
22+
// public JSONObject getUserNameById() {
23+
// JSONObject resultJson = new JSONObject();
24+
// resultJson.put("id", "-1" );
25+
// resultJson.put("name", "null" );
26+
// return resultJson;
2427
// }
2528
// };
2629

0 commit comments

Comments
 (0)