今天第三天,继续dubbo微服务环境搭建:
1. 新建module smbms-user-consume
2. 增加jar 包依赖
2.1)spring-webmvc (spring+spring web + spring mvc)
<!--spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
<!--servlet 包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- jstl 包 javaweb 的标签使用依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2.2)dubbo
<!-- dubbo 包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
2.3)zookeeper
<!--zookeeper 包-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
2.4)zkclient
<!-- zkclient 包-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
2.5)hessian
<!--hessian 包-->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
3. 编写控制器(spring mvc)--->UserController
package cn.kgc1803.smbms_user_consume.controller;
import cn.kgc1803.smbms_common.pojo.User;
import cn.kgc1803.smbms_common.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
//登陆
@RequestMapping(value = "/dologin.html",method = RequestMethod.POST)
public String dologin(User user, HttpSession session){
User u = userService.loginUser(user);
if (u!=null){//登陆失败
session.setAttribute("session_user",u);
return "index";
}else {//登陆成功
return "login";
}
}
}
4. 编写spring mvc 配置文件(spring-servlet)
配置文件的头部命名空间如下:
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
spring-servlet.xml配置文件:
4.1)配置springmvc扫描注入控制器
<!--配置springmvc扫描注入控制器-->
<context:component-scan base-package="cn.kgc1803.smbms_user_consume.controller"/>
4.2)配置注解驱动(解析控制器需求)
<!--配置注解驱动(解析控制器需求)-->
<mvc:annotation-driven/>
4.3)配置视图解析器(解析逻辑页面->jsp物理页面位置)
<!--配置视图解析器(解析逻辑页面->jsp物理页面位置)-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
4.4)配置静态资源
<!--静态资源配置-->
<mvc:resources mapping="/statics/**" location="/statics/"/>
5. 编写页面
login.jsp登陆页面:
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2018/12/24
Time: 15:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/user/dologin.html">
用户名:<input name="userCode">
密码:<input name="password">
<input type="submit" value="登陆">
</form>
</body>
</html>
index.jsp首页:
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2018/12/24
Time: 15:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
登陆成功,欢迎你:${session_user.userName}<br>
订单列表:
<ul>
</ul>
</body>
<script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/jquery-1.4.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/order.js"></script>
</html>
6. 配置web.xml
module中的web.xml设置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<!--启动之后直接进入首页-->
<welcome-file-list>
<welcome-file>/WEB-INF/view/login.jsp</welcome-file>
</welcome-file-list>
<!--spring mvc -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--启动spring mvc 配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
----------------------------以下都是dubbo 消费者的配置------------------------------
7. 编写dubbo的配置文件(消费者) dubbo_user_consume.xml
命名空间:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
7.1)应用的名称
<!--1)应用的名称-->
<dubbo:application name="smbms_user_consume"/>
7.2)注册中心
<!--注册中心地址-->
<dubbo:registry address="zookeeper://localhost:2181"/>
7.3)订阅服务
<!--订阅服务(有多少个服务,就需要写多少个订阅)-->
<dubbo:reference interface="cn.kgc1803.smbms_common.service.UserService" id="userService"/>
8. 配置smbms-user-consume的web.xml(dubbo)
8.1)启动dubbo 消费者配置文件
<!--启动dubbo-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:smbms_user_consume.xml</param-value>
</context-param>
8.2)让springmvc 与 dubbo 两个spring容器能互相访问
<!--让多个spring之间的访问-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
8.3)完整的web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<!--启动之后直接进入首页-->
<welcome-file-list>
<welcome-file>/WEB-INF/view/login.jsp</welcome-file>
</welcome-file-list>
<!--启动dubbo-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:smbms_user_consume.xml</param-value>
</context-param>
<!--让多个spring之间的访问-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--spring mvc -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--启动spring mvc 配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
9. 完善smbms-user-consume的补充,并体现出order用的rest风格
引进jquery-1.4.js,并简写一个order.js:
$(function () {
$.ajax({
url:"http://localhost:20888/order/orderlist/11",
dataType:"jsonp",
success:function (orderList) {
var ul=$('ul');
$.each(orderList,function (index,item) {
ul.append('<li>'+item.proName+'</li>');
});
}
});
});
并补充完整log4j.properties日志配置文件,结构如图:
10. 测试
tomcat布置,因为中间件的端口号已经使用了8080端口号,所以这里需要改端口号:
跟之前一样,需要启动zookeeper中间件的量的服务和tomcat的服务,并且这里先启动smbms-user-provider模块的测试,查看http://localhost:8080/dubbo-admin-2.8.4/注册中心是否有两个服务发布:
首先启动:TestProvider类;再启动tomcat,进入首页:
首页:
这里还需要调整,订单列表没有显示出来。