GatewayRouterBuilder.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package camel.gateway.route;
  2. import camel.gateway.processor.ErrorHandle;
  3. import camel.gateway.processor.GatewayProcessor;
  4. import camel.gateway.processor.SignProcessor;
  5. import camel.gateway.processor.WsProcessor;
  6. import camel.log.TracerFormatter;
  7. import org.apache.camel.Exchange;
  8. import org.apache.camel.builder.RouteBuilder;
  9. import org.apache.camel.processor.interceptor.Tracer;
  10. import org.springframework.stereotype.Component;
  11. /**
  12. * @author HZY
  13. * @vsrsion 1.0
  14. * Created at 2017/3/13.
  15. */
  16. @Component
  17. public class GatewayRouterBuilder extends RouteBuilder {
  18. protected static final String SIMPLE_ENDPOINT_ADDRESS = "http://0.0.0.0:3333/ws/api";
  19. protected static final String SIMPLE_ENDPOINT_URI = "cxf:" + SIMPLE_ENDPOINT_ADDRESS
  20. + "?serviceClass=camel.gateway.processor.WsService"
  21. // + "&dataFormat=CXF_MESSAGE"
  22. ;
  23. @Override
  24. public void configure() throws Exception {
  25. this.getContext().setUseMDCLogging(true);
  26. this.getContext().setTracing(true);
  27. Tracer tracer = new Tracer();
  28. // DefaultTraceFormatter formatter = new DefaultTraceFormatter();
  29. TracerFormatter formatter = new TracerFormatter();
  30. formatter.setShowHeaders(true);
  31. formatter.setShowBody(true);
  32. formatter.setShowBodyType(true);
  33. formatter.setShowOutHeaders(true);
  34. formatter.setShowOutBody(true);
  35. formatter.setShowOutBodyType(true);
  36. tracer.setFormatter(formatter);
  37. this.getContext().addInterceptStrategy(tracer);
  38. this.getContext().getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "true");
  39. this.getContext().getProperties().put(Exchange.BEAN_MULTI_PARAMETER_ARRAY, "true");
  40. from("jetty:http://0.0.0.0:9998?matchOnUriPrefix=true").routeId("proxy")
  41. .to("jetty:http://192.168.1.221:10000?bridgeEndpoint=true&throwExceptionOnFailure=false");
  42. from("jetty:http://0.0.0.0:9999/api").routeId("api")
  43. .process(new GatewayProcessor())
  44. .routingSlip(method(GatewayProcessor.class, "route"));
  45. from("jetty:http://0.0.0.0:9999/error/{error}")
  46. .choice()
  47. .when(header(Exchange.HTTP_URI).contains("paramError")).bean(new ErrorHandle(), "paramError")
  48. .when(header(Exchange.HTTP_URI).contains("outdataError")).bean(new ErrorHandle(), "outdataError")
  49. .when(header(Exchange.HTTP_URI).contains("signValidError")).bean(new ErrorHandle(), "signValidError")
  50. .when(header(Exchange.HTTP_URI).contains("unauthorizedError")).bean(new ErrorHandle(), "unauthorizedError")
  51. .endChoice();
  52. from("jetty:http://0.0.0.0:9999/healthy").routeId("healthy")
  53. .log("=========================心跳测试=====================");
  54. from("jetty:http://0.0.0.0:9999/sign").routeId("sign")
  55. .process(new SignProcessor());
  56. from(SIMPLE_ENDPOINT_URI)
  57. .setHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY, constant(true))
  58. .process(new WsProcessor())
  59. .routingSlip(method(GatewayProcessor.class, "route"))
  60. .to("bean:wsProcessor?method=responseWs")
  61. ;
  62. }
  63. }