SpringCloud使用zuul反向代理和负载均衡

网关zuul是cloud的核心组件之一,感觉和nginx类似,具有反向代理和负载等功能,这里要说一下反向代理和负载均衡。

实现负载首先要实现服务注册功能,zuul做为微服务客户端首先和其他客户端一样注册到eureka server中。
下面是我的服务结构,在注册中心可以看到
在这里插入图片描述其中,ZUUL是网关层,CLIENT-A,CLIENT-B做为测试客户端,一共四个应用(包括注册中心)

代码结构如下
在这里插入图片描述
demo1是注册中心,2,3为客户端,zuuls是路由转发zuul服务。
打开demo1 pom.xml
添加了

     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

做为服务中心的相关jar

打开demo2,3(客户端) pom.xml

添加了

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

做为服务客户端的jar

打开zuuls pom.xml
除了服务客户端的jar之外,还有zuul的相关jar

  <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>

看网上有一些其他artifactId,目前新版的需要用netflix包装的服务,原来那些用了会报错,这些更新其实挺烦人的,个人感觉是个缺点,老版的spring和新版spring各种不一致。

jar包引完之后设置一下启动类注解
demo1(注册中心),添加注解
@EnableEurekaServer
在这里插入图片描述

demo2,demo3(客户端),启动类添加注解
@EnableEurekaClient
在这里插入图片描述
同时,在demo2,demo3分别创建一个接口,为了负载均衡测试用。
demo2中

    @RequestMapping("/hello")
    public String hello() {
        logger.info("*********" + 001);
        return "hello,this is client-client001";
    }

demo3中

       @RequestMapping("/hello")
	    public String hello() {
	        logger.info("*********" + 002);
	        return "hello,client-client002";
	    }  

zuuls启动类添加注解(既是客户端也是网关层)
@EnableZuulProxy
@EnableEurekaClient
在这里插入图片描述

打开demo1(服务中心)application.properties
添加如下配置

server.port=8080 
spring.application.name=eu-server   #server的id
eureka.instance.hostname=localhost 
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
#registerWithEureka: false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
#fetchRegistry: false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/  

打开demo2(客户端)application.properties

server.port=9090
#设置服务名
spring.application.name=client-A
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka

打开demo3(客户端)application.properties,和2类似

server.port=9091
#设置服务名
spring.application.name=client-B
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone= http://localhost:8080/eureka

唯一区别是spring.application.name的值,这个在路由映射路径的时候会用到,注册中心显示的名称也是这个值

打开zuuls(路由)application.properties
zuul转发有两种方式,一种是url,另一种是serviceid的映射,这里是第二种方式,图下中表示/api-a/ 路径可以访问到id为client-A(demo2)下的方法,/api-b/ 路径可以访问到id为client-B(demo3)下的方法,

server.port=7070
#设置服务名
spring.application.name=zuul
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=client-A

zuul.routes.api-b.path=/api-b/**
zuul.routes.api-b.serviceId=client-B
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka

启动4个项目
访问网关层地址+映射
打开路径
http://localhost:7070/api-a/hello
结果如图所示:
在这里插入图片描述

打开http://localhost:7070/api-b/hello
结果如图所示:
在这里插入图片描述

那么如何负载呢,只要把客户端(demo2,3)的serviceId改成一样的就可以了
比如把demo3的spring.application.name值改成client-A

在这里插入图片描述
重启demo3,继续访问 http://localhost:7070/api-a/hello

第一次访问到了demo2
在这里插入图片描述
刷新页面,访问到了demo3
在这里插入图片描述
就这样,一个简单的反向代理和负载就实现了,关于负载,这里是轮询的方式,其他方式没有讲到。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值