最近跟朋友聊天,聊到他们现在项目的架构都是把数据层跟应用层分离开来,中间可以加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 实现方式的更多相关文章

  1. 【Spring3.0系列】---Bean不同配置方式比较 和适用场合

    Bean不同配置方式比较1.基于XML配置定义:在XML文件中通过<bean>元素定义Bean,例如<bean class="com.bbt.UserDao"/& ...

  2. Spring3.0 入门进阶(三):基于XML方式的AOP使用

    AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...

  3. Spring Remoting: HTTP Invoker--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp Concept Overview ...

  4. 一步一步学Remoting系列文章

    转自:http://www.cnblogs.com/lovecherry/archive/2005/05/24/161437.html (原创)一步一步学Remoting之一:从简单开始(原创)一步一 ...

  5. 【Spring】web开发 javaConfig方式 图解

    spring3.2之后开始支持java配置方式开发web项目,不使用web.xml,但需要在servlet3.0环境,一般tomcat7会支持,6不行 下图中:MyAppInitializer和Spr ...

  6. Microsoft .Net Remoting系列专题之二

    Microsoft .Net Remoting系列专题之二 一.远程对象的激活 在Remoting中有三种激活方式,一般的实现是通过RemotingServices类的静态方法来完成.工作过程事实上是 ...

  7. .NET高级代码审计(第五课) .NET Remoting反序列化漏洞

    0x00 前言 最近几天国外安全研究员Soroush Dalili (@irsdl)公布了.NET Remoting应用程序可能存在反序列化安全风险,当服务端使用HTTP信道中的SoapServerF ...

  8. 【转】Microsoft .Net Remoting之Marshal、Disconnect与生命周期以及跟踪服务

    Marshal.Disconnect与生命周期以及跟踪服务 一.远程对象的激活 在Remoting中有三种激活方式,一般的实现是通过RemotingServices类的静态方法来完成.工作过程事实上是 ...

  9. Remoting在多IP多网卡内外网环境下的问题

    Remoting服务器端如果服务器有多块网卡,多个IP地址的情况下会出现客户端callback失败的问题,debug以后发现客户端会callback到服务器端另外一个IP地址(例如外网地址,而不是内网 ...

随机推荐

  1. (线性回归)Liner Regression简单应用

    警告:本文为小白入门学习笔记 数据连接: http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearni ...

  2. gulp API 文档

    gulp.src(globs[, options]) 输出(Emits)符合所提供的匹配模式(glob)或者匹配模式的数组(array of globs)的文件. 将返回一个 Vinyl files  ...

  3. python3 aes加解密

    # encoding: utf-8 import xlrd import os import yaml import logging.config from Crypto.Cipher import ...

  4. MySQL高可用架构之Mycat-关于Mycat安装和参数设置详解

    MySQL高可用架构之Mycat-关于Mycat安装和参数设置详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Mycat介绍 1>.什么是Mycat Mycat背后是 ...

  5. SQL语法基础之CREATE语句

    SQL语法基础之CREATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.使用“?”来查看MySQL命令的帮助信息 mysql> ? CR ...

  6. Centos7安装Openldap初级篇

    openldap 单节点编译安装 1.获取源码包 #下载Berkeley DB www.oracle.com/technetwork/database/database-technologies/be ...

  7. JVM 启动参数及原理 转

    Java虚拟机(JVM)是Java应用的运行环境,从一般意义上来讲,JVM是通过规范来定义的一个虚拟的计算机,被设计用来解释执行从Java源码编译而来的字节码.更通俗地说,JVM是指对这个规范的具体实 ...

  8. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] 学习

    http://www.cnblogs.com/jjg0519/p/6707513.html

  9. 阅读:ECMAScript 6 入门(2)

    参考 ECMAScript 6 入门 ES6新特性概览 ES6 全套教程 ECMAScript6 (原著:阮一峰) JavaScript 教程 重新介绍 JavaScript(JS 教程) Modul ...

  10. HDU 1039(字符串判断 **)

    题意是检查一个字符串是否满足三个条件: 一.至少有一个元音字母.二.不能出现三个连续的元音或三个连续的辅音.三.除了 ee 和 oo 外不能出现两个连续相同字母. 若三个条件都能满足,该字符串满足条件 ...