idea+spring boot+spring cloud+eureka+gateway整合


1、創建一個maven項目

 

 

 

 next

 

Next

 Finish完成.

2、在創建好的maven項目上右鍵New->Module

 

選擇Spring initializr創建eureka注冊中心

 

 

Next

 

 

 

Next

 

 

 

Next

 

 

 Finish.

打開eureka-service項目下的application.properties配置文件,將.properties重命名為.yml,配置如下

server:
  port: 8001 #端口號

spring:
  application:
    name: eureka-service #服務名稱

eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false #是否從Eureka Server獲取注冊信息
    register-with-eureka: false #是否將自己注冊到Eureka Server
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 服務地址

 


 然后啟動類中增加@EnableEurekaServer注解就可以了

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}


啟動,瀏覽器輸入http://localhost:8001看到如下內容說明配置成功

 

 

 

3、創建服務提供者

cloud-demo右鍵->New->Module 重復第二步時的過程,只是選擇下圖項目創建

 

 

 

完成后打開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.2.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    <artifactId>admin-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>admin-api</name>
    <description>admin api</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>

        <!--新增-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

 


配置文件application.yml配置

server:
    port: 8081 #端口
spring:
    application:
        name: customer-center #服務名稱

eureka:
    client:
        service-url:
            defaultZone: http://localhost:8001/eureka/ #注冊中心地址
        

 

 

啟動,刷新注冊中心

 

 

成功。 

4、創建gateway服務

重復之前的創建過程,選擇Spring Cloud Routing->GateWay

 

 

 

創建完成后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.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xxxxxx</groupId>
    <artifactId>getway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>getway</name>
    <description>getway</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>

        <!--新增-->
        <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>
            <version>2.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>HikariCP</artifactId>
                    <groupId>com.zaxxer</groupId>
                </exclusion>
            </exclusions>
        </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>

配置文件application.yml

server:
  port: 8002 # 服務端口
spring:
  application:
    name: gateway-service # 服務名稱

  cloud:
    gateway:
      routes:
        - id: customer
          uri: lb://admin-api #eureka注冊中心存在的服務名稱
          predicates:
            - Path=/api/customer/** #路徑配置
          filters:
           - StripPrefix=1     #忽略Path配置的個數,此處為1代表訪問/api/customer/**時,會將api忽略,真實的訪問地址為lb://admin-api/customer/**,如果為2,則為lb://admin-api/**
        

在customer-center中創建一個controller類提供一個簡單的服務接口

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "customer")
public class UserController {
  @GetMapping(value = "user")
  public String user(){
    return "user";
  }
}

啟動gateway服務

在瀏覽器中輸入http://localhost:8002/api/customer/user和http://localhost:8081/customer/user得到的結果項目,說明配置成功

5、編寫gateway過濾器

第一種:使用全局GlobalFilter過濾器 

創建AuthorizeFilter類,只需要有@Component注解就可以了

package com.fulugame.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Created by ShiJiaoYun on 2020/5/13.
 */
@Component
@Slf4j
public class AuthorizeFilter  implements GlobalFilter, Ordered {

    private static final String AUTHORIZE_TOKEN = "token";


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("使用filter");
        ServerHttpRequest request = exchange.getRequest();
        HttpHeaders headers = request.getHeaders();
        String token = headers.getFirst(AUTHORIZE_TOKEN);
        if (null == token){
            token=request.getQueryParams().getFirst(AUTHORIZE_TOKEN);
        }
        ServerHttpResponse response = exchange.getResponse();
        if (StringUtils.isEmpty(token)){
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return  response.setComplete();
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

然后再通過http://localhost:8002/api/customer/user和http://localhost:8002/api/customer/user?token=12121訪問就會有兩種不同結果

 

這樣同樣可以實現過濾器效果


免責聲明!

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



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