File tree 15 files changed +170
-7
lines changed
java/com/example/demo/controller
main/java/com/example/demo
test/java/com/example/demo
15 files changed +170
-7
lines changed Original file line number Diff line number Diff line change 4
4
<modelVersion >4.0.0</modelVersion >
5
5
6
6
<groupId >com.example</groupId >
7
- <artifactId >demo </artifactId >
7
+ <artifactId >provider </artifactId >
8
8
<version >0.0.1-SNAPSHOT</version >
9
9
<packaging >jar</packaging >
10
10
30
30
<artifactId >spring-boot-starter-integration</artifactId >
31
31
</dependency >
32
32
33
+ <dependency >
34
+ <groupId >org.apache.httpcomponents</groupId >
35
+ <artifactId >httpcore</artifactId >
36
+ <version >4.4</version >
37
+ </dependency >
38
+
33
39
<dependency >
34
40
<groupId >mysql</groupId >
35
41
<artifactId >mysql-connector-java</artifactId >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -11,5 +11,5 @@ eureka:
11
11
client :
12
12
serviceUrl :
13
13
# 指定服务注册中心的位置
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/
Original file line number Diff line number Diff line change 47
47
<artifactId >spring-boot-starter-test</artifactId >
48
48
<scope >test</scope >
49
49
</dependency >
50
+
51
+ <dependency >
52
+ <groupId >org.projectlombok</groupId >
53
+ <artifactId >lombok</artifactId >
54
+ </dependency >
50
55
</dependencies >
51
56
52
57
<!-- 指定springCloud版本-->
Original file line number Diff line number Diff line change 1
1
package com .example .demo .controller ;
2
2
3
- import com .example .demo .feignInterface .DcClient ;
3
+ import com .example .demo .feignInterface .dc . DcClient ;
4
4
import org .springframework .beans .factory .annotation .Autowired ;
5
5
import org .springframework .web .bind .annotation .GetMapping ;
6
6
import org .springframework .web .bind .annotation .RestController ;
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 1
- package com .example .demo .feignInterface ;
1
+ package com .example .demo .feignInterface . dc ;
2
2
3
3
import org .springframework .stereotype .Component ;
4
4
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- package com .example .demo .feignInterface ;
1
+ package com .example .demo .feignInterface . dc ;
2
2
3
3
import org .springframework .cloud .netflix .feign .FeignClient ;
4
4
import org .springframework .web .bind .annotation .GetMapping ;
11
11
* 使用@FeignClient注解。其中的value指定这个接口所要调用的服务名称,fallback指定hystrix的回调类
12
12
* 接口中定义的各个函数使用Spring MVC的注解就可以来绑定服务提供方的REST接口
13
13
*/
14
- @ FeignClient (value = "eureka-client" ,fallback = ClientHystrix .class )
14
+ //@FeignClient(value = "eureka-client",fallback = ClientHystrix.class)
15
+ @ FeignClient (value = "eureka-client" ,fallbackFactory = ClientHystrixFallbackFactory .class )
15
16
public interface DcClient {
16
17
@ GetMapping (value = "/dc" )
17
18
String consumer ();
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments