Skip to content

Commit 6aaca8b

Browse files
FeignClient的fallbackFactory属性,服务降级。
1 parent f9b8004 commit 6aaca8b

File tree

15 files changed

+170
-7
lines changed

15 files changed

+170
-7
lines changed

eureka-client/pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.example</groupId>
7-
<artifactId>demo</artifactId>
7+
<artifactId>provider</artifactId>
88
<version>0.0.1-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010

@@ -30,6 +30,12 @@
3030
<artifactId>spring-boot-starter-integration</artifactId>
3131
</dependency>
3232

33+
<dependency>
34+
<groupId>org.apache.httpcomponents</groupId>
35+
<artifactId>httpcore</artifactId>
36+
<version>4.4</version>
37+
</dependency>
38+
3339
<dependency>
3440
<groupId>mysql</groupId>
3541
<artifactId>mysql-connector-java</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.demo.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class UserController {
8+
9+
@GetMapping("/user/name")
10+
public void getUserName( ) throws Exception {
11+
System.out.println("==================================================================>getUserName( )");
12+
throw new Exception("getUserNameException");
13+
}
14+
15+
16+
17+
}

eureka-client/src/main/resources/application.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ eureka:
1111
client:
1212
serviceUrl:
1313
# 指定服务注册中心的位置
14-
# defaultZone: http://localhost:1111/eureka/
15-
defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
14+
defaultZone: http://localhost:1111/eureka/
15+
# defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/

eureka-consumer-feign/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<artifactId>spring-boot-starter-test</artifactId>
4848
<scope>test</scope>
4949
</dependency>
50+
51+
<dependency>
52+
<groupId>org.projectlombok</groupId>
53+
<artifactId>lombok</artifactId>
54+
</dependency>
5055
</dependencies>
5156

5257
<!--指定springCloud版本-->

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.example.demo.controller;
22

3-
import com.example.demo.feignInterface.DcClient;
3+
import com.example.demo.feignInterface.dc.DcClient;
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.web.bind.annotation.GetMapping;
66
import org.springframework.web.bind.annotation.RestController;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.demo.controller;
2+
3+
import com.example.demo.feignInterface.user.UserFeignClient;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class UserController {
10+
11+
@Autowired
12+
private UserFeignClient userFeignClient;
13+
14+
@GetMapping("user/name")
15+
public String getUserName(){
16+
return userFeignClient.getUserName();
17+
}
18+
19+
}
20+
21+

eureka-consumer-feign/src/main/java/com/example/demo/feignInterface/ClientHystrix.java renamed to eureka-consumer-feign/src/main/java/com/example/demo/feignInterface/dc/ClientHystrix.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.demo.feignInterface;
1+
package com.example.demo.feignInterface.dc;
22

33
import org.springframework.stereotype.Component;
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.demo.feignInterface.dc;
2+
3+
import feign.hystrix.FallbackFactory;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
@Slf4j
9+
public class ClientHystrixFallbackFactory implements FallbackFactory<DcClient> {
10+
@Override
11+
public DcClient create(Throwable cause) {
12+
log.error(" fallback reason was: {} " , cause.getMessage());
13+
return new DcClientFallbackFactory();
14+
}
15+
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.demo.feignInterface;
1+
package com.example.demo.feignInterface.dc;
22

33
import org.springframework.cloud.netflix.feign.FeignClient;
44
import org.springframework.web.bind.annotation.GetMapping;
@@ -11,7 +11,8 @@
1111
* 使用@FeignClient注解。其中的value指定这个接口所要调用的服务名称,fallback指定hystrix的回调类
1212
* 接口中定义的各个函数使用Spring MVC的注解就可以来绑定服务提供方的REST接口
1313
*/
14-
@FeignClient(value = "eureka-client",fallback = ClientHystrix.class)
14+
//@FeignClient(value = "eureka-client",fallback = ClientHystrix.class)
15+
@FeignClient(value = "eureka-client",fallbackFactory = ClientHystrixFallbackFactory.class)
1516
public interface DcClient {
1617
@GetMapping(value = "/dc")
1718
String consumer();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.demo.feignInterface.dc;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class DcClientFallbackFactory implements DcClient {
7+
@Override
8+
public String consumer() {
9+
String test="DcClientFallbackFactory";
10+
return test;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.demo.feignInterface.user;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Slf4j
8+
public class UserClientFallbackFactory implements UserFeignClient {
9+
10+
@Override
11+
public String getUserName() {
12+
log.debug("=======================>UserClientFallbackFactoryTest");
13+
return "UserClientFallbackFactoryTest";
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.demo.feignInterface.user;
2+
3+
import org.springframework.cloud.netflix.feign.FeignClient;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
7+
/**
8+
* 使用fallbackFactory捕获异常,并进行服务降级。
9+
*/
10+
@FeignClient(value = "eureka-client",fallbackFactory = UserHystrixFallbackFactory.class)
11+
public interface UserFeignClient {
12+
13+
@GetMapping(value = "/user/name")
14+
String getUserName();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.demo.feignInterface.user;
2+
3+
import feign.hystrix.FallbackFactory;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* 服务降级,
9+
*/
10+
@Slf4j
11+
@Component
12+
public class UserHystrixFallbackFactory implements FallbackFactory<UserFeignClient> {
13+
14+
@Override
15+
public UserFeignClient create(Throwable cause) {
16+
log.info("fallback reason was: {} ", cause.getMessage());
17+
18+
return new UserClientFallbackFactory();
19+
//也可以不写UserClientFallbackFactory类,直接写成以下形式:
20+
// return new UserFeignClient() {
21+
// @Override
22+
// public String getUserName() {
23+
// return "降级信息";
24+
// }
25+
// };
26+
27+
}
28+
29+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.demo;
2+
3+
import com.example.demo.pojo.User;
4+
5+
public class UserTest {
6+
public static void main(String[] args) {
7+
User user=User.builder().id(2).name("lin").build();
8+
System.out.println(user.getId());
9+
}
10+
}

0 commit comments

Comments
 (0)