您好,登錄后才能下訂單哦!
上幾篇主要講解了網(wǎng)關(guān)在單個(gè)服務(wù)的使用,在實(shí)際的工作中,服務(wù)的相互調(diào)用都是依賴于服務(wù)中心提供的入口來使用,服務(wù)中心往往注冊(cè)了很多服務(wù),如果每個(gè)服務(wù)都需要單獨(dú)配置的話,非常麻煩。Spring Cloud Gateway 提供了一種默認(rèn)轉(zhuǎn)發(fā)的能力,只要將 Spring Cloud Gateway 注冊(cè)到服務(wù)中心,Spring Cloud Gateway 默認(rèn)就會(huì)代理服務(wù)中心的所有服務(wù),下面就具體講解下。
本節(jié)案例中一共有四個(gè)工程,如下:
工程名 | 端口 | 作用 |
---|---|---|
sc-eureka-server | 8760 | 注冊(cè)中心 |
sc-service-gateway | 8761 | 路由網(wǎng)關(guān) |
sc-service-hi | 8762 | 服務(wù)提供者 |
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
server:
port: 8760
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: sc-eurka-server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8761
spring:
application:
name: sc-service-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lowerCaseServiceId: true
eureka:
client:
service-url:
defaultZone: http://localhost:8760/eureka/
配置說明:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8762
spring:
application:
name: sc-service-hi
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8760/eureka/
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ScServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run( ScServiceHiApplication.class, args );
}
@Value("${server.port}")
String port;
@GetMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "zhangsan") String name) {
return "hi " + name + " ,i am from port:" + port;
}
}
啟動(dòng)三個(gè)項(xiàng)目后,訪問 http://localhost:8761/sc-service-hi/hi?name=zhangsan~,返回如下:
hi zhangsan~ ,i am from port:8762
說明服務(wù)網(wǎng)關(guān)轉(zhuǎn)發(fā)成功了。
在上面的例子中,向sc-service-gateway發(fā)送的請(qǐng)求時(shí),url必須帶上服務(wù)名sc-service-hi這個(gè)前綴,才能轉(zhuǎn)發(fā)到sc-service-hi上,轉(zhuǎn)發(fā)之前會(huì)將sc-service-hi去掉。有時(shí)服務(wù)名稱過長(zhǎng),不易使用,需要自定義路徑并轉(zhuǎn)發(fā)到具體的服務(wù)上。配置如下:
server:
port: 8761
spring:
application:
name: sc-service-gateway
cloud:
gateway:
discovery:
locator:
enabled: false
lowerCaseServiceId: true
routes:
- id: sc-service-hi
uri: lb://SC-SERVICE-HI
predicates:
- Path=/demo/**
filters:
- StripPrefix=1
eureka:
client:
service-url:
defaultZone: http://localhost:8760/eureka/
logging:
level:
org.springframework.cloud.gateway: debug
在上面的配置中,配置了一個(gè)Path 的 predict,將以/demo/**開頭的請(qǐng)求都會(huì)轉(zhuǎn)發(fā)到uri為lb://SC-SERVICE-HI的地址上,lb://SC-SERVICE-HI即sc-service-hi服務(wù)的負(fù)載均衡地址,并用StripPrefix的filter 在轉(zhuǎn)發(fā)之前將/demo去掉。同時(shí)將spring.cloud.gateway.discovery.locator.enabled改為false,如果不改的話,之前的localhost:8761/sc-service-hi/hi?name=zhangsan~這樣的請(qǐng)求地址也能正常訪問,因?yàn)檫@時(shí)為每個(gè)服務(wù)創(chuàng)建了2個(gè)router。
重啟sc-service-gateway項(xiàng)目后,訪問 http://localhost:8761/demo/hi?name=zhangsan~ ,返回如下:
hi zhangsan~ ,i am from port:8762
服務(wù)網(wǎng)關(guān)轉(zhuǎn)發(fā)成功,說明自定義請(qǐng)求路徑生效了。
源碼:https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter13
歡迎關(guān)注我的公眾號(hào)《程序員果果》,關(guān)注有驚喜~~
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。