File tree 14 files changed +289
-70
lines changed
src/main/java/com/example/demo
14 files changed +289
-70
lines changed Original file line number Diff line number Diff line change 53
53
</dependency >
54
54
55
55
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
+
56
67
</dependencies >
57
68
58
69
<dependencyManagement >
Original file line number Diff line number Diff line change 1
1
package com .example .demo .controller ;
2
2
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 .*;
5
6
6
7
@ RestController
7
8
public class UserController {
8
9
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" );
13
15
}
14
16
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
+ }
16
22
17
23
}
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 52
52
<groupId >org.projectlombok</groupId >
53
53
<artifactId >lombok</artifactId >
54
54
</dependency >
55
+
56
+ <dependency >
57
+ <groupId >com.alibaba</groupId >
58
+ <artifactId >fastjson</artifactId >
59
+ <version >1.2.54</version >
60
+ </dependency >
55
61
</dependencies >
56
62
63
+
64
+
57
65
<!-- 指定springCloud版本-->
58
66
<dependencyManagement >
59
67
<dependencies >
Original file line number Diff line number Diff line change 9
9
* Created by lenovo on 九月
10
10
*/
11
11
@ RestController
12
- public class DcController {
12
+ public class ConsumerDcController {
13
13
14
14
@ Autowired
15
15
private DcClient dcClient ;
Original file line number Diff line number Diff line change
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
+
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11
11
* 使用@FeignClient注解。其中的value指定这个接口所要调用的服务名称,fallback指定hystrix的回调类
12
12
* 接口中定义的各个函数使用Spring MVC的注解就可以来绑定服务提供方的REST接口
13
13
*/
14
- //@FeignClient(value = "eureka-client",fallback = ClientHystrix.class)
15
- @ FeignClient (value = "eureka-client" ,fallbackFactory = ClientHystrixFallbackFactory .class )
14
+ @ FeignClient (value = "eureka-client" ,fallback = ClientHystrix .class )
16
15
public interface DcClient {
17
16
@ GetMapping (value = "/dc" )
18
17
String consumer ();
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
package com .example .demo .feignInterface .user ;
2
2
3
+ import com .alibaba .fastjson .JSONObject ;
4
+ import com .example .demo .pojo .User ;
3
5
import lombok .extern .slf4j .Slf4j ;
4
6
import org .springframework .stereotype .Component ;
5
7
6
8
@ Component
7
- @ Slf4j
8
9
public class UserClientFallbackFactory implements UserFeignClient {
9
10
11
+ /**
12
+ * 服务熔断
13
+ * @param id
14
+ * @return
15
+ */
10
16
@ 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 ;
14
24
}
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
+
15
36
}
Original file line number Diff line number Diff line change 1
1
package com .example .demo .feignInterface .user ;
2
2
3
+ import com .alibaba .fastjson .JSONObject ;
4
+ import com .example .demo .pojo .User ;
3
5
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 ;
5
9
6
10
7
11
/**
10
14
@ FeignClient (value = "eureka-client" ,fallbackFactory = UserHystrixFallbackFactory .class )
11
15
public interface UserFeignClient {
12
16
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 );
15
22
}
Original file line number Diff line number Diff line change @@ -13,14 +13,17 @@ public class UserHystrixFallbackFactory implements FallbackFactory<UserFeignCli
13
13
14
14
@ Override
15
15
public UserFeignClient create (Throwable cause ) {
16
- log . info ( " fallback reason was: {} " , cause .getMessage ());
16
+ System . out . println ( "====================================》 fallback reason was: " + cause .getMessage ());
17
17
18
18
return new UserClientFallbackFactory ();
19
- //也可以不写UserClientFallbackFactory类,直接写成以下形式 :
20
- // return new UserFeignClient() {
19
+ //也可以不写UserClientFallbackFactory类,直接用匿名对象写成以下形式 :
20
+ // return new UserFeignClient(Integer id ) {
21
21
// @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;
24
27
// }
25
28
// };
26
29
You can’t perform that action at this time.
0 commit comments