寫在前面
網關的作用不在此贅述,舉個最常用的例子,我們搭建了微服務,前端調用各服務接口時,由於各服務接口不一樣,如果讓前端同事分別調用,前端同事會瘋的。而網關就可以解決這個問題,網關屏蔽了各業務服務的端口,對前端同事來說,他們只負責調用網關服務端口下的服務就可以了。本文簡單描述如何使用Spring Cloud全家桶中的網關服務,再配以Nacos。關於Nacos簡單應用,可以看我其他博客。
服務提供者
從https://start.spring.io/下載一個原始的spring boot工程,如何下載就不在這里說了。添加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
分別添加了web依賴、配置中心依賴和注冊中心依賴。
配置文件如下:
server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
啟動類如下:
package com.chris.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class MySpringbootApplication { public static void main(String[] args) { SpringApplication.run(MySpringbootApplication.class, args); } }
接口類如下:
package com.chris.springboot.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/provider") @RefreshScope public class ConfigController { @Value(value = "${Hello:123}") private String hello; @GetMapping("/helloProvider") public String helloProvider(){ return hello; } }
此服務為我的博客:https://www.cnblogs.com/ncwuwsh/p/12732516.html中的服務,可參看。
網關服務
從https://start.spring.io/下載一個原始的spring boot工程,如何下載就不在這里說了。添加依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency>
注意,千萬不要添加web依賴。
配置文件可以使用properties,也可以使用yml格式。yml格式如下:
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
discovery:
locator:
enabled: true #表明gateway開啟服務注冊和發現的功能,並且spring cloud gateway自動根據服務發現為每一個服務創建了一個router,這個router將以服務名開頭的請求路徑轉發到對應的服務。
lower-case-service-id: true #是將請求路徑上的服務名配置為小寫(因為服務注冊的時候,向注冊中心注冊時將服務名轉成大寫的了),比如以/service-hi/*的請求路徑被路由轉發到服務名為service-hi的服務上。
routes:
- id: gateway-service
uri: lb://service-provider #此配置的值注冊到Nacos中服務提供者的spring.application.name的值
predicates:
- Path=/provider/**
使用yml的同學,一定要去查下yml的一些規則,比如 :后面,值的前面,一定要有空格,縮進不要使用tab鍵,而要用兩個空格縮進等
下面是properties格式配置文件:
server.port=8080
spring.application.name=api-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#表明gateway開啟服務注冊和發現的功能,並且spring cloud gateway自動根據服務發現為每一個服務創建了一個router,這個router將以服務名開頭的請求路徑轉發到對應的服務。
spring.cloud.gateway.discovery.locator.enabled=true
#是將請求路徑上的服務名配置為小寫(因為服務注冊的時候,向注冊中心注冊時將服務名轉成大寫的了),比如以/service-hi/*的請求路徑被路由轉發到服務名為service-hi的服務上。
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
spring.cloud.gateway.routes[0].id=gateway-service
spring.cloud.gateway.routes[0].uri=lb://service-provider
spring.cloud.gateway.routes[0].predicates[0]=Path=/provider/**
下面是網關的啟動類:
package com.chris.gatewayrouter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableDiscoveryClient public class GatewayrouterApplication { public static void main(String[] args) { SpringApplication.run(GatewayrouterApplication.class, args); } @Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes().build(); } }
然后啟動Nacos,服務提供者和網關服務,使用瀏覽器訪問:http://127.0.0.1:8080/provider/helloProvider
搞定。
網關服務的其他高級應用,自己去看官網吧。
官網是最好的老師