如何連接上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.154.163 > 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