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地址(例如外网地址,而不是内网 ...
随机推荐
- sqlserver Timeout 时间已到。在操作完成之前超时时间已过或服务器未响应
随着数据库数据的不断增大,查询时间也随之增长.今天在之前一个项目中执行数据库查询超过30秒就报“Timeout 时间已到.在操作完成之前超时时间已过或服务器未响应.”了,网上找了些文章,是在.co ...
- (二叉树 BFS) leetcode513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- (BFS/DFS) leetcode 200. Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- mongodb的sql日志
在Yii2中是没有打印出mongodb的sql语句,故借用下log来查看吧. 在网上有说可以使用$model->find()->createCommand()->getRawSql( ...
- Java测试Junit
Junit就是做测试用的,想想平常我们是怎么测试我们的方法或者类的,是不是在main方法里面去调用?这样有缺点: 1.每次都要在main方法里面写测试,假如我要上线新系统,里面有1000个方法需要测试 ...
- 11.享元模式(Flyweight Pattern)
面向对象的代价 面向对象很好地解决了系统抽象性的问题,同时在大多数情况下,也不会损及系统的性能.但是,在某些特殊的应用中下,由于对象的数量太大,采用面向对象会给系统带来难以承受的内存开销.比如: ...
- gcc编译出现dlopen、dlerror、dlsym、dlcolse的解决方法
➜ test_sqlite3 gcc *.c -I . -o xixi -pthread /tmp/cckGKTrr.o: In function `unixDlOpen': sqli ...
- 【1】Java中double转BigDecimal的注意事项
项目遇到该问题 先上结论:不要直接用double变量作为构造BigDecimal的参数. 线上有这么一段Java代码逻辑: 1,接口传来一个JSON串,里面有个数字:57.3. 2,解析JSON并把这 ...
- Baidu地图Map api直接加https不起作用
这种情况一般出现在v1.1等老版本上,加s=1也不起作用,尽量使用新版本. https://api.map.baidu.com/api?v=2.0&ak=你的密钥&s=1:
- idea JRebe插件激活方法
具体方法请看创始人博客及github