如何连接上springcloud搭建的网关
需求描述
由于小程序的发布需要用域名访问,不能直接裸ip,所以需要绑定域名
环境说明
- 使用springboot搭建的后端项目
- 使用springcloud搭建的网关
- 例如,域名为 https://duganlx.com
tips:
文章进行脱敏,如果是绑定到彬哥的域名上,你需要得到两个信息:域名、彬哥云服务器的ip,然后在下面操作的过程,将对应修改即可
操作小记
step1 maven添加依赖
<properties>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
step2 启动类中添加注解@EnableDiscoveryClient
@tk.mybatis.spring.annotation.MapperScan(basePackages = "com.ddu.practiceplatform.mapper")
@SpringBootApplication
@EnableTransactionManagement
@EnableDiscoveryClient //here
public class PracticeplatformApplication {
public static void main(String[] args) {
SpringApplication.run(PracticeplatformApplication.class, args);
}
}
step3 在resources文件夹中创建bootstrap.yml文件
bootstrap.yml文件内容
ip-address: localhost
spring:
application:
name: op
eureka:
client:
service-url:
defaultZone: http://39.108.xxx.xxx:1000/eureka/
fetch-registry: true
register-with-eureka: true
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
prefer-ip-address: true
hostname: ${ip-address}
ip-address: ${ip-address}
说明
ip-address
:启动应用时候的服务器的ip地址,要在启动的时候声明参数- 例如:
java -jar op.jar --ip-address=xx.xx.xx.xx(服务器所在ip地址)
- 例如:
Spring.application.name
:示例中的应用名称叫做op,所以每个应用的名字都应该不同,取好自己应用的名字,因为网关映射的时候要用到defaultZone
:配置的网关的地址- 在IDEA启动项目,启动的时候可以先不必输入
ip-address
这个参数,但是正式部署到服务器的时候要输入,如果参数正确并且启动不会报错,到 http://39.108.xxx.xxx:1000/ 查看自己的项目是否已经注册到eureka
如果成功,就可以在查看到自己的项目,没有则看看配置有没有出错
step4 修改application.properties文件
server.port=8090
#项目的访问路径 online practice简称
#server.servlet.context-path=/op
若是原本在application.properties中配置了server.servlet.context-path
属性,则可以把它注释掉,否则在用域名访问时需要多加这么一层路径
step5 将项目打包放入云服务器中
运行时,请带上--ip-address=xx.xx.xx.xx
参数,其中xx.xx.xx.xx
是你云服务器的外网地址,例子如下:
root@duganlx:~/jar# java -jar practiceplatform-0.0.1-SNAPSHOT.jar --ip-address=47.102.xxx.xxx
2020-03-26 12:01:58.395 INFO 8701 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a0316989] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)
step6 测试
若运行没有报错,则可以通过域名访问了,例如:https://duganlx.com/op/test
step7 正式部署
例如,在后台运行项目,并且将日志输出到pp.txt,再次访问没有问题,则完成了(*^▽^*)
root@duganlx:~/jar# nohup java -jar practiceplatform-0.0.1-SNAPSHOT.jar --ip-address=47.102.xxx.xxx > logs/pp.txt
nohup: ignoring input and redirecting stderr to stdout
附录
若在idea中跑的时候,控制台显示如下信息,解决方案是把项目jdk降低到1.8
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils (file:/C:/Users/ddu/.m2/repository/org/springframework/spring-core/5.1.6.RELEASE/spring-core-5.1.6.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
具体操作可参看:SprinfBoot报警告WARNING: An illegal reflective access operation has occurred
查看某端口占用的线程的pid:netstat -nlp | grep :8090
删除正在后台运行的项目:kill [pid]
后台运行项目:nohup java -jar shareniu.jar >temp.txt &
操作小记
root@duganlx:~# netstat -nlp | grep :8090
tcp 0 0 0.0.0.0:8090 0.0.0.0:* LISTEN 18629/java
root@duganlx:~# kill 18629
root@duganlx:~# netstat -nlp | grep :8090