Spring之集成之Exposing services using HTTP invokers

本文详细介绍了Spring HTTP Invoker的使用方法,包括如何通过HttpInvokerServiceExporter暴露业务接口及如何在客户端通过HttpInvokerProxyFactoryBean调用这些接口。此外还讨论了HTTP调用的实现方式及其优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

与Burlap和Hessian相反,这些都是轻量级协议,使用其自带的灵活的序列化机制,Spring HTTP调用者使用标准的Java序列化机制通过HTTP暴露业务接口。如果你的参数和返回的类型很复杂,其不能通过Burlap和Hessian序列化,这是一个很大的优点(当选择一个远程技术的时候在下一章要考虑更多的细节)。


在高级选项中,Spring要不使用由JDK提供的标准机制,要不使用Apache的 HttpComponents执行HTTP调用。如果你需要使用更强大和容易使用的功能,就使用后者。


21.4.1 暴露业务对象


为一个对象设置HTTP调用者机制,与使用Hessian或Burlap非常相似。就比如Hessian支持提供的HessianServiceExporter,Spring的HttpInvoker支持提供了org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.


为暴露一个Spring Web MVC DispatcherServlet内的AccountService(上述提到的),下面的配置需要在分发的应用上下文中替换。


<bean name="/AccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    <property name="service" ref="accountService"/>
    <property name="serviceInterface" value="example.AccountService"/>
</bean>

这样的输出定义将通过DispatcherServlet的标准映射机制暴露,如同上述Hessian或Burlap解释的那样。


作为可选地,在你的根应用程序上下文中创建一个HttpInvokerServiceExporter(例如:在'WEB-INF/applicationContext.xml'中)


<bean name="accountExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    <property name="service" ref="accountService"/>
    <property name="serviceInterface" value="example.AccountService"/>
</bean>

另外,在web.xml中为这个输出定义一个对应的Servlet,其Servlet名字与目标输出者的bean名匹配。


<servlet>
    <servlet-name>accountExporter</servlet-name>
    <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>accountExporter</servlet-name>
    <url-pattern>/remoting/AccountService</url-pattern>
</servlet-mapping>

如果在一个Servlet容器外部运行并且使用oracle的Java 6 版本,那么你可以使用内建的HTTP服务实现。你可以一起配置SimpleHttpServerFactoryBean,如下面例子所示:


<bean name="accountExporter"
        class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter">
    <property name="service" ref="accountService"/>
    <property name="serviceInterface" value="example.AccountService"/>
</bean>

<bean id="httpServer"
        class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
    <property name="contexts">
        <util:map>
            <entry key="/remoting/AccountService" value-ref="accountExporter"/>
        </util:map>
    </property>
    <property name="port" value="8080" />
</bean>


21.4.2 在客户端联系业务


在客户端联系业务也与Hessian和Burlap是非常相似的。使用一个代理,Spring可以将你的调用转换为HTTP POST请求道URL,其指向暴露的业务。


<bean id="httpInvokerProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
    <property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/>
    <property name="serviceInterface" value="example.AccountService"/>
</bean>

如同之前提到的,你可以选择你想使用的客户端。默认地,HttpInvokerProxy使用了JDK的HTTP功能,但是了你也可以使用Apache的HttpComponents客户端来设置httpInvokerRequestExecutor属性

property name="httpInvokerRequestExecutor">
    <bean class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor"/>
</property>


### Spring Cloud 框架架构图与组件关系 Spring Cloud 是一种基于 Java 的分布式系统开发工具集,旨在帮助开发者快速构建微服务应用。其核心目标是通过一系列标准化的组件和服务来简化复杂的分布式系统的开发过程。 以下是 Spring Cloud 架构的主要组成部分及其相互之间的关系: #### 1. **Spring Cloud Config** - 提供集中化的外部配置管理支持,允许应用程序在不同环境中共享相同的代码库,同时加载不同的配置文件。 - 支持 Git 和 SVN 存储库作为后端存储解决方案,便于版本控制和协作[^2]。 #### 2. **Spring Cloud Bus** - 实现了一个轻量级的消息代理机制,用于广播状态更改(例如配置更新)到整个集群中的实例。 - 常见的应用场景包括动态刷新配置以及实时通知其他服务节点的状态变更[^1]。 #### 3. **Spring Cloud Netflix** - 这是一组由 Netflix 开发并被集成Spring 生态圈内的开源项目集合,主要包括 Eureka、Hystrix、Zuul 等子模块。 - **Eureka**: 注册中心,负责服务发现的功能; - **Ribbon**: 客户端负载均衡器; - **Feign**: HTTP 调用接口声明式的 RESTful Web Service Client; - **Hystrix**: 断路器模式实现类库,提供容错保护能力; - **Zuul**: API Gateway 层面网关服务器[^3]。 #### 4. **Spring Cloud Stream & Kafka/RabbitMQ Bindings** - 面向消息驱动型微服务体系设计的一种抽象层框架,屏蔽底层具体协议细节差异的同时增强了灵活性。 - 用户可以选择 RabbitMQ 或 Apache Kafka 作为实际传输媒介。 #### 5. **Spring Cloud Sleuth** - 主要用来追踪请求链路上各个阶段耗时情况的数据收集工具包。 - 结合 Zipkin 可视化展示调用路径上的性能瓶颈所在位置。 下面是关于这些主要部分如何组合在一起形成完整的 Spring Cloud 微服务平台的一个高层次逻辑结构示意图形描述: ```plaintext +-------------------+ | Load Balancer | (e.g., Ribbon, Zuul) +-------------------+ | v +-------------------+ | Clients | (Consumer Services using Feign/Hystrix etc.) +-------------------+ | v +-------------------+ | Discovery Server | (Service Registry like Eureka) +-------------------+ | v +-------------------+ | Provider Services | (Producer Applications exposing APIs) +-------------------+ ``` 上述图表展示了典型的客户端-服务器模型下各角色间的关系网络布局形式;其中还包括了诸如熔断处理(Hystrix)之类的额外防护措施以增强整体稳定性表现水平等方面的内容介绍说明文档链接地址如下所示: ```java // Example of a simple service discovery with Eureka client configuration. @SpringBootApplication @EnableDiscoveryClient public class MyApplication { public static void main(String[] args){ SpringApplication.run(MyApplication.class,args); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值