快捷搜索:

SpringCloud搭建 (二)

microservice-common(公共模块)

我们在microservice-parent下创建一个名叫microservice-common的maven模块,用于放一些工具类,通用实体等,注意咯,这次我们创建的是maven模块,和maven项目类似,是一个叫做Maven Module的东西,如下所示

之后点击Next,注意,这次的Packaging模式我们选择jar模式,后面亦如是,然后Finish,创建好的项目层级关系如下所示

然后我们在pom.xml引入一些jar包,这次我们修改的是microservice-common下的pom.xml文件而不是microservice-parent下的pom.xml,要看仔细咯

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.springcloud</groupId>
		<artifactId>microservice-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microservice-common</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
		</dependency>
	</dependencies>

</project>

注意,这里为什么lombok没有版本号?这是因为,它爸爸(microservice-parent)里的pom.xml说了,听我的,所以在microservice-parent的pom.xml里指定的版本号,它的子模块都无需指定

创建好项目层级关系如下图所示

这时我们发现,microservice-common是作为一个文件夹存在的,而非一个项目,这时我们怎么做呢?重启! 没错,就是重启eclipse,万能的重启大法啊...

后面他就变成一个项目了,好神奇啊!接下来我们在microservice-common下创建util和entity,用于放一些通用工具类和实体,如下所示

接下来我们在entity包下封装一个Restful返回实体如下所示

package entity;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import util.EmptyUtil;

@Getter
@Setter
@Accessors(chain = true)
public class Result implements Serializable {

	private static final long serialVersionUID = 1L;

	private ResultCode resultCode; // 状态值

	private String msg; // 状态信息

	private Object data; // 传输数据

	private volatile static Result result;

	private Result() {
	}

	public static Result response(ResultCode resultCode, String msg, Object data) {
		if (result == null) // 双锁模式
			synchronized (Result.class) {
				if (result == null)
					result = new Result();
			}
		return result.setResultCode(resultCode).setMsg(msg).setData(data);
	}

	public static Result response(ResultCode resultCode, Object data) {
		return response(resultCode, ResultCode.getMsgByCode(resultCode), data);
	}

	public static Result response(ResultCode resultCode) {
		return response(resultCode, null);
	}

	public static Result response(Object data) {
		if (EmptyUtil.isEmpty(data))
			return response(ResultCode.FAILURE, data);
		return response(ResultCode.SUCCESS, data);
	}

	@Override
	public String toString() {
		try {
			return new ObjectMapper().writeValueAsString(this);
		} catch (JsonProcessingException e) {
			return "{}";
		}
	}

	// 定义返回状态值枚举
	public static enum ResultCode {
		SUCCESS("200"), // 成功
		FAILURE("500"); // 失败

		@JsonValue
		private String resultCode;

		private ResultCode(String resultCode) {
			this.resultCode = resultCode;
		}

		static String getMsgByCode(ResultCode resultCode) {
			String result = null;
			switch (resultCode) {
			case SUCCESS:
				result = "success";
				break;
			case FAILURE:
				result = "failure";
			}
			return result;
		}

		@Override
		public String toString() {
			return resultCode;
		}
	}

}

之后我们在放个User类方便后面重用

package entity;

import java.util.Date;

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class User {
	private Long id;
	private String username;
	private String password;
	private String name;
	private String phone;
	private String email;
	private String salt;
	private Boolean enable = true;
	private Date createTime;
	private String creater;
	private Date updateTime;
	private String updater;
	private Date lastLoginTime;
}

然后在util下面简单的写一个非空判断工具类

package util;

import java.util.Collection;
import java.util.Map;

public class EmptyUtil {

	public static boolean isEmpty(Object obj) {
		if (obj == null)
			return true;
		if (obj instanceof String)
			return obj.toString().isEmpty();
		if (obj instanceof Collection)
			return ((Collection<?>) obj).isEmpty();
		if (obj instanceof String)
			return ((Map<?, ?>) obj).isEmpty();
		return false;
	}
	
}

当然这个类判断的情况很少,除此之外还有很多其他类型在此不一一列举了

那现在,我们的公共模块就ok了,下节我们就开始正课--microservice-provider(提供者)

经验分享 程序员 微信小程序 职场和发展