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. @Value("${test.aaa}")
  35. private String aaaa;
  36. @GetMapping("/hello")
  37. @ApiVersion(1)
  38. @ResponseBody
  39. public String hello1(Integer id) throws Exception {
  40. switch (id){
  41. case 1:{
  42. throw new ManageException("后台管理系统异常");
  43. }
  44. case 2:{
  45. throw new SecurityException("权限异常");
  46. }
  47. case 3:{
  48. throw new SystemException("后台系统异常");
  49. }
  50. }
  51. return "hello1";
  52. }
  53. @GetMapping("/hello")
  54. @ApiVersion(2)
  55. @ResponseBody
  56. public String hello2(HttpServletRequest request) throws Exception {
  57. System.out.println("haha2.........");
  58. return "hello2";
  59. }
  60. @GetMapping("/refresh")
  61. @ApiVersion(0)
  62. @ResponseBody
  63. public String refresh(HttpServletRequest request) throws Exception {
  64. return aaaa;
  65. }
  66. @ApiOperation(value = "根据code查找患者")
  67. @GetMapping(value = "findByCode")
  68. //配置HystrixProperty 则调用的方法和fallback是同一个线程 否则就不是
  69. //@HystrixCommand(fallbackMethod = "findByCodeFallback",commandProperties = @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE"))
  70. // @HystrixCommand(fallbackMethod = "findByCodeFallback" )
  71. @HystrixCommand(commandProperties = {
  72. @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "-1"),//超时时间
  73. @HystrixProperty(name = "execution.timeout.enabled", value = "false")})
  74. public String findByCode(
  75. @ApiParam(name = "code", value = "患者code", required = true) @RequestParam(value = "code", required = true) String code) {
  76. tracer.getCurrentSpan().logEvent("开始调用微服务查询患者");
  77. String text1 = patientFegin.findByCode(code);
  78. tracer.getCurrentSpan().logEvent("查询调用微服务找患者结束");
  79. return text1;
  80. }
  81. // /**
  82. // * 参数要一致 返回值类型也要一致
  83. // *
  84. // * @param code
  85. // * @return
  86. // */
  87. // public String findByCodeFallback(String code) {
  88. // return "启动断路器";
  89. // }
  90. }