zuul 是netflix开源的一个API Gateway 服务器

所有从设备或网站来的请求都会经过Zuul到达后端的Netflix应用程序。

作为一个边界性质的应用程序,Zuul提供了动态路由、监控、弹性负载和安全功能。

实现反向代理

1.服务注册发现中心Consul

启动

consul agent -dev

2.服务端

provider和provider1

spring boot 版本 2.2.1.RELEASE

(1)添加依赖

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

(2)配置

server.port=8010

spring.application.name=service-provider
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

(3)测试方法

package com.xyz.provider.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
} }

(4)启动类

package com.xyz.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication { public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
} }

provide1的

server.port=8011

测试方法

package com.xyz.provider1.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,another provider";
} }

3.网关

zuul

Spring boot版本 2.1.8.RELEASE

上面用的Spring boot版本为 2.2.1.RELEASE

但是启动时遇到了报错,因此改成了这个版本(报错问题

(1)添加依赖

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

(2)添加配置

server.port=8090

spring.application.name=service-zuul
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always zuul.routes.api.path=/api/**
zuul.routes.api.serviceId=service-provider

(3)启动类

package com.xyz.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy
@SpringBootApplication
public class ZuulApplication { public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
} }

启动Consul

启动provider、provider1

启动 zuul

访问http://127.0.0.1:8090/api/hello

结果输出:

  hello,provider和hello,another provider

结果交替出现的,负载均衡器采用的是轮询的方式

示例 https://gitee.com/babybeibeili/zuul_consul.git

最新文章

  1. .NET MVC Razor模板预编译(二)
  2. Docker Container 配置独立IP
  3. javascript 中的 bind (编辑中。。。。)
  4. APACHE 在windows下的配置
  5. LAMP平台部署及应用
  6. Enabling CORS in WCF
  7. linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2
  8. spring 集成mongo配置
  9. Linux 6.4 partprobe出现warning问题
  10. 关于移动web教程免费发布
  11. [usaco18Feb] New Barns
  12. [转载]Fiddler为所欲为第四篇 直播源抓取与接口分析 [四]
  13. LOJ #2142. 「SHOI2017」相逢是问候(欧拉函数 + 线段树)
  14. tedu训练营day01
  15. Linux 查看内存状态
  16. Apache和Nginx负载均衡集群及测试分析
  17. B1048 数字加密
  18. 21.5.3 Updatable and Insertable Views
  19. 自定义相机下使用clippingNode注意事项
  20. 右值引用与转移语义(C++11)

热门文章

  1. C# LDAP认证登录类参考
  2. leetcode求峰值,js实现
  3. 微信小程序主要开发语言
  4. C# Socket服务器及多客户端连接示例
  5. SpringBoot配置多注册中心(yml,properties)
  6. seq2seq模型详解及对比(CNN,RNN,Transformer)
  7. vs2017 curl7.6编译
  8. maven 配置文件
  9. python递归和内置方法
  10. Java 内存分配(转)