SpringCloud初體驗:4、API GateWay 服務網關

網關服務不少,好比:Zuul、Kong、spring cloud gateway ……, 這裏不糾結哪一種性能好,本次體驗是用的 spring cloud gatewayjava

更多網關比較能夠了解這篇文章: 微服務網關哪家強?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差別web

 

配置網關服務步驟:spring

一、初始化一個 SpringBoot 應用apache

二、引入相關依賴app

 

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zjj7</groupId>
    <artifactId>gateway_server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>gateway_server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <!--配置倉庫-->
    <repositories>
        <repository>
            <id>aliRepository</id>
            <name>aliRepository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <!--cloud 引來-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-web</artifactId>-->
        <!--</dependency>-->

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

        <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-gateway</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
pom.xml 的配置代碼

 

 

三、修改配置文件maven

 

spring:
  application:
    name: gateway_server
  cloud:
    gateway:
      default-filters:
      routes:
      - id:  user-server
        # lb表明從註冊中心獲取服務
        uri: lb://user
        predicates:
        # 轉發該路徑
        - Path=/user/**
        # 帶前綴
        filters:
        - StripPrefix=1
      - id:  publish-server
        uri: lb://publish
        predicates:
        - Path=/publish/**
        filters:
        - StripPrefix=1

#服務註冊中心端口號
server:
  port: 6400

#服務註冊中心實例的主機名、端口
#是否向服務註冊中心註冊本身
#是否檢索服務
#服務註冊中心的配置內容,指定服務註冊中心的位置
eureka:
  port:
    6110
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${eureka.port}/eureka/
application.yml 的 相關配置

 

 

 

 

這裏和其它服務不一樣的是: 啓動類上面除了默認註解,無需添加更多ide

 

這時,一個簡單的網關服務就搭建完了, 啓動後全部的 API 就能以網關服務的地址爲入口開始訪問了spring-boot

相關文章
相關標籤/搜索