DemoController.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import io.swagger.annotations.ApiParam;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.cloud.context.config.annotation.RefreshScope;
  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("/patient")
  23. @Api(description = "患者")
  24. @RefreshScope
  25. public class DemoController {
  26. private Logger logger = LoggerFactory.getLogger(DemoController.class);
  27. @Autowired
  28. private PatientFeign patientFegin;
  29. @Autowired
  30. private Tracer tracer;
  31. // @Value("${test.aaa}")
  32. private String aaaa;
  33. @GetMapping("/hello")
  34. @ResponseBody
  35. public Envelop hello1(@RequestParam(name = "id") Integer id,
  36. @RequestParam(name = "name") String name,
  37. HttpServletRequest request
  38. ) throws Exception {
  39. tracer.getCurrentSpan().logEvent("logEvent");
  40. tracer.getCurrentSpan().tag("test","tag");
  41. tracer.getCurrentSpan().setBaggageItem("test","BaggageItem");
  42. switch (id) {
  43. case 1: {
  44. throw new ManageException("后台管理系统异常");
  45. }
  46. case 2: {
  47. throw new SecurityException("权限异常");
  48. }
  49. case 3: {
  50. throw new SystemException("后台系统异常");
  51. }
  52. }
  53. return Envelop.getSuccess("请求成功");
  54. }
  55. @GetMapping("/hello")
  56. @ResponseBody
  57. public String hello2(String id) throws Exception {
  58. System.out.println("haha2.........");
  59. return "hello2";
  60. }
  61. @GetMapping("/refresh")
  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. }