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

pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.drawnblue</groupId>
 7         <artifactId>pom</artifactId>
 8         <version>0.0.1-SNAPSHOT</version>
 9     </parent>
10     <groupId>com.drawblue</groupId>
11     <artifactId>gateway</artifactId>
12     <version>0.0.1-SNAPSHOT</version>
13     <name>gateway</name>
14     <description>Demo project for Spring Boot</description>
15 
16     <properties>
17         <java.version>1.8</java.version>
18     </properties>
19 
20     <dependencies>
21         <dependency>
22             <groupId>org.springframework.boot</groupId>
23             <artifactId>spring-boot-starter</artifactId>
24         </dependency>
25 
26         <dependency>
27             <groupId>org.springframework.boot</groupId>
28             <artifactId>spring-boot-starter-test</artifactId>
29             <scope>test</scope>
30         </dependency>
31 <!--        <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-starter-web</artifactId>
34         </dependency>-->
35         <dependency>
36             <groupId>org.springframework.cloud</groupId>
37             <artifactId>spring-cloud-starter-gateway</artifactId>
38             <version>2.1.2.RELEASE</version>
39         </dependency>
40         <dependency>
41             <groupId>org.springframework.cloud</groupId>
42             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
43             <version>0.9.0.RELEASE</version>
44         </dependency>
45         <dependency>
46             <groupId>org.springframework.boot</groupId>
47             <artifactId>spring-boot-starter-actuator</artifactId>
48         </dependency>
49     </dependencies>
50 
51     <build>
52         <plugins>
53             <plugin>
54                 <groupId>org.springframework.boot</groupId>
55                 <artifactId>spring-boot-maven-plugin</artifactId>
56                 <executions>
57                     <execution>
58                         <goals>
59                             <goal>repackage</goal>
60                         </goals>
61                     </execution>
62                 </executions>
63             </plugin>
64             <plugin>
65                 <groupId>org.apache.maven.plugins</groupId>
66                 <artifactId>maven-assembly-plugin</artifactId>
67                 <version>2.4</version>
68                 <executions>
69                     <execution>
70                         <id>bundle</id>
71                         <phase>package</phase>
72                         <goals>
73                             <goal>single</goal>
74                         </goals>
75                         <configuration>
76                             <descriptors>
77                                 <descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
78                             </descriptors>
79                         </configuration>
80                     </execution>
81                 </executions>
82             </plugin>
83         </plugins>
84     </build>
85 
86 </project>

其中项目同其他随笔上的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上的服务
项目结构如下:

assembly.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <!-- 不配置会报错:Assembly ID must be present and non-empty -->
    <id>bin</id>
    <formats>
        <!--压缩文件的类型-->
        <format>tar.gz</format>
    </formats>
    <!--指明打包后是否有分发包的最外层,如果不写,默认是true-->
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <!--需要包含的文件与输出的路径-->
        <fileSet>
            <!--指明要对src/main/assembly/bin文件夹操作-->
            <directory>src/main/assembly/bin</directory>
            <!--指明经过assembly插件打包后bin目录会放置在项目根目录下-->
            <outputDirectory>bin</outputDirectory>
            <!--指明bin目录下所有文件的权限为755-->
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <includes>
                <include></include>
            </includes>
            <outputDirectory>conf</outputDirectory>
            <fileMode>0644</fileMode>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
        </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目录

部署项目

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

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

然后查看nacos

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

然后看到跳转至百度页面

 

 

 

 
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐