PatientController.java 3.7 KB

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