Spring boot2X负载均衡和反向代理实现过程解析
作者:慕尘 时间:2023-02-06 04:18:53
这篇文章主要介绍了Spring boot2X负载均衡和反向代理实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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
来源:https://www.cnblogs.com/baby123/p/11907377.html
标签:spring,boot,负载,均衡,反向,代理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析Java设计模式编程中的单例模式和简单工厂模式
2021-10-13 15:27:54
![](https://img.aspxhome.com/file/2023/3/69103_0s.gif)
kotlin实现五子棋单机游戏
2022-10-16 03:20:09
![](https://img.aspxhome.com/file/2023/9/138319_0s.jpg)
java中ZXing 生成、解析二维码图片的小示例
2022-07-24 11:50:39
![](https://img.aspxhome.com/file/2023/3/125633_0s.png)
详解Android获取设备唯一ID的几种方式
2022-07-15 12:33:41
java对象拷贝详解及实例
2023-01-20 08:27:00
Java定时器Timer使用方法详解
2023-08-25 17:32:20
![](https://img.aspxhome.com/file/2023/8/76528_0s.png)
Java中i++与++i的区别和使用
2022-03-20 18:08:18
![](https://img.aspxhome.com/file/2023/9/101999_0s.png)
C#生成PDF文件流
2023-03-19 08:52:56
Android通过AIDL在两个APP之间Service通信
2022-04-15 23:58:27
![](https://img.aspxhome.com/file/2023/6/137246_0s.png)
Android 嵌套Fragment的使用实例代码
2022-07-18 06:37:15
![](https://img.aspxhome.com/file/2023/6/111686_0s.png)
listview控件实现点击列表头进行listview排序示例分享
2023-06-06 18:00:39
Android使用Intent传递组件大数据
2023-09-30 12:21:46
Android基于高德地图完全自定义Marker的实现方法
2023-03-09 03:32:30
![](https://img.aspxhome.com/file/2023/8/138618_0s.jpg)
深入理解C#中常见的委托
2022-03-23 01:05:46
![](https://img.aspxhome.com/file/2023/3/80073_0s.png)
Mybatis-Plus 新增获取自增列id方式
2021-10-22 17:06:38
![](https://img.aspxhome.com/file/2023/3/113383_0s.png)
Android中把bitmap存成BMP格式图片的方法
2022-10-02 10:09:16
带你了解Java Maven的打包操作
2022-08-03 15:23:33
![](https://img.aspxhome.com/file/2023/7/68537_0s.jpg)
WinForm项目中添加帮助文档功能
2022-12-30 02:22:50
![](https://img.aspxhome.com/file/2023/1/119541_0s.png)
Android开发之文件操作详解
2023-02-06 01:23:30
![](https://img.aspxhome.com/file/2023/2/105992_0s.png)
C++ 实现球迷 今日头条面试题
2022-07-08 11:03:24