欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

GateWay動態(tài)路由與負載均衡詳細介紹

 更新時間:2022年11月17日 14:59:11   作者:做一道光  
這篇文章主要介紹了GateWay動態(tài)路由與負載均衡,GateWay支持自動從注冊中心中獲取服務列表并訪問,即所謂的動態(tài)路由

概述

從之前的配置里面我們可以看到我們的 URL 都是寫死的,這不符合我們微服務的要求,我們微服務是只要知道服務的名字,根據(jù)名字去找,而直接寫死就沒有負載均衡的效果了 默認情況下 Gateway 會根據(jù)注冊中心的服務列表,以注冊中心上微服務名為路徑創(chuàng)建動態(tài)路 由進行轉發(fā),從而實現(xiàn)動態(tài)路由的功能。

需要注意的是 uri 的協(xié)議為 lb( load Balance ),表示啟用 Gateway 的負載均衡功能。

lb://serviceName 是 spring cloud gateway 在微服務中自動為我們創(chuàng)建的負載均衡 uri

協(xié)議:就是雙方約定的一個接頭暗號

http : //

項目實例

1.gateway-server模塊

1.1.pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.it</groupId>
    <artifactId>gateway-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway-server</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

1.2.application.yml文件

server:
  port: 80
spring:
  application:
    name: gateway-server
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true #開啟動態(tài)路由  開啟通過應用名稱找到服務的功能
          lower-case-service-id: true #開啟服務名稱小寫
eureka:
  client:
    service-url:
      defaultZone: http://192.168.174.133:8761/eureka
    registry-fetch-interval-seconds: 3
  instance:
    hostname: localhost
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}

1.3.主函數(shù)類

package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GatewayServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
    }
}

2.login-service模塊

2.1.pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.it</groupId>
    <artifactId>login-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>login-service</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2.application.yml文件

server:
  port: 8081
spring:
  application:
    name: login-service
eureka:
  client:
    service-url:
      defaultZone: http://192.168.174.133:8761/eureka
    registry-fetch-interval-seconds: 3
  instance:
    hostname: localhost
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}

2.3.LoginController文件

package com.it.controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.UUID;
public class LoginController {
    @GetMapping("doLogin")
    public String doLogin(String name,String pwd){
        System.out.println(name);
        System.out.println(pwd);
        String s = UUID.randomUUID().toString();
        return s;
    }
}

2.4.主函數(shù)類

package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class LoginServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(LoginServiceApplication.class, args);
    }
}

3.功能測試

啟動動態(tài)路由后,訪問路徑時需要在localhost后面加上,要訪問服務的名稱,后面再跟上這個服務中的方法接口名

如果不加服務名稱,還是按照以前的方法寫,會導致訪問報404

到此這篇關于GateWay動態(tài)路由與負載均衡詳細介紹的文章就介紹到這了,更多相關GateWay動態(tài)路由與負載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導入Excel的解決方法

    Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導入Excel的解決方法

    本篇文章介紹了,Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導入Excel的解決方法。需要的朋友參考下
    2013-05-05
  • 可視化定時任務quartz集成解析全過程

    可視化定時任務quartz集成解析全過程

    在開發(fā)中有很多定時任務都不是寫死的而是可以人為配置并且寫到數(shù)據(jù)庫中的,下面這篇文章主要給大家介紹了關于可視化定時任務quartz集成解析的相關資料,需要的朋友可以參考下
    2022-10-10
  • SpringBoot中攔截器和動態(tài)代理的區(qū)別詳解

    SpringBoot中攔截器和動態(tài)代理的區(qū)別詳解

    在?Spring?Boot?中,攔截器和動態(tài)代理都是用來實現(xiàn)功能增強的,所以在很多時候,有人會認為攔截器的底層是通過動態(tài)代理實現(xiàn)的,所以本文就來盤點一下他們兩的區(qū)別,以及攔截器的底層實現(xiàn)吧
    2023-09-09
  • 詳解Spring Cloud Gateway 限流操作

    詳解Spring Cloud Gateway 限流操作

    這篇文章主要介紹了詳解Spring Cloud Gateway 限流操作,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 如何解決Spring的UnsatisfiedDependencyException異常問題

    如何解決Spring的UnsatisfiedDependencyException異常問題

    這篇文章主要介紹了如何解決Spring的UnsatisfiedDependencyException異常問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • springboot?整合表達式計算引擎?Aviator?使用示例詳解

    springboot?整合表達式計算引擎?Aviator?使用示例詳解

    本文詳細介紹了Google?Aviator?這款高性能、輕量級的?Java?表達式求值引擎,并通過詳細的代碼操作演示了相關API的使用以及如何在springboot項目中進行集成,感興趣的朋友一起看看吧
    2024-08-08
  • java操作hdfs的方法示例代碼

    java操作hdfs的方法示例代碼

    這篇文章主要介紹了java操作hdfs的相關資料,在本地配置Hadoop和Maven的環(huán)境變量,首先需從官網(wǎng)下載與服務器相同版本的Hadoop安裝包,配置環(huán)境變量后,引入Maven的配置文件,以便管理項目依賴,最后,編寫代碼實現(xiàn)對HDFS的連接和操作,完成數(shù)據(jù)的讀寫,需要的朋友可以參考下
    2022-02-02
  • SpringBoot整合RocketMQ實現(xiàn)發(fā)送同步消息

    SpringBoot整合RocketMQ實現(xiàn)發(fā)送同步消息

    RocketMQ 是一款開源的分布式消息中間件,由阿里巴巴開源,它具有高可用性、高性能、低延遲等特點,廣泛應用于阿里巴巴集團內(nèi)部以及眾多外部企業(yè)的業(yè)務系統(tǒng)中,本文給大家介紹了SpringBoot整合RocketMQ實現(xiàn)發(fā)送同步消息,需要的朋友可以參考下
    2024-04-04
  • Spring AOP訪問目標方法的參數(shù)操作示例

    Spring AOP訪問目標方法的參數(shù)操作示例

    這篇文章主要介紹了Spring AOP訪問目標方法的參數(shù)操作,結合實例形式詳細分析了spring面向切面AOP訪問目標方法的參數(shù)相關實現(xiàn)步驟與操作注意事項,需要的朋友可以參考下
    2020-01-01
  • spring中WebClient如何設置連接超時時間以及讀取超時時間

    spring中WebClient如何設置連接超時時間以及讀取超時時間

    這篇文章主要給大家介紹了關于spring中WebClient如何設置連接超時時間以及讀取超時時間的相關資料,WebClient是Spring框架5.0引入的基于響應式編程模型的HTTP客戶端,它提供一種簡便的方式來處理HTTP請求和響應,需要的朋友可以參考下
    2024-08-08

最新評論