SpringCloud-Ribbon快速搭建+EurekaServer高可用搭建

一、搭建一个或以上的EurekaServer(本文以两个为例)

  1. 添加Eureke-Server依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  1. 添加注解@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer01Application {
	public static void main(String[] args) {
	SpringApplication.run(EurekaServer01Application.class, args);
	}
}
  1. 添加配置
#设置服务端口,eureka server端默认端口是8761
spring:
  application:
    name: eureka-server
#设置服务端口,eureka server端默认端口是8761
server:
  port: 8000
#表示是否将自己注册到Eureka Server,默认为true
eureka:
    client:
      register-with-eureka: true
#表示是否从Eureka Server获取注册信息,默认为true
      fetch-registry: true
#暴露给其他eureka client端注册的地址
      service-url.defaultZone: http://localhost:8001/eureka/
 4. 依据以上步骤再搭建一个EurekaServer注意修改端口号,注意spring.application.name=eureka-server保持一致

二、搭建三个Provider节点

  1. 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderRibbon01Application {

	public static void main(String[] args) {
		SpringApplication.run(ProviderRibbon03Application.class, args);
	}

}
  1. 配置文件
spring:
  application:
    name: provider-ribbon
server:
  port: 10002

eureka:
  client:
    service-url.defaultZone: http://localhost:8000/eureka/,http://localhost:8001/eureka/
  1. 按照以上步骤继续搭建两个及以上,注意spring.application.name=provider-ribbon保持一致

三、搭建Consumer节点

  1. 添加Ribbon依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
  1. 添加配置类
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RetryRule;
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;

/**
 1. @author Eden
 2. @date 2020/5/1 10:17
 */
@Configuration
public class RibbonConfig {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    /**
     * Ribbon默认负载均衡策略是轮询,现在将其改为随机
     * @return
     */
    @Bean
    public IRule demoRule(){
        return new RetryRule();
    }
}

demo代码
主要是以下几个模块
在这里插入图片描述

三、拓展

  1. Ribbon原理示意图
    在这里插入图片描述
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值