springcloud gateway 项目打包部署运行

新建一个springboot项目然后做了一个小demo跳转到baidu

pom

 1 
 2  3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     4.0.0
 5     
 6         com.drawnblue
 7         pom
 8         0.0.1-SNAPSHOT
 9     
10     com.drawblue
11     gateway
12     0.0.1-SNAPSHOT
13     gateway
14     Demo project for Spring Boot
15 
16     
17         1.8
18     
19 
20     
21         
22             org.springframework.boot
23             spring-boot-starter
24         
25 
26         
27             org.springframework.boot
28             spring-boot-starter-test
29             test
30         
31 
35         
36             org.springframework.cloud
37             spring-cloud-starter-gateway
38             2.1.2.RELEASE
39         
40         
41             org.springframework.cloud
42             spring-cloud-starter-alibaba-nacos-discovery
43             0.9.0.RELEASE
44         
45         
46             org.springframework.boot
47             spring-boot-starter-actuator
48         
49     
50 
51     
52         
53             
54                 org.springframework.boot
55                 spring-boot-maven-plugin
56                 
57                     
58                         
59                             repackage
60                         
61                     
62                 
63             
64             
65                 org.apache.maven.plugins
66                 maven-assembly-plugin
67                 2.4
68                 
69                     
70                         bundle
71                         package
72                         
73                             single
74                         
75                         
76                             
77                                 ${basedir}/src/main/assembly/assembly.xml
78                             
79                         
80                     
81                 
82             
83         
84     
85 
86 

其中项目同其他随笔上的pom项目,由于springcloud gateway 不能在tomcat运行,所以一定要注释掉web依赖

属性文件:

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.135.129:8848
    gateway:
      routes:
        - id: test-gateway
          uri: http://www.baidu.com
          predicates:
            - Path=/test/**
          filters:
          - StripPrefix=1
server:
  port: 8800

我将该项目注册在nacos上,另外注意

- StripPrefix=1不加的话就会报错,因为跳转后有前缀导致跳转错误不能跳转到百度
本项目搞的是个demo,nacos上没有其他服务,所以启动类上可以不加任何注解去发现注册到nacos上的服务
项目结构如下:

springcloud gateway 项目打包部署运行_第1张图片

assembly.xml

xml version="1.0" encoding="UTF-8"?>
<assembly>
    
    <id>binid>
    <formats>
        
        <format>tar.gzformat>
    formats>
    
    <includeBaseDirectory>trueincludeBaseDirectory>
    <fileSets>
        
        <fileSet>
            
            <directory>src/main/assembly/bindirectory>
            
            <outputDirectory>binoutputDirectory>
            
            <fileMode>0755fileMode>
        fileSet>
        <fileSet>
            <directory>src/main/resourcesdirectory>
            <includes>
                <include>include>
            includes>
            <outputDirectory>confoutputDirectory>
            <fileMode>0644fileMode>
        fileSet>
    fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>liboutputDirectory>
        dependencySet>
    dependencySets>
assembly>

start.sh

#!/bin/bash

cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
SERVER_NAME=gateway
JAR_File=$DEPLOY_DIR/lib/gateway-0.0.1-SNAPSHOT.jar

echo " "

PIDS=`ps -ef | grep java | grep "$JAR_File" |awk '{print $2}'`
if [ -n "$PIDS" ]; then
    echo "ERROR: The server $SERVER_NAME already started!"
    echo "PID: $PIDS"
    echo " "
    exit 1
fi

LOGS_DIR=$DEPLOY_DIR/logs

if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi

STDOUT_FILE=$LOGS_DIR/stdout.log

JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
    JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi
JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
    JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi
JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
    JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
    JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi

echo -e "Starting the server $SERVER_NAME ...\c"
nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -jar $JAR_File -Ddubbo.application.logger=slf4j > $STDOUT_FILE 2>&1 &

echo "OK!"
PIDS=`ps -ef | grep java | grep "$JAR_File" | awk '{print $2}'`
echo "Server $SERVER_NAME startup, PID: $PIDS"
echo "STDOUT: $STDOUT_FILE"
echo " "

restart.sh

#!/bin/bash
cd `dirname $0`
./stop.sh
./start.sh

stop.sh

#!/bin/bash

cd `dirname $0`
cd ..
DEPLOY_DIR=`pwd`

SERVER_NAME=gateway
JAR_File=$DEPLOY_DIR/lib/gateway-0.0.1-SNAPSHOT.jar

echo " "

PIDS=`ps -ef | grep java | grep "$JAR_File" |awk '{print $2}'`
if [ -z "$PIDS" ]; then
    echo "ERROR: The server $SERVER_NAME does not started!"
    echo " "
    exit 1
fi

echo -e "Stopping the server $SERVER_NAME ...\c"
for PID in $PIDS ; do
    kill $PID > /dev/null 2>&1
done

COUNT=0
while [ $COUNT -lt 1 ]; do    
    echo -e ".\c"
    sleep 1
    
    PIDS=`ps -ef | grep java | grep "$JAR_File" |awk '{print $2}'`
    if [ -z "$PIDS" ]; then        
        COUNT=2         
    fi
done

echo "OK!"
echo " "

status.sh

#!/bin/bash

cd `dirname $0`
cd ..
DEPLOY_DIR=`pwd`

SERVER_NAME=gateway
JAR_File=$DEPLOY_DIR/lib/gateway-0.0.1-SNAPSHOT.jar

echo " "

PIDS=`ps -ef | grep java | grep "$JAR_File" |awk '{print $2}'`
if [ -n "$PIDS" ]; then
    echo "The server $SERVER_NAME is running!"
    echo "PID: $PIDS"
    echo " "
    exit 1
fi

if [ -z "$PIDS" ]; then
    echo "The server $SERVER_NAME is stop!"
    echo " "
    exit 1
fi

stop.sh

#!/bin/bash

cd `dirname $0`
cd ..
DEPLOY_DIR=`pwd`

SERVER_NAME=gateway
JAR_File=$DEPLOY_DIR/lib/gateway-0.0.1-SNAPSHOT.jar

echo " "

PIDS=`ps -ef | grep java | grep "$JAR_File" |awk '{print $2}'`
if [ -z "$PIDS" ]; then
    echo "ERROR: The server $SERVER_NAME does not started!"
    echo " "
    exit 1
fi

echo -e "Stopping the server $SERVER_NAME ...\c"
for PID in $PIDS ; do
    kill $PID > /dev/null 2>&1
done

COUNT=0
while [ $COUNT -lt 1 ]; do    
    echo -e ".\c"
    sleep 1
    
    PIDS=`ps -ef | grep java | grep "$JAR_File" |awk '{print $2}'`
    if [ -z "$PIDS" ]; then        
        COUNT=2         
    fi
done

echo "OK!"
echo " "

打包后target目录

springcloud gateway 项目打包部署运行_第2张图片

部署项目

将tar.gz部署到linux上解压,进入bin文件启动,启动后,开放端口,然后再重新加载一下

springcloud gateway 项目打包部署运行_第3张图片

注意:启动时候要确保nacos已经启动且端口可以访问

然后查看nacos

springcloud gateway 项目打包部署运行_第4张图片

浏览器中输入:http://192.168.135.130:8800/test,回车

然后看到跳转至百度页面

 springcloud gateway 项目打包部署运行_第5张图片

 

 

 
  

你可能感兴趣的:(springcloud gateway 项目打包部署运行)