如何使用自定义 Mule ESB 路由器保留入站主机

发布于 2024-08-18 11:45:34 字数 3176 浏览 4 评论 0原文

我创建了一个具有一个端点的自定义路由器。自定义路由器根据入站 URL 的 URL 参数查找端点的目标。我有一个正在运行的示例,我正在浏览器中对其进行测试。我正在尝试解决最后一件事。当我使用 http://localhost:8787/my-site 在浏览器中进行调用时,调用进行重定向,浏览器中的 URL 更改为 http://server2.xyz.com: 8080/我的网站。我不希望用户看到 http://server2.xyz.com:8080/我的网站。我希望用户始终看到 http://localhost:8787/my-site。我怎样才能实现这个目标?我正在使用 Mule 2.2.1 社区版和 Java 1.6。

这是我的 Mule 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:spring="http://www.springframework.org/schema/beans"
    xmlns:http="http://www.mulesource.org/schema/mule/http/2.2"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
        http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd">

    <model name="ProxyService">
        <service name="HttpProxyService">
            <inbound>
                <http:inbound-endpoint address="http://localhost:8787" synchronous="true"/>
            </inbound>
            <outbound>
                <custom-outbound-router class="com.abc.xyz.routing.LookupOutboundRouter">
                    <outbound-endpoint name="custom" address="http://nonexistant.server.com:8080" synchronous="true"/>
                </custom-outbound-router>
            </outbound>
        </service>
    </model>
</mule>

这是我的自定义路由器:

public class LookupOutboundRouter extends AbstractOutboundRouter {
 Logger logger = Logger.getLogger(LookupOutboundRouter.class);

 @Override
 public boolean isMatch(MuleMessage message) throws MessagingException {
  return true;
 }

 @Override
 public MuleMessage route(MuleMessage message, MuleSession session) throws MessagingException {
  String[] urlValues = StringUtils.split(message.getProperty("http.request").toString(), "/");

  String newUri = lookupServiceUri(urlValues[0]) + urlValues[1];
  logger.info("newUri=" + newUri);

  DynamicURIOutboundEndpoint ep;

  try {
   ep = new DynamicURIOutboundEndpoint((OutboundEndpoint) getEndpoints().get(0), new MuleEndpointURI(newUri));

   MuleMessage message2 = send(session, message, ep);

   return message2;
  } catch (EndpointException e1) {
   e1.printStackTrace();
  } catch (MuleException e) {
   e.printStackTrace();
  }

  return null;
 }

 /**
  * This will call the service registry.
  * @param id
  * @return
  */
 private String lookupServiceUri(String id) {
  if(id.equalsIgnoreCase("12345")) {
   return "http://server.xyz.com:8080/";
  } else {
   return "http://server2.xyz.com:8080/";
  }
 }
}

I created a custom router with one endpoint. The custom router looks up the destination of the endpoint based on the URL parameters of the inbound URL. I have an example of this up and running, and I am testing it out in a browser. I am trying to solve one last thing with this. When I make the call in the browser using http://localhost:8787/my-site, the call makes a redirect and the URL in the browser changes to http://server2.xyz.com:8080/my-site. I don't want the user to ever see http://server2.xyz.com:8080/my-site. I want the user to always see http://localhost:8787/my-site. How can I achieve this? I am using Mule 2.2.1 community edition with Java 1.6.

Here is my Mule configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:spring="http://www.springframework.org/schema/beans"
    xmlns:http="http://www.mulesource.org/schema/mule/http/2.2"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
        http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd">

    <model name="ProxyService">
        <service name="HttpProxyService">
            <inbound>
                <http:inbound-endpoint address="http://localhost:8787" synchronous="true"/>
            </inbound>
            <outbound>
                <custom-outbound-router class="com.abc.xyz.routing.LookupOutboundRouter">
                    <outbound-endpoint name="custom" address="http://nonexistant.server.com:8080" synchronous="true"/>
                </custom-outbound-router>
            </outbound>
        </service>
    </model>
</mule>

Here is my custom router:

public class LookupOutboundRouter extends AbstractOutboundRouter {
 Logger logger = Logger.getLogger(LookupOutboundRouter.class);

 @Override
 public boolean isMatch(MuleMessage message) throws MessagingException {
  return true;
 }

 @Override
 public MuleMessage route(MuleMessage message, MuleSession session) throws MessagingException {
  String[] urlValues = StringUtils.split(message.getProperty("http.request").toString(), "/");

  String newUri = lookupServiceUri(urlValues[0]) + urlValues[1];
  logger.info("newUri=" + newUri);

  DynamicURIOutboundEndpoint ep;

  try {
   ep = new DynamicURIOutboundEndpoint((OutboundEndpoint) getEndpoints().get(0), new MuleEndpointURI(newUri));

   MuleMessage message2 = send(session, message, ep);

   return message2;
  } catch (EndpointException e1) {
   e1.printStackTrace();
  } catch (MuleException e) {
   e.printStackTrace();
  }

  return null;
 }

 /**
  * This will call the service registry.
  * @param id
  * @return
  */
 private String lookupServiceUri(String id) {
  if(id.equalsIgnoreCase("12345")) {
   return "http://server.xyz.com:8080/";
  } else {
   return "http://server2.xyz.com:8080/";
  }
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

游魂 2024-08-25 11:45:34

我可以通过在 HTTP 连接器上将 followRedirects 设置为 true 来在浏览器中实现此目的。现在唯一的问题是它不适用于 POST 重定向。我现在从 SoapUI 进行 SOAP 调用,而不是使用浏览器。

Entity enclosing requests cannot be redirected without user intervention

Message               : Failed to route event via endpoint: org.mule.endpoint.DynamicURIOutboundEndpoint@fd285ee0. Message payload is of type: PostMethod
Type                  : org.mule.api.transport.DispatchException
Code                  : MULE_ERROR-42999
Payload               : org.apache.commons.httpclient.methods.PostMethod@9fa8f
JavaDoc               : http://www.mulesource.org/docs/site/current2/apidocs/org/mule/api/transport/DispatchException.html

I was able to achieve this in the browser by setting followRedirects to true on the HTTP connector. The only issue with this now is that it does not work for POST redirects. I'm making a SOAP call from SoapUI now instead of using the browser.

Entity enclosing requests cannot be redirected without user intervention

Message               : Failed to route event via endpoint: org.mule.endpoint.DynamicURIOutboundEndpoint@fd285ee0. Message payload is of type: PostMethod
Type                  : org.mule.api.transport.DispatchException
Code                  : MULE_ERROR-42999
Payload               : org.apache.commons.httpclient.methods.PostMethod@9fa8f
JavaDoc               : http://www.mulesource.org/docs/site/current2/apidocs/org/mule/api/transport/DispatchException.html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文