您的位置:首页 > 理论基础 > 计算机网络

spring3.2.2 remoting HTTP invoker 实现方式

2013-11-14 16:27 316 查看
最近跟朋友聊天,聊到他们现在项目的架构都是把数据层跟应用层分离开来,中间可以加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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: