06.SpringCloud之Zuul路由网关

一.简介

Zuul实现的是路由网关微服务,所有路由网关微服务都需要注册到注册中心。Zuul网关路由微服务代理客户端访问微服务,即消费端不直接访问微服务,而是访问路由网关微服务,这样可以在消费端与微服务之间构建过滤、认证操作。

二.Zuul路由网关模块

1.新建zuul网关微服务模块

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</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-netflix-zuul</artifactId>
</dependency>

2.编写application.yml配置文件

server:
  port: 9501
spring:
  application:
    name: service-zuul

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/service-eureka/eureka

3.编写路由网关启动类

package com.vincent;

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

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

4.访问注册中心:http://localhost:7001/service-eureka/,zuul网关服务也注册成功
在这里插入图片描述

5.微服务可以通过网关代理访问,访问:http://localhost:9501/service-user/service-user/detail?id=1

在这里插入图片描述
zuul网关代理访问时,采用的时Eureka注册中心的服务名称

6.修改Feign远程接口映射,通过zuul网关代理访问微服务

package com.vincent.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "service-zuul",fallbackFactory = UserServiceFallbackFactory.class)
@RequestMapping("/service-user/service-user")//前一个service-user 是微服务的名称,后一个service-user是微服务的context-path
public interface IUserService {
    @GetMapping("/detail")
    Object detail(@RequestParam("id") Integer id);
}

7.访问消费端:http://localhost:9001/detail?id=1
在这里插入图片描述

三.Zuul路由配置

1.Zuul默认通过微服务名称进行代理访问,zuul 可以自定义路由访问规则,即给注册中心的微服务名称取一个别名

server:
  port: 9501
spring:
  application:
    name: service-zuul

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/service-eureka/eureka
zuul:
  #禁用服务代理名称
  ignored-services: "*"
  routes:
    #配置服务名称别名
    service-user: /user-proxy/**

2.访问消费端:http://localhost:9001/detail?id=1
在这里插入图片描述
3.修改Feign访问的微服务名称为Zuul网关配置的别名

package com.vincent.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "service-zuul",fallbackFactory = UserServiceFallbackFactory.class)
@RequestMapping("/user-proxy/service-user")//前一个service-user 是微服务的名称,后一个service-user是微服务的context-path
public interface IUserService {
    @GetMapping("/detail")
    Object detail(@RequestParam("id") Integer id);
}

4.再次访问消费端
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Zllvincent/article/details/108434086