SpringCloud第五篇:路由网关(zuul)(Finchley版本)

       在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现服务消费负载均衡断路器智能路由配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个简单的微服务系统如下图:

       在Spring Cloud微服务系统中,一种常见的负载均衡方式是:客户端的请求首先经过负载均衡zuulNgnix),再到达服务网关(zuul集群),然后再到具体的服务。服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件配置服务管理(下一篇文章讲述),配置服务的配置文件放在git仓库,方便开发人员随时改配置。


 一、Zuul简介

       Zuul的主要功能是路由转发过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。

二、创建SpringCloud_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.0</modelVersion>

  <artifactId>SpringCloud_Zuul</artifactId>
  <packaging>jar</packaging>
  
  <parent>
	 <groupId>com</groupId>
  	 <artifactId>SpringCloud_Common</artifactId>
     <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <name>SpringCloud_Zuul</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
     <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
  </dependencies>
</project>

       在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:

package com.zit;

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

@SpringBootApplication
/*@EnableZuuProxy开启路由功能*/
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
public class SpringCloudZuulApplication {
	
    public static void main( String[] args ){
    	
        SpringApplication.run(SpringCloudZuulApplication.class, args);
    }
}

       application.properties内容如下:

server.port=8769
#指定注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=service-zuul
#以/api-a/开头的请求都转发给service-ribbon服务
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=service-ribbon
#以/api-b/开头的请求都转发给service-feign服务
zuul.routes.api-b.path=/api-b/**
zuul.routes.api-b.serviceId=service-feign

       依次启动SpringCloud_ServerSpringCloud_ClientSpringCloud_Customer_Rest_RibbonSpringCloud_Customer_FeignSpringCloud_Zuul这五个工程。

       打开浏览器访问:http://localhost:8769/api-a/hi?name=forezp ;浏览器显示:

        打开浏览器访问:http://localhost:8769/api-b/hi?name=forezp ;浏览器显示:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值