千家信息网

Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇文章为大家展示了Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。创建父项目步骤一:父
千家信息网最后更新 2026年11月01日Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

本篇文章为大家展示了Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创建父项目

  • 步骤一:父项目 could_parent

  • 步骤二:修改pom.xml文件,配置 spring boot 版本,spring cloud版本,锁定cloud依赖,确定cloud私有仓库

            org.springframework.boot        spring-boot-starter-parent        2.1.4.RELEASE                    UTF-8        1.8        Greenwich.RELEASE                                                org.springframework.cloud                spring-cloud-dependencies                ${spring-cloud-release.version}                pom                import                                                    spring-milestones            Spring Milestones            https://repo.spring.io/milestone                            false                        

创建注册中心

  • 步骤一:创建项目 eureka_demo

  • 步骤二:修改pom.xml,配置 web 和 eureka server 依赖

                            org.springframework.boot            spring-boot-starter-web                                    org.springframework.cloud            spring-cloud-starter-netflix-eureka-server            
  • 步骤三:创建yml文件,配置端口号、服务名、注册地址

#端口号server:  port: 10086#服务名spring:  application:          #服务名不可以使用下划线 列如:eureka_demo  在后面的调用中下划线会变成空格从而导致找不到该服务    name: eurekaDemo3#注册地址eureka:  client:    service-url:      defaultZone: http://localhost:10086/eureka    register-with-eureka: false   #是否注册自己到注册中心    fetch-registry: false         #是否从注册中心拉取服务列表
  • 步骤四:创建启动类,添加开启eureka server注解 @EnableEurekaServer

package com.czxy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServer     //开启eureka servicepublic class EurekaDemoApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaDemoApplication.class,args);    }}

配置高可用的注册中心:集群

  • 步骤一:修改注册中心(eureka_demo) 核心yml文件,application.yml,只配置服务名(共享内容)

#服务名spring:  application:    name: eurekaDemo
  • 步骤二:创建 application-10086.yml,配置10086端口和注册路径(10087)

#端口号server:  port: 10086#注册地址eureka:  client:    service-url:      defaultZone: http://localhost:10087/eureka    register-with-eureka: true   #是否注册自己到注册中心,默认值true(可省略)    fetch-registry: true         #是否从注册中心拉取服务列表,默认值true(可省略)
  • 步骤三:创建 application-10087.yml,配置10087端口和注册路径(10086)

#端口号server:  port: 10087#注册地址eureka:  client:    service-url:      defaultZone: http://localhost:10086/eureka    register-with-eureka: true   #是否注册自己到注册中心    fetch-registry: true         #是否从注册中心拉取服务列表
  • 步骤四:修改核心yml文件,激活10086配置,并启动程序, 此时控制台抛异常,10087还没有启动,等10087启动后异常自动消失。

#服务名spring:  application:    name: eurekaDemo  profiles:    active: 10086
  • 步骤五:修改核心yml文件,激活10087配置,并启动程序(idea不支持多启动 需要手动设置,不同版本不同设置)

#服务名spring:  application:    name: eurekaDemo  profiles:    active: 10087
  • 所有注册中心yml

IDEA中配置多启动

步骤一:创建 spring boot启动项

步骤二:配置10087启动 同配置10086一样(此处省略)

步骤三:启动

创建服务提供方 eureka_service4 (集群)

  • pom文件:

                             org.springframework.boot            spring-boot-starter-web                                    org.springframework.cloud            spring-cloud-starter-netflix-eureka-client                                    org.springframework.boot            spring-boot-starter-actuator            

同一个名称的服务,只要提供多个实例,注册到eureka中,就可以自动形成集群。

  • 步骤一:创建 application-8081.yml文件,并配置端口号8081

server:  port: 8081
  • 步骤二:创建 application-8082.yml文件,并配置端口号8082

server:  port: 8081
  • 步骤三:为两个yml文件,分别配置启动项 (同上10086,10087配置一样)

  • 步骤四:启动,测试

在服务提供方 eureka_service3 中添加controller方便测试数据

package com.czxy.controller;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;@RestController@RequestMapping("/test")public class TestController {    @GetMapping                              public ResponseEntity test(HttpServletRequest request){                                               //端口号        return ResponseEntity.ok("测试数据" + request.getServerPort());    }}

创建服务调用方

  • 步骤一:创建项目 eureka_client3

  • 步骤二:核心3步,pom文件,yml文件,启动类

pom文件:

                            org.springframework.boot            spring-boot-starter-web                                    org.springframework.cloud            spring-cloud-starter-netflix-eureka-client                                    org.springframework.boot            spring-boot-starter-actuator            

yml文件:

server:  port: 9090spring:  application:    name: eurekaClienteureka:  client:    service-url:      defaultZone: http://localhost:10086/eureka

启动类 :

package com.czxy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class ClientApplication {    public static void main(String[] args) {        SpringApplication.run(ClientApplication.class,args);    }}
  • 步骤三:编写配置类,配置RestTemplate实例 ,让RestTemplate支持"服务名"访问机制 需要添加注解 @LoadBalanced

package com.czxy.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class HttpConfig {    @Bean    @LoadBalanced       //让RestTemplate支持负载均衡,也就是说支持"服务名"访问    public RestTemplate restTemplate(){        return new RestTemplate();    }}
  • 步骤四:修改dao,使用RestTemplate进行远程调用时,使用"服务名"进行调用即可。

package com.czxy.dao;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component;import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;@Componentpublic class DataDao {    @Resource    private RestTemplate restTemplate;    public ResponseEntity data(){//        String url = "http://localhost:8081/test";        String url = "http://service3/test";        return restTemplate.getForEntity(url,String.class);    }}
  • 测试结果:

上述内容就是Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

步骤 配置 服务 文件 口号 集群 地址 核心 项目 支持 测试 均衡 内容 版本 不同 供方 实例 技能 数据 注解 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广州淘业互联网络科技 河南云网互联网络科技有限公司 互联网是不是新科技 互联网扶贫科技 平安科技 互联网 以房互联网科技 善班互联网科技有限公司 互联网 视角下的科技创新 北京华百购互联网科技有限公司 浙江互联网科技研学营 注册互联网科技公司经营范围大全 互联网媒体平台算高科技产业 互联网科技圈年度人物 互联网金融科技元年 广州百诚互联网科技有限公司官网 上海创道互联网络科技有限公司 云南互联网科技学费 北京军民互联网科技公司 陕西彩京云互联网科技有限公司 互联网大会出现的黑科技 上海互联网科技有限公司 互联网科技作文 互联网科技革命 大白互联网科技 科技伦理 互联网公司 深圳卡尔迅互联网科技有限公司怎么样 中国航天科技大学国际工业互联网 内蒙古九一互联网科技 互联网时代的科技公司 互联网界的黑科技
0