1. registry可视化管理界面安装
- 下载对应的war文件(dubbo-admin-2.5.10.war)
- 将该文件复制到tomcat的webapps目录下
- 启动tomcat,tomcat将自动解压
- 解压完毕后,关闭tomcat,修改解压后目录下的WEB-INF目录下的dubbo.properties文件
#zookeeper的地址 dubbo.registry.address=zookeeper://192.168.54.20:2181 #root账户密码 dubbo.admin.root.password=root #guest账户密码 dubbo.admin.guest.password=guest
- 再次启动tomcat,访问该项目即可
2. Assembly打包插件安装
- 在maven项目的pom配置文件中添加assembly插件
<!-- 指定项目的打包插件信息 --> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <!-- 指定打包描述文件的位置:相对项目根目录的路径 --> <!-- assembly打包的描述文件 --> <descriptor>assembly/assembly.xml</descriptor> </configuration> <!--执行器 --> <executions> <execution> <!--名字任意 --> <id>make-assembly</id> <!-- 绑定到package生命周期阶段上 --> <phase>package</phase> <goals> <!-- 只运行一次 --> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins>
- 在该maven项目根目录创建Assembly目录
- 在创建的assembly目录下创建assembly.xml文件,用来配置打包详情
<?xml version='1.0' encoding='UTF-8'?> <assembly> <!-- 该字符会添加到最终tar.gz包的名称后面,作为后缀 --> <id>assembly</id> <!-- 指定打包的格式为tar.gz,该类型压缩包在linux中比较常见 --> <formats> <format>tar.gz</format> </formats> <!-- 在tar.gz压缩包中是否包含根文件夹,该根文件夹名称和tar.gz去掉id后缀一致 --> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <!-- 将项目根路径下assembly/bin路径中的内容打包到压缩包中的根目录下的bin目录中 --> <fileSet> <!-- 相对项目根路径的相对路径 --> <directory>assembly/bin</directory> <outputDirectory>bin</outputDirectory> <!-- 设置最终tar.gz中该文件夹下的权限,跟linux权限写法一致 --> <fileMode>0755</fileMode> </fileSet> <!-- 将项目根路径下assembly/conf路径中的内容打包到压缩包中的根目录下的conf目录中 --> <fileSet> <directory>assembly/conf</directory> <outputDirectory>conf</outputDirectory> <!-- 设置其linux权限 --> <fileMode>0644</fileMode> </fileSet> </fileSets> <!-- 将所有依赖的jar包打包到压缩包中的根目录下的lib目录中 --> <!-- 此lib目录中包含自己开发的项目jar包以及demo_service.jar,还有第三方的jar包 --> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> </assembly>
- 在创建的assembly目录下创建bin目录及conf目录(dubbo.properties内容为空)
- 执行package即可实现打包
3. 搭建文件服务器的好处
- 实现文件的统一管理
- 减轻应用服务器的压力
- 防止直接发布到tomcat后出现重启丢失等问题
4. 搭建文件服务器的一般步骤
1. 安装vsftpd
vsftpd是一款遵循了ftp协议的FTP(File Transfer Protocol)服务端程序(小巧、安全、易用)
- 创建专用账户名
#创建专用账户名
useradd ftpuser
#创建该账户的密码
passwd useradd
- 在线安装vsftpd
yum install vsftpd -y
- 开启ftp
#查询ftp是否可用
getseboll -a|grep ftp
#为off则开启(只需开启两项)
setsebool allow_ftpd_full_access on
setsebool ftp_home_dir on
- 配置vsftpd
#修改/etc/vsftpd/vsftpd.conf
#关闭匿名访问
anonymous_enable=NO
#开启被动模式
pasv_min_port=30000
pasv_max_port=30999
- 设置自启并重启vsftpd
#设置开机自启
chkconfig vsftpd on
#重启ftpd服务
service vsftps restart
2. 安装Nginx
Nginx是一个高性能的HTTP和反向代理服务器
- 安装c环境依赖(Nginx是使用c语言编写的)
yum -y install gcc-c++ pcre-devel zlib-devel
- 上传Nginx压缩包并解压到根目录
- 进入Nginx目录,执行configure文件,检查配置
./configure
- 编译并安装
make && make install
- /usr/local/nginx/sbin/下启动(默认80端口)
./nginx
- 修改配置
#修改/usr/local/nginx/conf/nginx.conf
#设置具有指定用户权限(没有权限则会报错403)
user ftpuser;
#设置代理目录及默认显示资源
location / {
#指定代理目录
root /home/ftpuser;
#指定显示资源(资源不存在则报错403)
index lady.png;
}
- 重启nginx
#sbin目录下执行
./nginx -s reload
3. 文件上传演示代码
- 文件目录结构
- web.xml
<?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" id="WebApp_ID" version="3.0">
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--指定spring配置文件名称及路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<!--配置SpringMVC的DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置字符编码过滤器-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--配置组件扫描器-->
<context:component-scan base-package="cn.khue.controller"/>
<!--配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>
spring-service.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置组件扫描器-->
<context:component-scan base-package="cn.khue.service.impl"/>
<!--加载属性文件-->
<context:property-placeholder location="classpath:vsftp.properties"/>
</beans>
vsftp.properties
vsftp.host=192.168.54.30
vsftp.port=21
vsftp.username=ftpuser
vsftp.password=ftpuser
vsftp.basePath=/home/ftpuser
vsftp.filePath=/
nginx.url=http://192.168.54.30/
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div align="center">
<h2>文件上传</h2>
<hr/>
<form action="${ctx}/fileUpload.do" method="post" enctype="multipart/form-data">
<input type="file" name="img"/>
<input type="submit" value="点击上传"/>
</form>
</div>
</body>
</html>
FileUploadServiceImpl.java
package cn.khue.service.impl;
import cn.khue.service.FileUploadService;
import cn.khue.utils.FtpUtil;
import cn.khue.utils.NameUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
@Service
public class FileUploadServiceImpl implements FileUploadService
{
@Value("${vsftp.host}")
private String host;
@Value("${vsftp.port}")
private int port;
@Value("${vsftp.username}")
private String username;
@Value("${vsftp.password}")
private String password;
@Value("${vsftp.basePath}")
private String basePath;
@Value("${vsftp.filePath}")
private String filePath;
@Value("${nginx.url}")
private String url;
//文件上传
@Override
public Map<String ,Object> fileUpload(MultipartFile img) {
String oldName=img.getOriginalFilename();
String suffix=oldName.substring(oldName.lastIndexOf("."));
String fileName= NameUtil.getFileName()+suffix;
Map<String,Object> map=new HashMap<>();
try {
boolean result = FtpUtil.fileUpload(host, port, username, password, basePath, filePath, fileName, img.getInputStream());
if(result){
map.put("url",url+fileName);
}
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
}
FileUploadController.java
package cn.khue.controller;
import cn.khue.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
@Controller
public class FileUploadController {
@Autowired
private FileUploadService fileUploadService;
@RequestMapping("fileUpload.do")
public String fileUpload(MultipartFile img, Model model){
Map<String, Object> map = fileUploadService.fileUpload(img);
if(map.containsKey("url")){
model.addAttribute("url",map.get("url"));
System.out.println(map.get("url"));
return "/success.jsp";
}else {
return "/fail.jsp";
}
}
}
FtpUtil.java
package cn.khue.utils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
public class FtpUtil {
//FTP文件上传
public static boolean fileUpload(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream input){
boolean result=false;
FTPClient ftp=new FTPClient();
try{
int reply;
//连接FTP服务器
ftp.connect(host,port);
//登录
ftp.login(username,password);
//登录结果
reply=ftp.getReplyCode();
//登录结果判断
if(!(FTPReply.isPositiveCompletion(reply))){
ftp.disconnect();
return result;
}
//登录成功java.net.ConnectException: Connection refused: connect
//切换到上传目录
if(!ftp.changeWorkingDirectory(basePath+filePath)){
//如果目录不存在就创建
String[] dirs=filePath.split("/");
String tempPath=basePath;
for(String dir:dirs){
if(null == dir || "".equals(dir)){
continue;
}
tempPath+="/"+dir;
if(!ftp.changeWorkingDirectory(tempPath)){
if(!ftp.makeDirectory(tempPath)){
return result;
}else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
//设置上传文件的类型为二进制
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//上传文件
if(!ftp.storeFile(filename,input)){
return result;
}
input.close();
ftp.logout();
result=true;
}catch (IOException e){
e.printStackTrace();
}finally {
if(ftp.isConnected()){
try{
ftp.disconnect();
}catch (IOException ignored){
}
}
}
return result;
}
}
NameUtil.java
package cn.khue.utils;
import java.util.Random;
import java.util.UUID;
public class NameUtil {
//文件名生成
public static String getFileName(){
//获取当前时间的毫秒数
long millis=System.currentTimeMillis();
//生成全局唯一标识符
String id= UUID.randomUUID().toString();
//获取一个0-999之间的整形随机数
Random random=new Random();
int end=random.nextInt(999);
//如果end随机数不足三位则补零
String ends=String.format("%03d",end);
//返回最后的文件名
return millis+id+ends;
}
}