PatientController.java 3.5 KB

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