spring

springboot 2.0 + springcloud ali

2019-05-31  本文已影响208人  JSM_SIMONS

前言:

springcloud gateway本身就是一个很优秀的路由网关,alibaba本身也对这个网关支持的很好,只需要把原来的eureka的配置换成nacos的就可以了,但由于这个地方我没有打算用netfilx的hystrix,所以我把原本hystrix的一些断路器配置去掉了,打算后期用Sentinel 替换。

代码配置

首先在pom文件中加入gateway以及nacos的配置

<?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">
    <parent>
        <groupId>com.gitee.simons.cloud</groupId>
        <artifactId>simons-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>simons-gateway</artifactId>
    <name>${project.artifactId} [web]</name>
    <packaging>jar</packaging>

    <properties>
        <start-class>com.gitee.simons.gateway.GatewayApplication</start-class>
    </properties>

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

        <!--swagger相关 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.gitee.simons.cloud</groupId>
            <artifactId>simons-base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>spring-boot</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${spring-boot.version}</version>
                        <configuration>
                            <mainClass>${start-class}</mainClass>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>


    </profiles>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!--解決持續集成無法找到main類 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

由于gateway要做swagger的路由分发所以这边加入了swagger的配置,如果不用swagger的朋友可以不加入,还是这个simons base也是一样,因为里面有一些基础的工具类会在拦截器里面用到所以引入,普通的只是小demo的话可以这两个配置去掉。

配置文件的话也是一样在,nacos服务发现的基础上加上gateway的路由配置即可
routes里面配置里面:

id:设置成对应服务的applicationname

uri: 其中lb => 就是loadbalance负载均衡的意思,就是说uri里面有这个nacos的那么就去找服务名为nacos的进行负载均衡

predicates:说明下面的信息依附于上面服务上,path对应的就是说要做路由的地址路径

filters: 这个是gateway的自定义过滤器的配置在后面的篇章会说到这个东西的详细配置

spring:
  application:
    name: gateway
  main:
    banner-mode: "off" #on or off
  profiles:
    active: deve
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
      - id: nacos
        uri: lb://nacos
        predicates:
        - Path=/nacos/**
        filters:
        - StripPrefix=1
#        - JwtCheck=true
server:
  port: 2001


management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS
package com.gitee.simons.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author jsm
 */
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan("com.gitee")
public class GatewayApplication {

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

    @Bean
    LoadBalancerInterceptor loadBalancerInterceptor(LoadBalancerClient loadBalance) {
        return new LoadBalancerInterceptor(loadBalance);
    }

}

只要上面三个配置里面配好了对与基本的路由来说就已经足够了那么把第一张的nacos 的测试项目中的test接口跑起来测试一下


image.png

当我们在开启相同的服务的时候,多次请求路由,会实现负载均衡的操作


image.png
image.png

ok完成

上一篇下一篇

猜你喜欢

热点阅读