需求:完成一個URL形式的接口。
在URL中請求數據,服務器中轉數據到目標服務器A,返回從A中獲取的返回值。
開發工具:myeclipse, tomcat-7.0,
項目結構:
步驟:
1.用myeclipse引用spring3.1框架依賴;
2.編寫web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>projectnamenamename</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.可以看出對servlet-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.xxx.xx.*" />
<!-- 這個必須包含所有的package文件, 否則會可能會出現進不了Controler的錯誤!!!-->
<!-- <mvc:default-servlet-handler />
Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven/>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- <context:component-scan base-package="com.fanews.tn.action.*"/> -->
</beans>
4.編寫控制器–@部分別丟了
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/")
public class TNController {
/**
* 控制器
*/
@Autowired
TNService tnService;//引用服務器類
@RequestMapping("/hello")
public String hello(){
tnService.justSend();
return "hello";
}
}
5.編寫服務器類–@部分別丟了
import org.springframework.stereotype.Service;
@Service
public class TNService {
/**
* 處理器
*/
public void justSend(){
try {
ClientReq req = new ClientReq();
Client client = new Client("xxx.xxx.xxx.xxx", 9998);
client.connect(req);
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.編寫netty處理類。–客戶端
·Client
·ClientHandler
·MarshallingCodeCFactory
7.於是可以作爲一個web項目放到tomcat裏面進行運行/調試了。
URL:localhost:8080/”projectName”/hello.action
期間碰到的問題及解決:
Client發送消息,但是沒有得到Server的響應
分析:
在編程上的編碼格式是沒有問題的Marshalling;
在小程序單跑是能得到響應的;
方案:
1.調試程序,研究比較tomcat和單跑 數據的差異;
2.抓包進行分析。
@
選擇方案1-由於走TCP,用wireshark進行獲取,發現Client都發送了數據,但是bytes不同(或許有其他的差異,但是目前只能從中看懂這個),Server端單跑接收到數據,但是tomcat端接收不到;
@
於是,繼續做一個單跑的程序進行測試。換了包名Client發送的bytes不同,而且也無法得到反饋——發現 包名的不同造成的結果。
事後:
不知道包名爲什麼會混到Client發送的信息裏,要知道原因得去研究Netty源碼了吧。。。
(很佩服前輩能在調試Netty源碼的時候找到數據不同的關鍵方法,真是大神啊。。)