spring-cloud (四) 于spring-cloud-zuul的路由网关设置

由于微服务的日益增多,管理也会不方便,所以需要一个可以集中管理所有服务的功能(类似sevelet的filter),可以在此做同一的权限入口管理

新建一个模块spring-cloud-gateway

增加zuul及相关依赖如下:

pom.xml


<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.0modelVersion>

    <artifactId>spring-cloud-gatewayartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
        <file.encoding>UTF8file.encoding>
        <spring-cloud.version>Finchley.SR2spring-cloud.version>
    properties>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.2.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-zuulartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
    
project>

application.properties

server.port=8886
spring.application.name=gateway-service
eureka.client.service-url.defaultZone=http://localhost:8881/eureka/

#调用路径服务的路径
zuul.routes.eureka-client.path=/eureka-client/**

#调用服务的链接(不推荐,失去了均衡负载功能,只能使用一个服务)
#zuul.routes.lisiHello.url=http://localhost:8882/
#调用服务的id,即调用服务的名称 spring.application.name
zuul.routes.eureka-client.serviceId=eureka-client

主方法只需要添加相关注解即可
application.java

package top.littlematch.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

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

依次启动eureka-server,eureka-client,gateway-service再调用

http://localhost:8886/hello?name=match

浏览器返回:

hello! match

gitee代码地址:https://gitee.com/leftbehindmatches/spring-cloud-examples/tree/master/spring-cloud-gateway

你可能感兴趣的:(Java,SpringBoot,eureka,springcloud,zuul,gateway,spring-cloud入门)