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地址(例如外网地址,而不是内网 ...
随机推荐
- 网络I/O模型总结
把网络IO模型整理了一下,如下图
- STM32L011D4 ----- 低功耗
After resuming from STOP the clock configuration returns to its reset state (MSI, HSI16 or HSI16/4 u ...
- #1479 : 三等分(树形DP)
http://hihocoder.com/problemset/problem/1479 #1479 : 三等分 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最 ...
- 2017-12-18python全栈9期第三天第二节之str常用操作方法及for循环之判断字母数字组成
#!/user/bin/python# -*- coding:utf-8 -*-name = 'zd123'print(name.isalnum()) #由数字或字母组成print(name.isal ...
- Docker CE 各安装方法
1.Docker CE 镜像源站 使用官方安装脚本自动安装 curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun 2.U ...
- C++ Random 的使用
1.rand() 方法 rand()不需要参数,它会返回一个从0到最大随机数的任意整数,最大随机数的大小通常是固定的一个大整数. 这样,如果你要产生0~10的10个整数,可以表达为: int N ...
- 设计模式---接口隔离模式之中介者模式(Mediator)
一:概念 在Mediator模式中,类之间的交互行为被统一放在Mediator的对象中,对象通过Mediator对象同其他对象交互.Mediator对象起到控制器的作用 二:动机 在软件构建的过程中, ...
- 【leetcode-66】 加一
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入 ...
- js方法参数中含有单引号双引号的处理
最近在做项目时,遇到一个问题.当在js脚本中,拼接生成一个tr,然后添加到一个表格里. //假定testval是从后台传过来的数据 var testval = "含有'半角单引号的字符串&q ...
- C语言宏定义##连接符和#符的使用
1. 关于宏(Macro) 属于编译器预处理的范畴,属于编译器概念(而非运行期概念). 2. 关于# #的功能:是 将其后面的宏参数进行 字符串化操作(Stringfication),即:在对它所引用 ...