Spring Cloud Gateway(二):Spring Cloud Gateway整合Eureka應用


Spring Cloud Gateway 應用概述

下面的示例啟動兩個服務:gataway-server 和 user-service 都注冊到注冊中心 Eureka上,客戶端請求后端服務【user-service】,網關提供后端服務的統一入口。網關通過負載均衡將客戶端請求轉發到具體的后端服務。

image

序號 服務名稱 端口號 備注
1 gateway-server 9000 網關服務
2 user-service 8001 用戶服務
3 eureka-server 8888 注冊中心

用戶服務搭建

用戶服務比較簡單,只需要注冊到注冊中心並提供一個測試接口 /user

Maven 依賴

<!--注冊中心客戶端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--actuator 監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--配置中心客戶端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

配置文件

#服務暴露頓口號
server.port=8001
#服務名稱
spring.application.name=user-service

#注冊中心地址
eureka.client.serviceUrl.defaultZone=${EUREKA_SERVICE_URL:http://localhost:8888}/lb/eureka/

management.endpoint.conditions.enabled=true
#優先使用IP地址注冊
eureka.instance.prefer-ip-address=true
##服務所屬分組名稱
#eureka.instance.app-group-name=lkf_group
#服務實例id
eureka.instance.instanceId=${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}
        
#開放所有頁面節點  默認只開啟了health、info兩個節點
management.endpoints.web.exposure.include=*

暴露服務接口

對外暴露 /user 接口,返回字符串 I’m ok

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

    @GetMapping("/user")
    public String getUser() {
        return "I'm ok";
    }
}

網關服務搭建

網關服務提供路由配置、路由斷言和過濾器等功能。下面將會分別實現這些功能

Maven 引用

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--注冊中心客戶端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

配置文件

注冊到注冊中心並讀取服務列表,支持向注冊中心服務列表中的服務路由,這里需要注意,如果 spring.cloud.gateway.discovery.locator.lowerCaseServiceId 不為true,則只能根據服務實例id的大寫進行轉發,不支持小寫的serviceId

#服務暴露頓口號
server.port=9000
#服務名稱
spring.application.name=gateway-server

#注冊中心地址
eureka.client.serviceUrl.defaultZone=${EUREKA_SERVICE_URL:http://localhost:8888}/eureka/

management.endpoint.conditions.enabled=true
#優先使用IP地址注冊
eureka.instance.prefer-ip-address=true
##服務所屬分組名稱
#eureka.instance.app-group-name=lkf_group
#服務實例id
eureka.instance.instanceId=${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}

#開放所有頁面節點  默認只開啟了health、info兩個節點
management.endpoints.web.exposure.include=*
#啟用基於服務發現的路由定位
spring.cloud.gateway.discovery.locator.enabled=true
#啟用服務實例id名稱小寫支持
spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true

路由斷言

路由斷言有很多種,根據請求時間、Host地址、請求路徑和請求方法等,這里實現一個基於路徑的路由斷言匹配

 @Bean
    public RouterFunction<ServerResponse> testFunRouterFunction() {
        RouterFunction<ServerResponse> route = RouterFunctions.route(
                RequestPredicates.path("/test"), request -> ServerResponse.ok()
                        .body(BodyInserters.fromObject("I am testing")));
        return route;
    }

當請求路徑為 /test 時,直接返回結果為 I am testing

過濾器

網關經常需要對路由請求進行過濾,對符合條件的請求進行一些操作,例如:增加請求頭、增加請求參數、增加響應頭和斷路器等功能。

 @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes().route(r -> r.path("/test/custom").uri("http://ww.baidu.com"))
                .route(r -> r.path("/user/**").uri("lb://user-service"))
                .build();

    }

當請求路徑為 /test/custom 時,網關將請求轉發到 http://ww.baidu.com;

當請求路徑為 /user 時,網關將請求轉發到用戶服務的/user 接口 ,/user-service/user

客戶端訪問

向網關發送請求:http://127.0.0.1:9000/user ,
被網關轉發到 http://127.0.0.1:9000/user-service/user,得到結果為:I’m ok


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM