PatientController.java 3.1 KB

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