|
|
@@ -1,6 +1,10 @@
|
|
|
package cn.reghao.tnb.user.app.controller;
|
|
|
|
|
|
import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import cn.reghao.tnb.user.api.dto.UserInfo;
|
|
|
+import cn.reghao.tnb.user.app.hystrix.SimpleHystrixCommand;
|
|
|
+import cn.reghao.tnb.user.app.hystrix.observable.HaloSemaphoreIsolationCommand;
|
|
|
+import cn.reghao.tnb.user.app.hystrix.observable.HaloThreadIsolationCommand;
|
|
|
import cn.reghao.tnb.user.app.service.UserServiceHystrix;
|
|
|
import cn.reghao.tnb.user.app.service.UserProfileService;
|
|
|
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
|
|
|
@@ -11,6 +15,7 @@ import io.swagger.v3.oas.annotations.Operation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import rx.Observable;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
@@ -26,14 +31,40 @@ import java.util.Map;
|
|
|
public class UserControllerHystrix {
|
|
|
private final UserProfileService userProfileService;
|
|
|
private final UserServiceHystrix userServiceHystrix;
|
|
|
+ private HaloSemaphoreIsolationCommand haloSemaphoreIsolationCommand;
|
|
|
+ private HaloThreadIsolationCommand haloThreadIsolationCommand;
|
|
|
|
|
|
- public UserControllerHystrix(UserProfileService userProfileService, UserServiceHystrix userServiceHystrix) {
|
|
|
+ public UserControllerHystrix(UserProfileService userProfileService, UserServiceHystrix userServiceHystrix,
|
|
|
+ HaloSemaphoreIsolationCommand haloSemaphoreIsolationCommand,
|
|
|
+ HaloThreadIsolationCommand haloThreadIsolationCommand) {
|
|
|
this.userProfileService = userProfileService;
|
|
|
this.userServiceHystrix = userServiceHystrix;
|
|
|
+ this.haloSemaphoreIsolationCommand = haloSemaphoreIsolationCommand;
|
|
|
+ this.haloThreadIsolationCommand = haloThreadIsolationCommand;
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "获取用户资料", description = "N")
|
|
|
@GetMapping(value = "/info", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String getUserInfo(@RequestParam("userId") Long userId) {
|
|
|
+ UserHolder.setUser("reghao");
|
|
|
+ userServiceHystrix.hello();
|
|
|
+ log.info("user -> {}", UserHolder.getUser());
|
|
|
+
|
|
|
+ /*Observable<String> observable = haloThreadIsolationCommand.toObservable();
|
|
|
+ String result = observable.toBlocking().single();*/
|
|
|
+
|
|
|
+ /*Observable<String> observable1 = haloSemaphoreIsolationCommand.toObservable();
|
|
|
+ String result1 = observable1.toBlocking().single();*/
|
|
|
+
|
|
|
+ /*SimpleHystrixCommand simpleHystrixCommand = new SimpleHystrixCommand(userServiceHystrix);
|
|
|
+ String result2 = simpleHystrixCommand.execute();*/
|
|
|
+
|
|
|
+ //UserInfo userInfo = userProfileService.getUserInfo(userId);
|
|
|
+ String userInfoJson = userServiceHystrix.getUserInfo(userId);
|
|
|
+ return userInfoJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/info1", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
@HystrixCommand(
|
|
|
fallbackMethod = "getUserInfoFallback",
|
|
|
commandProperties = {
|
|
|
@@ -45,21 +76,20 @@ public class UserControllerHystrix {
|
|
|
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage", value="50"),
|
|
|
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds", value="3000")
|
|
|
})
|
|
|
- public String getUserInfo(@RequestParam("userId") Long userId) {
|
|
|
+ public UserInfo getUserInfo1(@RequestParam("userId") Long userId) {
|
|
|
UserHolder.setUser("reghao");
|
|
|
userServiceHystrix.hello();
|
|
|
log.info("user -> {}", UserHolder.getUser());
|
|
|
- //UserInfo userInfo = userProfileService.getUserInfo(userId);
|
|
|
- String userInfoJson = userServiceHystrix.getUserInfo(userId);
|
|
|
- return userInfoJson;
|
|
|
+ UserInfo userInfo = userServiceHystrix.getUserInfo1(userId);
|
|
|
+ return userInfo;
|
|
|
}
|
|
|
|
|
|
- public String getUserInfoFallback(Long userId) {
|
|
|
+ public UserInfo getUserInfoFallback(Long userId) {
|
|
|
System.out.printf("hystrix fallback with userId %s\n", userId);
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
map.put("code", "0");
|
|
|
map.put("msg", "hystrix fallback");
|
|
|
- return JsonConverter.objectToJson(map);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
public String defaultFallback() {
|