新建一个springboot项目然后做了一个小demo跳转到baidu
pom
1 23 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 4.0.0 56 10com.drawnblue 7pom 80.0.1-SNAPSHOT 9com.drawblue 11gateway 120.0.1-SNAPSHOT 13gateway 14Demo project for Spring Boot 15 1617 19 201.8 1821 50 5122 25 26org.springframework.boot 23spring-boot-starter 2427 31 35org.springframework.boot 28spring-boot-starter-test 29test 3036 40org.springframework.cloud 37spring-cloud-starter-gateway 382.1.2.RELEASE 3941 45org.springframework.cloud 42spring-cloud-starter-alibaba-nacos-discovery 430.9.0.RELEASE 4446 49org.springframework.boot 47spring-boot-starter-actuator 4852 85 8653 8454 64org.springframework.boot 55spring-boot-maven-plugin 5657 6358 6259 61repackage 6065 83org.apache.maven.plugins 66maven-assembly-plugin 672.4 6869 8270 81bundle 71package 7273 75single 7476 8077 79${basedir}/src/main/assembly/assembly.xml 78
其中项目同其他随笔上的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> <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目录
部署项目
将tar.gz部署到linux上解压,进入bin文件启动,启动后,开放端口,然后再重新加载一下
注意:启动时候要确保nacos已经启动且端口可以访问
然后查看nacos
浏览器中输入:http://192.168.135.130:8800/test,回车
然后看到跳转至百度页面