当前位置: 首页 > news >正文

SpringCloud高可用集群搭建及负载均衡配置实战

代码github地址:https://github.com/tzb1017432592/springcloud-lean
这里是我整个项目,其中包含了三个子模块

我会用eureka-server子模块启动三个注册中心实现注册中心高可用,使用eureka-client子模块启动两个服务提供客户端实现服务提供客户端的高可用,然后在启动一个服务消费者端进行服务消费

然后启动三个gateway 网关服务实现,搭建网关的高可用,最后使用nginx实现网关的负载均衡

整个设计是这样的:

注册中心高可用

现在我们要给注册中心eureka-server配置搭建高可用,实现的原理就是起动两个相同服务名称的注册中心在不同的节点上相互注册(由于我这里条件有限,我只能配置相同ip下不同的端口号起动两个不同的注册中心)

注册中心的配置

我这里是配置在了同一个节点上不同的端口启动三个注册中心

server:port: 8761eureka:instance:#eureka 实例的hostname,可以是hostname,也可以自定义配置hostnamehostname: eureka-cluster01#instance-id:是该实例注册到服务中心的唯一ID,也就是在浏览器中看到的服务名#默认是${eureka.instance.ip-address}:${spring.application.name}:${server.port}#instance-id: tzb1#注册时客户端是否使用自己的IP而不是主机名,默认是falseprefer-ip-address: falseclient:#表示是否将自己注册到Eureka Server,在server端这里应该设置为false,但是为了方便看到效果,这里设置我truefetch-registry: false#表示是否从Eureka Server获取注册的服务信息,在server端这里应该设置为false,但是为了方便看到效果,这里设置我trueregister-with-eureka: trueservice-url:defaultZone: http://eureka-cluster02:8762/eureka/,http://eureka-cluster03:8763/eureka/spring:application:name: eureka-clusterprofiles: pre01---
server:port: 8762eureka:instance:#eureka 实例的hostname,可以是hostname,也可以自定义配置hostnamehostname: eureka-cluster02#instance-id:是该实例注册到服务中心的唯一ID,也就是在浏览器中看到的服务名#默认是${eureka.instance.ip-address}:${spring.application.name}:${server.port}#instance-id: tzb2#注册时客户端是否使用自己的IP而不是主机名,默认是falseprefer-ip-address: falseclient:fetch-registry: falseregister-with-eureka: trueservice-url:defaultZone: http://eureka-cluster01:8761/eureka/,http://eureka-cluster03:8763/eureka/spring:application:name: eureka-clusterprofiles: pre02---
server:port: 8763eureka:instance:#eureka 实例的hostname,可以是hostname,也可以自定义配置hostnamehostname: eureka-cluster03#instance-id:是该实例注册到服务中心的唯一ID,也就是在浏览器中看到的服务名#默认是${eureka.instance.ip-address}:${spring.application.name}:${server.port}#instance-id: tzb3#注册时客户端是否使用自己的IP而不是主机名,默认是falseprefer-ip-address: falseclient:fetch-registry: falseregister-with-eureka: trueservice-url:defaultZone: http://eureka-cluster01:8761/eureka/,http://eureka-cluster02:8762/eureka/spring:application:name: eureka-clusterprofiles: pre03

hostname可以在C:\Windows\System32\drivers\etc\host中配置

利用profiles启动服务应用不同的配置,依旧不同的profiles使用不同的配置,利用idea配置好启动的主类的profiles:

依据profiles配置,启动三个注册中心,这样注册中心就实现了高可用

提供服务客户端高可用

实现的原理就是在不同的节点上分别启动服务名一样的提供服务客户端,将其注册到集群的注册中心中(由于我这里条件有限,我只能配置相同ip下不同的端口号起动不同的服务提供客户端)
(这里注意了,客户端即使只注册了其中一个注册中心,注册中心集群中所有的注册中心成员都会有一样的注册信息,因为集群中注册中心会进行注册信息的相互拷贝)

提供服务客户端的配置

我这里是配置在了同一个节点上不同的端口起动两个提供服务客户端

server:port: 8751eureka:client:service-url:defaultZone: http://eureka-cluster01:8761/eureka/,http://eureka-cluster02:8762/eureka/,http://eureka-cluster03:8763/eureka/
spring:application:name: eureka-client01profiles: cl01
---
server:port: 8752eureka:client:service-url:defaultZone: http://eureka-cluster01:8761/eureka/,http://eureka-cluster02:8762/eureka/,http://eureka-cluster03:8763/eureka/
spring:application:name: eureka-client01profiles: cl02

我在eureka-client子模块定义了一些接口提供后续服务消费者验证使用

@RestController
public class Controller {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;@Autowiredprivate ServiceInfoUtil serviceInfoUtil;@GetMapping("/client01/hello")public String test(){List<String> services = discoveryClient.getServices();return "服务消费:"+services+",端口号:"+serviceInfoUtil.getPort();}@GetMapping("/consummer/hello")public String consummer(){return restTemplate.getForEntity("http://EUREKA-CLIENT01/client01/hello",String.class).getBody();}
}

提供一个获取被调用服务的端口工具类

@Configuration
public class ServiceInfoUtil implements ApplicationListener<WebServerInitializedEvent> {private int serverPort;@Overridepublic void onApplicationEvent(WebServerInitializedEvent event) {this.serverPort = event.getWebServer().getPort();}public int getPort() {return this.serverPort;}
}

依据profiles配置,启动两个客户端,这样服务提供客户端就实现了高可用:

服务消费

配置了注册中心与服务提供客户端的高可用,我们知道springcloud会通过ribbon进行负载均衡现在
我们在子模块eureka-client基础上启动一个服务消费者,起名叫eureka-consummer

server:port: 8753eureka:client:service-url:defaultZone: http://eureka-cluster01:8761/eureka/,http://eureka-cluster02:8762/eureka/,http://eureka-cluster03:8763/eureka/
spring:application:name: eureka-consummerprofiles: cl03

配置ribbon与RestTemplate 整合

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(EurekaClientApplication.class, args);}
}

根据profiles启动应用:

现在三个eureka-server上的注册服务如图:eureka-cluster01:

eureka-cluster02:
eureka-cluster03:

现在我们调用8753端口eureka-consummer应用的/consummer/hello接口,我这里访问http://localhost:8753/consummer/hello即可

第一次访问,访问到了8751端口的eureka-client01应用:

第二次访问,访问到了8752端口的eureka-client01应用:

服务网关高可用

在gateway子模块中引入依赖

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

yml配置

server:port: 8851eureka:client:service-url:defaultZone: http://eureka-cluster01:8761/eureka/,http://eureka-cluster02:8762/eureka/,http://eureka-cluster03:8763/eureka/
spring:application:name: eureka-gatewaycloud:gateway:discovery:locator:enabled: trueroutes:- id: eureka-client01uri: lb://eureka-client01predicates:- Path=/client01/**- id: eureka-consummeruri: lb://eureka-consummerpredicates:- Path=/consummer/**profiles: gw01---
server:port: 8852eureka:client:service-url:defaultZone: http://eureka-cluster01:8761/eureka/,http://eureka-cluster02:8762/eureka/,http://eureka-cluster03:8763/eureka/
spring:application:name: eureka-gatewaycloud:gateway:discovery:locator:enabled: trueroutes:- id: eureka-client01uri: lb://eureka-client01predicates:- Path=/client01/**- id: eureka-consummeruri: lb://eureka-consummerpredicates:- Path=/consummer/**profiles: gw02
---
server:port: 8853eureka:client:service-url:defaultZone: http://eureka-cluster01:8761/eureka/,http://eureka-cluster02:8762/eureka/,http://eureka-cluster03:8763/eureka/
spring:application:name: eureka-gatewaycloud:gateway:discovery:locator:enabled: trueroutes:- id: eureka-client01uri: lb://eureka-client01predicates:- Path=/client01/**- id: eureka-consummeruri: lb://eureka-consummerpredicates:- Path=/consummer/**profiles: gw03

启动类

@SpringBootApplication
@EnableEurekaClient
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}

配置idea的启动配置

分别启动服务,看配置中心的信息,我这里已经启动成功

验证:
分别问:http://localhost:8853/consummer/hello,http://localhost:8852/consummer/hello,http://localhost:8853/consummer/hello

nginx配置服务网关负载均衡

下载nginx配置nginx.conf

http {include       mime.types;default_type  application/octet-stream;sendfile        on;upstream gatewayserver{server 127.0.0.1:8851 weight=1;server 127.0.0.1:8852 weight=1;server 127.0.0.1:8853 weight=1;}server {listen       80;server_name  gateway.cluster;location / {proxy_pass  http://gatewayserver/;}}
}

然后配置hostname,在C:\Windows\System32\drivers\etc\host中配置

启动nginx

访问http://gateway.cluster/consummer/hello

这样我们的springcloud的高可用就算完成了

最后编辑于:2025-06-28 11:18:52


喜欢的朋友记得点赞、收藏、关注哦!!!

http://www.dtcms.com/a/403140.html

相关文章:

  • AI产品独立开发完全指南:技术栈选择、案例分析与商业化路径
  • Jenkins+Tomcat持续集成教程
  • 哪里有免费建设网站承德在线
  • 【金融保研复习】知识点与可能的题目
  • 基于ZYNQ的ARM+FPGA+yolo AI火灾实时监测与识别系统
  • 【Python语法基础学习笔记】常用函数
  • Uniapp运行时错误修复报告
  • PHP 8.0+ 高级特性深度探索:架构设计与性能优化
  • 网站管理建设总结大数据营销的概念
  • 顺德品牌网站建设辽宁建设工程信息网上
  • Oracle Clint11g安装
  • Gerkin+unittest(python)实现自动化
  • MySQL基础语法大全
  • 从企业实战中学习Appium自动化(二)
  • Unity 使用ADB工具打包Apk 安装到Android手机或平板
  • 一、移动零,复写零,快乐数
  • React资源合集
  • sem是什么职业邢台做网站建设优化制作公司
  • 福建省建设执业资格注册中心网站企业建设网站注意点
  • 配置Modbus TCP转RS485模块读取温度数据
  • OSPF LSA/ 路由种类
  • 算法面试(5)------NMS(非极大值抑制)原理 Soft-NMS、DIoU-NMS 是什么?
  • 乐清网站制作推荐建湖人才网最新招聘信息
  • AWS下载sentinel-2原始影像
  • docker-容器网络类型
  • MySQL 中使用索引
  • 双功能分子:NOTA Octreotide Acetate,NOTA-奥曲肽具有放射性金属螯合能力
  • 帆软FCP开发认证模拟第二题
  • 做网站打印费复印费清单中方建设局网站
  • PyTorch DataLoader 接受的返回值类型