【四】Spring Cloud Zuul 使用

一、简介

前面已经启动了服务注册Eurake、启动了两个服务item和order

【一】RestTemplate+Eureka 服务A调用服务B(无feign)

【二】Spring Cloud Feign+Eureka 使用

本篇加入服务网关用Zuul后:

无论是请求item还是order系统,前端都统一访问请求到Zuul,由Zuul根据自己配置的路由规则转发到item或者order上。

有了服务网关后,我们可以在服务网关部分加入权限校验,符合权限校验的才放行到后面的系统中去。

本篇只演示基本的使用,在Zuul再加入拦截去鉴权以后再写。

二、代码

创建项目zuul

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>zuul</artifactId>

    <dependencies>
        <!--整合zuul网关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!--整合eureka客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

server:
  port: 8087 #服务端口
spring:
  application:
    name: app-zuul #指定服务名
###服务注册到eureka注册中心的地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka/
    ###因为该应用为服务提供者,是eureka的一个客户端,需要注册到注册中心
    register-with-eureka: true
    ###是否需要从eureka上检索服务
    fetch-registry: true
  instance:
    prefer-ip-address: true #将自己的ip地址注册到Eureka服务中
    ip-address: localhost
    #instance-id: ${spring.application.name}###${server.port} #指定实例id
zuul:
  routes: #定义服务转发规则
    item-service: #item-service这个名字是任意写的
      path: /item-service/** #匹配item-service的请求app-item服务
      #url: http://127.0.0.1:8081 #真正的微服务地址
      serviceid: app-item
    order-service: #名字尽量和业务系统相关
      path: /order-service/** #匹配order-service的请求app-order服务
      serviceid: app-order

启动文件ZuulApp.java

package com.sid;

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

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

请求zuul的地址http://localhost:8087/item-service/item/2

该请求会被路由到http://127.0.0.1:8081/item/2上(这是在zuul的application.yml配置文件中配置的)

结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值