您现在的位置是:首页 > 正文

dubbox在异构系统中的使用-补充1

2024-02-01 04:45:19阅读 5

除了添加必要的dependencies外,其他需要设置的文件包括:

1. web.xml,加载dubbox dispatch servlet:

<?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">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/servlet-context.xml</param-value>
	</context-param>

    <listener>
        <listener-class>com.alibaba.dubbo.remoting.http.servlet.BootstrapListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

2. servlet-context.xml, 加载dubbo application配置,注意前面的配置作用是设置编码为utf-8,并忽略json中不存在的字段:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		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  
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-2.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven>
	    <message-converters register-defaults="false">
	        <beans:bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
	        <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">
	            <beans:constructor-arg index="0" value="utf-8"/>
	        </beans:bean>
	        <beans:bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
	        <beans:bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
	        <beans:bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter" />
	        <beans:bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
	        <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	            <beans:property name="objectMapper">
	                <beans:bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
	                    <beans:property name="featuresToDisable">
	                        <util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES" />
	                    </beans:property>
	                </beans:bean>
	            </beans:property>
	        </beans:bean>
	    </message-converters>
	</annotation-driven>
    <beans:bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"></beans:bean>
	
	<dubbo:application name="dubbox-demo"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
	<dubbo:protocol name="rest" port="8080" server="servlet" contextpath="dubbox"></dubbo:protocol>
	
    <beans:bean id="helloService" class="com.abel.dubbox.impl.HelloServiceImpl"></beans:bean>
	<dubbo:service interface="com.abel.dubbox.HelloService" ref="helloService"></dubbo:service>
	
    <beans:bean id="userService" class="com.abel.dubbox.impl.UserServiceImpl"></beans:bean>
	<dubbo:service interface="com.abel.dubbox.UserService" ref="userService"></dubbo:service>
	
</beans:beans>

3. 注意为了正确解析日期时间类型,本示例采用了ISO格式传递,主要引用了两个序列化/反系列化两个类:

package com.smart.sso.server.model;

import java.util.Date;

import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.abel.bigwater.model.JsonDateDeserializer;
import com.abel.bigwater.model.JsonDateSerializer;
import com.alibaba.fastjson.annotation.JSONField;
import com.smart.mvc.enums.TrueFalseEnum;
import com.smart.mvc.model.PersistentObject;

/**
 * 管理员
 * 
 * @author Joe
 */
public class User extends PersistentObject {

	private static final long serialVersionUID = 1106412532325860697L;
	
	/** 登录名 */
	private String account;
	/** 密码 */
	private String password;
	/** 最后登录IP */
	private String lastLoginIp;
	/** 登录总次数 */
	private Integer loginCount = Integer.valueOf(0);
	/** 最后登录时间 */
	@JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date lastLoginTime;
	/** 创建时间 */
	@JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;
	/** 是否启用 */
	private Boolean isEnable = Boolean.valueOf(true);
	
	/** 部门编号 */
	private Integer deptId;

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLastLoginIp() {
		return lastLoginIp;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}

	public Integer getLoginCount() {
		return loginCount;
	}

	public void setLoginCount(Integer loginCount) {
		this.loginCount = loginCount;
	}

	@JsonSerialize(using = JsonDateSerializer.class)
	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	@JsonDeserialize(using = JsonDateDeserializer.class)
	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	@JsonSerialize(using = JsonDateSerializer.class)
	public Date getCreateTime() {
		return createTime;
	}

	@JsonDeserialize(using = JsonDateDeserializer.class)
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Boolean getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(Boolean isEnable) {
		this.isEnable = isEnable;
	}

	public String getIsEnableStr() {
		return (isEnable != null && isEnable) ? TrueFalseEnum.TRUE.getLabel() : TrueFalseEnum.FALSE.getLabel();
	}

    /**
     * @return the deptId
     */
    public Integer getDeptId() {
        return deptId;
    }

    /**
     * @param deptId the deptId to set
     */
    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }
}

其中 JsonDateSerializer类:

package com.abel.bigwater.model;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

public class JsonDateSerializer extends JsonSerializer<Date> {
    private SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    @Override
    public void serialize(Date date, JsonGenerator jgen,
            SerializerProvider provider) throws IOException,
            JsonProcessingException {
        String value = dateFormat.format(date);
        jgen.writeString(value);
    }

}

JsonDateDeserializer类:

package com.abel.bigwater.model;

import java.io.IOException;
import java.util.Date;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

public class JsonDateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        return JsonHelperJava.parseISODate(jp.getText());
    }

}


JsonHelperJava类:

package com.abel.bigwater.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class JsonHelperJava {

    private JsonHelperJava() {
        // nothing
    }

    public static final String FULL_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSX";

    public static final String FULL_DATE_FORMAT_NO_AREA = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    public static final String FULL_DATE_FORMAT_NO_MILLI = "yyyy-MM-dd'T'HH:mm:ssX";

    public static final String FULL_ISODATE_FORMAT_NO_MILLI = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    public static final String FULL_DATE_FORMAT_NO_MILLI_AREA = "yyyy-MM-dd'T'HH:mm:ss";

    public static final String DATE_MIN_FORMAT = "yyyy-MM-dd'T'HH:mmX";

    public static final String DATE_MIN_FORMAT_NO_AREA = "yyyy-MM-dd'T'HH:mm";

    /**
     * parses string with iso-date-format/other precise date-format/default date-format.
     * 
     * @param str
     * @param dv
     * @return
     */
    public static Date parseISODate(String str) {
        if (str == null) {
            return null;
        }

        //        str = str.replace('T', ' ');
        SimpleDateFormat sdf = new SimpleDateFormat(FULL_DATE_FORMAT);
        try {
            return sdf.parse(str);
        } catch (Exception ex) {
            // nothing.
        }

        sdf = new SimpleDateFormat(FULL_ISODATE_FORMAT_NO_MILLI);
        try {
            return sdf.parse(str);
        } catch (Exception ex) {
            // nothing.
        }

        // if invalid 1.
        sdf = new SimpleDateFormat(FULL_DATE_FORMAT_NO_AREA);
        try {
            return sdf.parse(str);
        } catch (Exception ex) {
            // nothing.
        }

        // if invalid 2.
        sdf = new SimpleDateFormat(FULL_DATE_FORMAT_NO_MILLI);
        try {
            return sdf.parse(str);
        } catch (Exception ex) {
            // nothing.
        }

        // if no area & no millis.
        sdf = new SimpleDateFormat(FULL_DATE_FORMAT_NO_MILLI_AREA);
        try {
            return sdf.parse(str);
        } catch (Exception ex) {
            // nothing.
        }

        // if no seconds
        sdf = new SimpleDateFormat(DATE_MIN_FORMAT);
        try {
            return sdf.parse(str);
        } catch (Exception ex) {
            // nothing.
        }

        // if no seconds & area
        sdf = new SimpleDateFormat(DATE_MIN_FORMAT_NO_AREA);
        try {
            return sdf.parse(str);
        } catch (Exception ex) {
            // nothing.
        }

        try {
            return DateFormat.getInstance().parse(str);
        } catch (Exception ex) {
            // does nothing
        }

        return null;
    }
}



网站文章

  • Android 8.0 App Standby

    一、概述低电耗模式和应用待机模式是从Android M引入的新特性,之前一直没有分析,低电耗模式就是Doze,应用待机模式就是 App Standby。Doze模式我们之前分析过了,Doze模式在Android N又有修改,Android 6.0(API 级别 23)引入了低电耗模式,当用户设备未插接电源、处于静止状态且屏幕关闭时,该模式会推迟 CPU 和网络活动,从而延长电池寿命。而 A...

    2024-02-01 04:45:01
  • 维宏控制卡win7 驱动_雕刻机专用维宏5.55运动驱动控制卡

    详情介绍雕刻机维宏控制卡电脑雕刻机控制系统功能特点: 1.全中文windows界面,图形三维显示,操作简捷; 2.pci总线插卡结构,pci 2.1标准,windows 2000/nt/98兼容; 3...

    2024-02-01 04:44:52
  • Vue3 计算属性

    Vue3 计算属性

    上一篇博文说了 vue3 项目的 toRefs 函数和 toRef 函数,今天就稍微总结一下 vue3 的计算属性,其实学过 vue2 的宝子们应该都清楚,计算属性这个东西在项目开发过程中使用的还是比...

    2024-02-01 04:44:25
  • Spring data elasticsearch简单上手 | ES-7版本,springboot 2.4

    本来觉得写这个没什么意义,感觉看看别人就够了,然鹅,,,,被网上的坑的很惨再次强调本文的环境:2021年1月23日01:51:43的最新配置spring boot 2.4版本Elasticsearch...

    2024-02-01 04:44:18
  • Typora 开始收费,改用好玩的MarkText

    Typora 开始收费,改用好玩的MarkText

    收费……可以考虑使用:MarkText简述MarkTextMarkText 这个工具侧重于“命令”,导航栏都被收起来了。有些小伙伴感觉反而不好用,其实不然,是未了解该工具的强大之处。md 文本本身就是...

    2024-02-01 04:44:12
  • jsp中base标签的两种使用方法

    方法一 &lt;% String basePath = request.getScheme() + &quot;://&quot; + request.getServerName() + &quot;...

    2024-02-01 04:43:41
  • Cython入门到放弃(一) 热门推荐

    无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。教程链接:https://www.cbedai.net/qtlyx python作为一门强大的脚本语言,优势自然不必说,目前中低频的量化投资基本都是使用python作为research和production作为语...

    2024-02-01 04:43:33
  • dubbo微服务架构中传递MuitipartFile的问题及解决方式

    在编程的过程中总会遇到各式各样的问题,只有遇到的多了,总结的多了,我们才会有提升,在未来的道路上走的更远。

    2024-02-01 04:43:26
  • python处理命令行参数

    python处理命令行参数

    导航:起始页 &gt; Dive Into Python &gt; 脚本和流 &gt; 处理命令行参数 &gt;&gt; 深入 Python :Dive Into Python 中文版 Python 从新手到专家 [Dip_5.4b_CPyUG_Release] Find: 10.6. 处理命令行参数 Python

    2024-02-01 04:42:55
  • 【CSS学习】CSS中常用样式大全

    【CSS学习】CSS中常用样式大全,各种在web设计中使用到的css样式

    2024-02-01 04:42:49