1 前言
最近幾天剛開始接觸微信小程序的開發,才接觸到了https的概念(微信小程序中的請求必須為https請求,不然請求無法成功)。https算是對http的安全封裝,在http的基礎上加了ssl證書機制,此處不贅述,想要詳細了解自行百度區別。所以博主就在考慮怎么讓整個小程序后台(用的spring boot來作為后台)都能通過https訪問,終於經過兩天的研究,我發現了將ssl結合spring cloud的zuul(路由網關)可以實現我想要的效果,話不多說,上步驟。
2 申請域名,並用域名申請ssl證書(博主用的阿里申請的ssl證書)
點擊下載,將對應的ssl證書下載到本地
3 創建zuul微服務
3.1pom.xml中配置相關依賴(需要添加到eureka注冊中心)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
3.2 將ssl證書放到resources下的ssl文件夾中並在yml中配置
spring: application: name: zuul #注冊到eureka eureka: instance: lease-expiration-duration-in-seconds: 30 lease-renewal-interval-in-seconds: 10 prefer-ip-address: true instance-id: dragon client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:7001/eureka/ server: port: 443 # ssl證書相關配置 ssl: key-store: classpath:https/1538502410793.pfx key-store-password: 1538502410793 key-store-type: PKCS12 #zuul的相關配置 zuul: routes: # 需要配置路由的微服務 consumer: serviceId: eureka-consumer path: /con/** # 無法使用服務名稱訪問 ignored-services: "*" # 訪問前添加前綴 prefix: /fengyuntec/
注意:https的默認端口為443,這里也建議設置成443
3.3 在zuul微服務的啟動類上添加注解
@SpringBootApplication @EnableZuulProxy public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
4 訪問網址
4.1 訪問http://localhost:7001/ 進入eureka注冊中心查看服務
4.2 訪問網址 https://www.dragonchen.top/fengyuntec/con/hello
5 總結
1508952532 博主qq,有什么問題可以問我,願程序員的世界一片祥和。