手把手搭建SpringCloud,第五篇——使用Zuul构建微服务网关

本文介绍微服务网关的作用及其实现方式,包括如何通过网关简化客户端与多个微服务间的交互,解决跨域请求等问题,并提供了一种利用Spring Cloud Zuul实现微服务网关的具体案例。

前言概述

为什么要使用微服务网关?

使用场景:例如一个手机端得购票系统,可能会调用多个微服务得接口,才能完成一次购票得业务流程。

如果让客户端直接与各个微服务通信,会有以下问题:

  • 客户端会多次请求不同的微服务,增加客户端的复杂性。
  • 存在跨域请求,在一定场景下处理相对复杂。
  • 认证复杂,每个服务都要独立认证。
  • 难以重构,随着项目的迭代,可能需要重新划分微服务。例如,可能将多个服务合并成一个或者将一个服务拆分成多个。如果客户端直接与微服务通信,那么重构将会很难实现。
  • 某些微服务可能使用了防火墙/浏览器不友好协议,直接访问会有一定的困难。

 以上的这些问题可借助服务网关解决,微服务网关是介入客户端和服务器之间的中间层,所有的外部请求都会先经过服务网关,使用服务网关后,架构图

服务网关的优点:

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

概述就将到这,其他相关知识可以百度一下,我这写太多可能也没多少人耐心看,重要的是实战,那么下面就实战开始


项目实战

开发工具:STS

新建项目前,请确认工作环境已经配置好了Maven。

本章提及的其他测试项目,请阅读其他几篇博客,在这里就不从头新建项目了,直接用直接的项目添加相关依赖。

pom.xml

<!-- Eureka Client -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<!-- Feign -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<!-- 断路器 -->
	  	<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
			<!-- 断路器仪表盘 actuator -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- hystrix-dashboard -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
		</dependency>
		<!-- zuul -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zuul</artifactId>
		</dependency>

 application.yml

server:
  port: 2003
#---------------------------------------Eureka配置----------------------------------------------
eureka:
  client:
    service-url:
      #配置Eureka Server的地址,因为是要注册服务到Eureka Server
      defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
  instance:
    #将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
    prefer-ip-address: true
    #指明ip
    instance-id: 10.8.65.38:dev-feign-server-test:2003
    #告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
    lease-expiration-duration-in-seconds: 2
    #每间隔1s向服务器发送一次心跳,证明自己还活着
    lease-renewal-interval-in-seconds: 1
    
spring:
  application:
    name: dev-feign-server-test
    
#---------------------------------------断路器配置启用,注意:要配置在请求端------------------------
feign: 
  hystrix: 
    enabled: true
#---------------------------------------Zuul配置--------------------------------------------------
zuul:
  routes:
    api-a:   #这个名字可以随意
      path: /api-feign/**  #service-id对应的路径
      service-id: dev-feign-server #Eureka服务中实例名
    api-b:  #这个名字可以随意
      path: /api-dev/** #service-id对应的路径
      service-id: dev-feign-server2 #Eureka服务中实例名
    

启动类添加注解

package com.portal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient //开启服务注册
@EnableFeignClients//开启Feign功能
@EnableHystrix //开启断路器
@EnableHystrixDashboard  //开启Hystrix Dashborad仪表盘
@EnableCircuitBreaker
@EnableZuulProxy //zuul代理
public class SpringcloudFeignServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudFeignServerApplication.class, args);
	}

}

启动这些个项目

访问http://localhost:1001,看Eureka中服务实例名 和配置文件中  service-id  必须是一致,不然找不到

 

 开始测试,测试原接口

再测试配置的路由接口

 很明显是成功的,配置文件可以添加多个服务实例,另外Zuul路由配置的方式有很多种。

1.自定义指定微服务的访问路径

配置zuul.routes,指定微服务的serviceId = 指定路径 即可。

zuul:
  routes:
    dev-feign-server: /api-feign/**   #dev-feign-server  Eureka服务中实例名

 

 测试,没有问题

 2.忽略指定微服务(Zuul就会忽略配置中的这两个服务)

zuul:
  ignored-services:
    dev-feign-server,dev-feign-server2    #多个用逗号分隔

测试,没有问题

 3.忽略所有微服务,只路由指定的微服务

zuul:
  ignored-services: '*' #使用'*'可忽略所有微服务
  routes:
    dev-feign-server: /api-feign/** 

4,同时指定Path 和Url

zuul:
  routes:
    api-a:
      url: http://localhost:2001/   #指定的url
      path: /api-feign/**           #url对应的路径

注:这种配置方式的路由不会作为HystrixCommand执行,同时也不能使用Ribbon来负载均衡多个URL,并且不能破坏Zuul的Hystrix,Ribbon特性。 

5.同时指定path和URL,并且不破坏Zuul和Hystrix,Ribbon的特性 (推荐)

zuul:
  routes:
    api-a:   #这个名字可以随意
      path: /api-feign/**  #service-id对应的路径
      service-id: dev-feign-server #Eureka服务中实例名
ribbon:
  eureka:
    enabled: false #为Ribbon禁用Eureka
dev-feign-server:
  ribbon:
    listOfServers: localhost:2001,localhost:2002

测试1,

测试2,


 

总结:笔者写博客不仅仅是为了分享,也是为了给自己做笔记。

以上方法亲测有效,如果问题可下方回复,

不当之处,请多指教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值