spring3.2.2 remoting HTTP invoker 实现方式
最近跟朋友聊天,聊到他们现在项目的架构都是把数据层跟应用层分离开来,中间可以加memcached等的缓存系统,感觉挺好的,很大程度上的降低耦合,然后还明确分配了数据层跟应用层任务。也方便定位、找到问题。(我们都用最简单的架构,就没搞过分布式部署,小公司没办法o(︶︿︶)o),就找时间学习了,说不定以后就好应用上。这里用了 HTTP invoker方式,别的rmi或者jms等也大同小异。
这里我使用的是spring3.2.2,jar包就不列了,少哪个加哪个就可以了。
spring官方文档一共提供三种方式:通过Spring Web MVC,通过一个servlet指向跟不依赖web容器使用Sun's Java 6构建。我这里用的是第二种方式,别的官方文档讲解还是很清晰的,根据那个操作即可。
首先server端:
Model
model是需要序列化的才能remote传输
public class ServiceReso implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String serviceName;
    private String serviceAddress;
    public ServiceReso() {
        super();
    }
    public ServiceReso(String id, String serviceName, String serviceAddress) {
        super();
        this.id = id;
        this.serviceName = serviceName;
        this.serviceAddress = serviceAddress;
    }
    public final String getId() {
        return id;
    }
    public final void setId(String id) {
        this.id = id;
    }
    public final String getServiceName() {
        return serviceName;
    }
    public final void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    public final String getServiceAddress() {
        return serviceAddress;
    }
    public final void setServiceAddress(String serviceAddress) {
        this.serviceAddress = serviceAddress;
    }
    @Override
    public String toString() {
        return "{\"id\":\"" + this.id + "\",\"serviceName\":\""
                + this.serviceName + "\",\"serviceAddress\":\""
                + this.serviceAddress + "\"}";
    }
}
Dao
public interface ServiceResoDao {
    /**
     * 根据传入的id值返回ServiceReso对象
     *
     * @param id
     *            需要查询的ServiceReso对象id
     * @return ServiceReso
     */
    public ServiceReso find(String id);
}
DaoImp
jdbc没有做持久化,采用了spring自带的JdbcTemplate,感觉还是挺好用的。
@Repository("serviceResoDao")
public class ServiceResoDaoImp implements ServiceResoDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public ServiceReso find(String id) {
        String sql = "select SERVERID,REALSERVERNAME,DSIPADDR from COSH_SERVICE_REGISTER where SERVERID=?";
        ServiceReso serviceReso = jdbcTemplate.queryForObject(sql,
                new Object[] { id }, new RowMapper<ServiceReso>() {
                    public ServiceReso mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        ServiceReso serviceReso = new ServiceReso(rs
                                .getString("SERVERID"), rs
                                .getString("REALSERVERNAME"), rs
                                .getString("DSIPADDR"));
                        return serviceReso;
                    }
                });
        return serviceReso;
    }
}
beans.xml:
这里的urlMapping是用来分发不同的请求,免得在servlet中对应每个bean。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="com.blackbread" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="springDSN"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}">
</property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default">
<property name="dataSource">
<ref bean="springDSN" />
</property>
</bean>
<bean name="serviceResoDaoRemoting"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="serviceResoDao" />
<property name="serviceInterface"
value="com.blackbread.dao.ServiceResoDao" />
</bean>
<bean name="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/serviceResoDaoRemoting">
serviceResoDaoRemoting
</prop>
</props>
</property>
</bean>
</beans>
web.xml
这里有个问题:servlet-mapping中的url-pattern如果不是这样写,而是改成/remoting/*之类的就会请求不到资源,望知道的兄弟告知下,不胜感激。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
client端:
client需要将server端的接口类跟实体类打包成jar,加以引用。
service
public interface ServiceResoService {
    void getServiceReso(String id);
}
serviceImp
@Controller("serviceResoService")
public class ServiceResoServiceImp implements ServiceResoService {
    @Resource(name = "serviceResoDaoReomting")
    ServiceResoDao serviceResoDao;
    public void getServiceReso(String id) {
        ServiceReso serviceReso;
        try {
            serviceReso = serviceResoDao.find(id);
            System.out.println(serviceReso.toString());
        } catch (RuntimeException e) {
            System.out.println("未找到结果!");
        }
    }
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="com.blackbread" />
<bean id="serviceResoDaoReomting"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://localhost:8080/HttpInvokerDAO/serviceResoDaoRemoting" />
<property name="serviceInterface"
value="com.blackbread.dao.ServiceResoDao" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:beans.xml</param-value>
</context-param>
</web-app>
spring3.2.2 remoting HTTP invoker 实现方式的更多相关文章
- 【Spring3.0系列】---Bean不同配置方式比较 和适用场合
		
Bean不同配置方式比较1.基于XML配置定义:在XML文件中通过<bean>元素定义Bean,例如<bean class="com.bbt.UserDao"/& ...
 - Spring3.0 入门进阶(三):基于XML方式的AOP使用
		
AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...
 - Spring Remoting: HTTP Invoker--转
		
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp Concept Overview ...
 - 一步一步学Remoting系列文章
		
转自:http://www.cnblogs.com/lovecherry/archive/2005/05/24/161437.html (原创)一步一步学Remoting之一:从简单开始(原创)一步一 ...
 - 【Spring】web开发 javaConfig方式 图解
		
spring3.2之后开始支持java配置方式开发web项目,不使用web.xml,但需要在servlet3.0环境,一般tomcat7会支持,6不行 下图中:MyAppInitializer和Spr ...
 - Microsoft .Net Remoting系列专题之二
		
Microsoft .Net Remoting系列专题之二 一.远程对象的激活 在Remoting中有三种激活方式,一般的实现是通过RemotingServices类的静态方法来完成.工作过程事实上是 ...
 - .NET高级代码审计(第五课) .NET Remoting反序列化漏洞
		
0x00 前言 最近几天国外安全研究员Soroush Dalili (@irsdl)公布了.NET Remoting应用程序可能存在反序列化安全风险,当服务端使用HTTP信道中的SoapServerF ...
 - 【转】Microsoft .Net Remoting之Marshal、Disconnect与生命周期以及跟踪服务
		
Marshal.Disconnect与生命周期以及跟踪服务 一.远程对象的激活 在Remoting中有三种激活方式,一般的实现是通过RemotingServices类的静态方法来完成.工作过程事实上是 ...
 - Remoting在多IP多网卡内外网环境下的问题
		
Remoting服务器端如果服务器有多块网卡,多个IP地址的情况下会出现客户端callback失败的问题,debug以后发现客户端会callback到服务器端另外一个IP地址(例如外网地址,而不是内网 ...
 
随机推荐
- 使用postman测试hystrix
			
当在浏览器发送多次请求检测hystrix的作用时,我们可以使用postman来自动发送多次请求: 1.将链接保存到一个collection中 2.点击runner 3.设定运行次数
 - (贪心)nyoj91-阶乘之和
			
91-阶乘之和 内存限制:64MB 时间限制:3000ms 特判: No 通过数:71 提交数:191 难度:3 题目描述: 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为正数) ...
 - 使用PreparedStatement  查询一条数据   封装成一个学生的Student1对象
			
package cn.lijun.entity; public class Student1 { private int id; private String sname; private int g ...
 - 8款压箱底的Mac屏幕截图和录音录像工具软件,请你务必低调使用
			
以下几款是是Mac上优秀的屏幕截图.录像和录音工具,有了这些工具,在Mac上进行截屏.录制视频或者录音都会事半功倍. 1. Snagit Mac上最好用最强大的屏幕截图工具,支持各种方式的屏幕截图以及 ...
 - CodeForces999E  双dfs // 标记覆盖 // tarjan缩点
			
http://codeforces.com/problemset/problem/999/E 题意 有向图 给你n个点,m条边,以及一个初始点s,问你至少还需要增加多少条边,使得初始点s与剩下其 ...
 - Oracle分析函数-rank() over(partition by...order by...)
			
select *from ( SELECT t.s#,---学号 t.c#,---课程号 T.SCCORE, ---成绩 RANK() OVER(PARTITION BY t.c# ORDER BY ...
 - [JDK8] Lambda
			
本文转载原文http://www.cnblogs.com/jalja/p/7655170.html 一.使用线程 public static void main(String[] args) { // ...
 - 使用git 上传项目到gitee/github
			
参考: https://blog.csdn.net/qq944639839/article/details/79864081 注意:在此之前需要设置ssh公匙 详见:Github/github 初始化 ...
 - 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素
			
83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...
 - HDU - 3974 Assign the task (线段树区间修改+构建模型)
			
https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T ...