DemoController.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.yihu.jw.controller;
  2. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  3. import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
  4. import com.yihu.jw.exception.SystemException;
  5. import com.yihu.jw.exception.business.ManageException;
  6. import com.yihu.jw.fegin.DemoFeign;
  7. import com.yihu.jw.restmodel.common.Envelop;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import io.swagger.annotations.ApiParam;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.cloud.context.config.annotation.RefreshScope;
  15. import org.springframework.cloud.sleuth.Tracer;
  16. import org.springframework.web.bind.annotation.*;
  17. import javax.servlet.http.HttpServletRequest;
  18. /**
  19. * Created by chenweida on 2017/5/10.
  20. */
  21. @RestController
  22. @RequestMapping("/demo")
  23. @Api(description = "demo例子")
  24. @RefreshScope
  25. public class DemoController {
  26. private Logger logger = LoggerFactory.getLogger(DemoController.class);
  27. @Autowired
  28. private DemoFeign patientFegin;
  29. @Autowired
  30. private Tracer tracer;
  31. // @Value("${test.aaa}")
  32. private String aaaa;
  33. @GetMapping("/hello")
  34. public Envelop hello1(@RequestParam(name = "id") Integer id,
  35. @RequestParam(name = "name") String name,
  36. HttpServletRequest request
  37. ) throws Exception {
  38. tracer.getCurrentSpan().logEvent("logEvent");
  39. tracer.getCurrentSpan().tag("test","tag");
  40. tracer.getCurrentSpan().setBaggageItem("test","BaggageItem");
  41. switch (id) {
  42. case 1: {
  43. throw new ManageException("后台管理系统异常");
  44. }
  45. case 2: {
  46. throw new SecurityException("权限异常");
  47. }
  48. case 3: {
  49. throw new SystemException("后台系统异常");
  50. }
  51. }
  52. return Envelop.getSuccess("请求成功");
  53. }
  54. @GetMapping("/hello2")
  55. public Envelop hello2(String id) throws Exception {
  56. return Envelop.getSuccess("hahaha");
  57. }
  58. @GetMapping("/refresh")
  59. public String refresh(HttpServletRequest request) throws Exception {
  60. return aaaa;
  61. }
  62. @ApiOperation(value = "根据code查找患者")
  63. @GetMapping(value = "findByCode")
  64. //配置HystrixProperty 则调用的方法和fallback是同一个线程 否则就不是
  65. //@HystrixCommand(fallbackMethod = "findByCodeFallback",commandProperties = @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE"))
  66. // @HystrixCommand(fallbackMethod = "findByCodeFallback" )
  67. @HystrixCommand(commandProperties = {
  68. @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "-1"),//超时时间
  69. @HystrixProperty(name = "execution.timeout.enabled", value = "false")})
  70. public String findByCode(
  71. @ApiParam(name = "code", value = "患者code", required = true) @RequestParam(value = "code", required = true) String code) {
  72. tracer.getCurrentSpan().logEvent("开始调用微服务查询患者");
  73. String text1 = patientFegin.findByCode(code);
  74. tracer.getCurrentSpan().logEvent("查询调用微服务找患者结束");
  75. return text1;
  76. }
  77. // /**
  78. // * 参数要一致 返回值类型也要一致
  79. // *
  80. // * @param code
  81. // * @return
  82. // */
  83. // public String findByCodeFallback(String code) {
  84. // return "启动断路器";
  85. // }
  86. }