從零開始實現放置遊戲(十)——實現戰鬥掛機(1)hessian服務端搭建

  前面實現RMS系統時,我們讓其直接訪問底層數據庫。後面我們在idlewow-game模塊實現遊戲邏輯時,將不再直接訪問底層數據,而是通過hessian服務暴露接口給表現層。

  本章,我們先把hessian服務搭好,並做一個簡單的測試,這裏以用戶註冊接口爲例。

  先簡單介紹下,實現hessian接口,只需要在facade模塊暴露接口,然後在core模塊實現接口,最後在hessain模塊配置好接口路由,將其啓動即可。

實現步驟

idlewow-facade

  新建包com.idlewow.user.model,在此包下添加模型類:

package com.idlewow.user.model;

import com.idlewow.common.model.BaseModel;
import lombok.Data;

import java.io.Serializable;

@Data
public class UserAccount extends BaseModel implements Serializable {
    private String username;
    private String password;
    private String mail;
    private String phone;
    private String realName;
    private String idNo;
    private Integer status;
    private String remark;
    private String registerIp;
}
UserAccount.java

  新建包com.idlewow.user.service,在此包下添加接口類:

package com.idlewow.user.service;

import com.idlewow.common.model.CommonResult;

public interface UserService {
    CommonResult register(String username, String password, String ip);

    CommonResult login(String username, String password);
}
UserService.java

idlewow-core

    新建包com.idlewow.user.mapper,添加mapper文件:

package com.idlewow.user.mapper;

import com.idlewow.user.model.UserAccount;

public interface UserAccountMapper {
    int register(UserAccount userAccount);

    UserAccount login(UserAccount userAccount);

    UserAccount findByUsername(String username);
}
UserAccountMapper.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.idlewow.user.mapper.UserAccountMapper">
    <resultMap id="BaseResultMap" type="com.idlewow.user.model.UserAccount">
        <result column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="mail" property="mail"/>
        <result column="phone" property="phone"/>
        <result column="real_name" property="realName"/>
        <result column="id_no" property="idNo"/>
        <result column="status" property="status"/>
        <result column="remark" property="remark"/>
        <result column="register_ip" property="registerIp"/>
        <result column="create_user" property="createUser"/>
        <result column="update_user" property="updateUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_delete" property="isDelete"/>
        <result column="version" property="version"/>
    </resultMap>
    <!-- 註冊 -->
    <insert id="register">
        insert into user_account (username, password, register_ip, create_user)
        values (#{username}, #{password}, #{registerIp}, #{createUser})
    </insert>
    <!-- 登陸 -->
    <select id="login" resultMap="BaseResultMap">
        select *
        from user_account
        where username = #{username} and password = #{password} and is_delete = 0
    </select>
    <!-- id查詢 -->
    <select id="find" resultMap="BaseResultMap">
        select *
        from user_account
        where id = #{id} and is_delete = 0
    </select>
    <!-- 根據用戶名查找用戶 -->
    <select id="findByUsername" resultMap="BaseResultMap">
        select *
        from user_account
        where username = #{username} and is_delete = 0
    </select>
    <!-- 列表查詢總數 -->
    <select id="count" resultType="int">
        select count(1)
        from map_mob
        <where>
            is_delete = 0
            <if test="mapId != null">
                and map_id = #{mapId}
            </if>
            <if test="faction != null">
                and faction = #{faction}
            </if>
            <if test="mobClass != null">
                and mob_class = #{mobClass}
            </if>
            <if test="mobType != null">
                and mob_type = #{mobType}
            </if>
            <if test="levelStart != null">
                and level &gt;= #{levelStart}
            </if>
            <if test="levelEnd != null">
                and level &lt;= #{levelEnd}
            </if>
            <if test="name != null and name != ''">
                and name like concat('%', #{name}, '%')
            </if>
        </where>
    </select>
    <!-- 列表查詢 -->
    <select id="list" resultMap="BaseResultMap">
        select *
        from map_mob
        <where>
            is_delete = 0
            <if test="mapId != null">
                and map_id = #{mapId}
            </if>
            <if test="faction != null">
                and faction = #{faction}
            </if>
            <if test="mobClass != null">
                and mob_class = #{mobClass}
            </if>
            <if test="mobType != null">
                and mob_type = #{mobType}
            </if>
            <if test="levelStart != null">
                and level &gt;= #{levelStart}
            </if>
            <if test="levelEnd != null">
                and level &lt;= #{levelEnd}
            </if>
            <if test="name != null and name != ''">
                and name like concat('%', #{name}, '%')
            </if>
        </where>
        <if test="pageParam != null">
            limit ${(pageParam.pageIndex - 1) * pageParam.pageSize}, ${pageParam.pageSize}
        </if>
    </select>
</mapper>
UserAccountMapper.xml

  新建com.idlewow.user.manager包,添加manager類:

package com.idlewow.user.manager;

import com.idlewow.user.mapper.UserAccountMapper;
import com.idlewow.user.model.UserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserAccountManager {
    @Autowired
    UserAccountMapper userAccountMapper;

    public UserAccount findByUsername(String username) {
        return userAccountMapper.findByUsername(username);
    }

    public void register(String username, String password, String ip) {
        UserAccount userAccount = new UserAccount();
        userAccount.setUsername(username);
        userAccount.setPassword(password);
        userAccount.setRegisterIp(ip);
        userAccount.setCreateUser("idlewow");
        int effected = userAccountMapper.register(userAccount);
        if (effected == 0) {
            throw new RuntimeException("sql effected 0 rows");
        }
    }

    public UserAccount login(String username, String password) {
        UserAccount userAccount = new UserAccount();
        userAccount.setUsername(username);
        userAccount.setPassword(password);
        return userAccountMapper.login(userAccount);
    }
}
UserAccountManager.java

   新建com.idlewow.user.service.impl包,添加接口的實現類:

package com.idlewow.user.service.impl;

import com.idlewow.common.model.CommonResult;
import com.idlewow.user.manager.UserAccountManager;
import com.idlewow.user.model.UserAccount;
import com.idlewow.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
    private final Logger logger = LogManager.getLogger(this.getClass().getName());
    @Autowired
    UserAccountManager userAccountManager;

    public CommonResult register(String username, String password, String ip) {
        try {
            UserAccount userAccount = userAccountManager.findByUsername(username);
            if (userAccount != null) {
                return CommonResult.fail("此用戶名已被註冊!");
            }

            userAccountManager.register(username, password, ip);
            return CommonResult.success();
        } catch (Exception ex) {
            logger.error("用戶註冊失敗:" + ex.getMessage(), ex);
            return CommonResult.fail("用戶註冊失敗");
        }
    }

    @Override
    public CommonResult login(String username, String password) {
        try {
            UserAccount userAccount = userAccountManager.login(username, password);
            if (userAccount == null) {
                return CommonResult.fail("用戶名或密碼錯誤!");
            }

            if (userAccount.getStatus() == 1) {
                return CommonResult.fail("賬號已凍結!");
            }

            return CommonResult.success("", userAccount);
        } catch (Exception ex) {
            logger.error("用戶登錄失敗:" + ex.getMessage(), ex);
            return CommonResult.fail("用戶登錄失敗");
        }
    }
}
UserServiceImpl.java

  注意,這裏ServiceImple類上面有個註解 @Service("userService")。後面我們再添加這種對外的服務類時,都要加這個註解。

 idlewow-hessian

  hessian用於服務(器)間通信,實際上也是由一個DispatherServlet接收請求,並轉發到各個Service中處理,和springmvc差不多,只不過返回的是二進制數據,而不是視圖。我們在pom下添加下列依賴,可以發現依賴的包和mvc差不多。另外,作爲啓動項目,在plugins節點下,我們配置了啓動插件tomcat7以及啓動端口20000。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>idlewow</artifactId>
        <groupId>idlewow</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>idlewow-hessian</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>idlewow</groupId>
            <artifactId>idlewow-facade</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>idlewow</groupId>
            <artifactId>idlewow-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.60</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.6.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <uriEncoding>UTF-8</uriEncoding>
                    <port>20000</port>
                    <path>/</path>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
            </plugin>
        </plugins>
    </build>
</project>
pom.xml

  然後,我們需要在web.xml中配置hessian的servlet,以及添加一個字符編碼的filter等等,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/remoting/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>remoting</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/hessian-servlet.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>remoting</servlet-name>
        <url-pattern>/remoting/*</url-pattern>
    </servlet-mapping>
</web-app>
web.xml

  在/resource/spring目錄下,新建hessian服務的配置文件hessian-servlet.xml。這個xml主要配置對外暴露的hessian服務。現在我們只配置了UserService,後面每次添加對外的服務接口時,都需要在這裏添加配置。

<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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        ">

    <mvc:annotation-driven/>

    <bean name="/UserService" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="userService"/>
        <property name="serviceInterface" value="com.idlewow.user.service.UserService"/>
    </bean>
</beans>
hessian-servlet.xml

  除了這3個配置外,還需要配置applicationContext.xml, jdbc.propetries, dataSource.xml, log4j2.xml,和RMS系統大體一致,這裏就不再重複了。具體可在源碼中查看。

  全部搞定後,只要把hessian項目啓動,即可調用hessian接口了。啓動步驟和rms一樣,maven命令也是tomcat7:run,工作目錄切換到hessian目錄下即可。

運行效果

  在game模塊中調用hessian時,也是通過在xml中配置注入的方式調用。這裏我們還沒開始寫game模塊,爲了測試,先簡單寫一個入口類(即帶main函數的類)調用。類似於C#中寫控制檯程序調用。

  在/src/test/java包下新建一個類HessianTest.java如下:

import com.caucho.hessian.client.HessianProxyFactory;
import com.idlewow.common.model.CommonResult;
import com.idlewow.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class HessianTest {
    protected static final Logger logger = LogManager.getLogger(HessianTest.class);

    public static void main(String[] args) {
        String url = "http://localhost:20000/remoting/UserService";
        HessianProxyFactory factory = new HessianProxyFactory();
        try {
            UserService userService = (UserService) factory.create(UserService.class, url);
            CommonResult commonResult = userService.register("testuser", "123456", "127.0.0.1");
            logger.info(commonResult);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
    }
}
HessianTest.java

  在這個類中,我們指定hessian服務地址,並利用代理工廠創建一個服務代理。然後調用用戶註冊方法。測試的時候,先把hessian項目啓動。然後執行這個main方法即可。

運行效果如下圖,可以看到,接口調用成功,並把執行結果在日誌中打印了出來。

小結

  本節把hessian服務搭建運行起來,並實現了用戶註冊登錄的接口。後面game模塊凡是訪問底層數據,均需調用hessian服務。

  本章源碼下載:https://idlestudio.ctfile.com/fs/14960372-387256708

  本文原文地址:https://www.cnblogs.com/lyosaki88/p/idlewow_10.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章