springcloud zuul getaway

网关的优势

使用网关优点:

易于监控。可在微服务网关收集监控数据并将其推送到外部系统进行分析。
易于认证。可在微服务网关上进行认证。然后再将请求转发到后端的微服务,而无须在每个微服务中进行认证。
减少了客户端与各个微服务之间的交互次数。

新建springboot maven项目

此处变为了父子项目

引入相关的jar


<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>
        <artifactId>cloud-parentartifactId>
        <groupId>com.cloud.parentgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>com.cloud.parentgroupId>
    <artifactId>cloud-zuulartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>
    <description>user service Demo project for Spring clouddescription>


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

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

project>

新增配置类application

server:
  port: 9996
eureka:
  instance:
    hostname: 127.0.0.1
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:9999/eureka/
spring:
  application:
    name: springcloud-zuul-getaway    #注册的系统名

新建启动类

@EnableZuulProxy 这个的意思是启用zuul网关
@EnableDiscoveryClient 这个的意思将系统服务注册到eureka上可以用@EnableEurekaClient代替
EnableDiscoveryClient

package com.cloud.zuul.getaway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * @author :mmzs
 * @date :Created in 2020/3/6 15:42
 * @description:zuul网关
 * @modified By:
 * @version: 1.0$
 */
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class);
    }
}

此时启动效果:

1、登录注册中心:可以看到user、bpm、zuul
springcloud zuul getaway_第1张图片 2、访问user的某一个接口:http://localhost:9998/user/register
在这里插入图片描述3、通过网关访问:http://localhost:9996/eureka-user-manager/user/register
在这里插入图片描述注意:eureka-user-manager是service name

正常开发时需要将service name隐藏起来,增加如下配置

添加映射关系:

serviceId:服务的名称
path:映射的路径
**的意思是可以匹配任何多级目录的意思.
*为单级目录
zuul:
  routes:
    eureka-user-manager:
      path: /uc/**
      serviceId: eureka-user-manager
    feign-consumer:
      path: /feign-consumer/**
      serviceId: feign-consumer

完成上面的工作后,Zuul已经可以正常运行,但是要想为微服务工作还需要进行如下工作

增加过滤器的功能

Spring Cloud的API网关不但可以实现类似NGINX+Lua强大的路由分发,实现动静页面的分流,更重要可以实现对所有发往后端微服务请求的拦截。Zuul主要有四种类型的过滤器,我们可以为特定的url模式添加任意数量的过滤器。

“pre” 预过滤器 - 在路由分发一个请求之前调用。
“post” 后过滤器 - 在路由分发一个请求后调用。
“route” 路由过滤器 - 用于路由请求分发。
“error” 错误过滤器 - 在处理请求时发生错误时调用

zuul网关介绍
SpringCloud之Zuul网关过滤器,处理XSS攻击和SQL注入

添加pre过滤器

添加步骤

HttpServletRequestWrapper类

你可能感兴趣的:(spring,cloud)