PatientController.java 3.7 KB

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