Spring Cloud负载均衡(二)

Netflix Ribbon 整合Eureka

  • 激活服务发现的客户端
    @EnableDiscoveryClient
package com.example.springcloudlesson6;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RibbonClients({
        @RibbonClient(name = "spring-cloud-service-provider")
})
@EnableDiscoveryClient
public class SpringCloudLesson6Application {

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

    //声明RestTemplate
    @LoadBalanced//RestTemplate的行为变化
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

创建并且启动Eureka Server

spring-cloud-lesson-6-eureka-server为例
1.激活@EnableEurekaServer

package com.example.springcloudlesson6eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudLesson6EurekaServerApplication {

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

}

2.配置Eureka服务器,在application.properties


##定义应用名称
spring.application.name=spring-cloud-eureka-server
##配置端口
server.port=10000
##取消向注册中心注册
eureka.client.register-with-eureka=false
##取消向注册中心获取注册信息,实例信息
eureka.client.fetch-registry=false
##解决Peer/集群连接问题
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

3.启动Eureka Server

调整Ribbon客户端

1.调整application.properties配置文件



##服务提供方
spring.application.name=spring-cloud-ribbon-client

##服务端口
server.port=8080
##关闭注册
#eureka.client.enabled=false

##连接 Eureka Server url
eureka.client.service-url.defaultZone=http://localhost:10000/eureka/

##服务提供方主机
service-provider.host=localhost
##提供方端口
service-provider.port=9090

service-provider.name=spring-cloud-service-provider

###配置服务提供方的ribbon
spring-cloud-service-provider.ribbon.listOfServers=\
  http://${service-provider.host}:${service-provider.port}

服务提供方调整,并且连接 Eureka Server

1.修改application.properties


##服务提供方
spring.application.name=spring-cloud-service-provider

##服务端口
server.port=9090
##关闭注册
#eureka.client.enabled=false
##连接Eureka Server
eureka.client.service-url.defaultZone=http://localhost:10000/eureka/

在启动两台 服务提供方实例
--server=9091
--server=9092
d1

负载均衡示意图

d2

程序界面示意图

d3

通过刷新重连后的结论为,三个服务器的权重是一致的,是以轮询的方式进行连接

Netflix Ribbon核心接口

  • 实际请求客户端
    LoadBalancerClient

RibbonLoadBalancerClient

  • 负载均衡上下文
    LoadBalancerContext

RibbonLoadBalancerContext

负载均衡器
ILoadBalancer
  • BaseLoadBalancer
  • DynamicServerListLoadBalancer
  • ZoneAwareLoadBalancer
  • NoOploadBalancer
规则接口
IRule
  • 随机规则:RandomRule
  • 最可用原则:BestAvailableRule
  • 轮训规则:RoundRobinRule (默认情况)
  • 重试实现:RetryRule
IRule
  • 客户端配置:ClientConfigEnabledRoundRobinRule
  • 可用性过滤规则:AvailabilityFilteringRule
  • RT权重规则:WeightedResponseTimeRule
  • 规避区域规则: ZoneAvoidanceRule

PING策略

IPingStrategy
  • NoOpPing (默认情况)
  • DummyPing
  • PingConstant
  • pingUrl
  • NIWSDiscoveryPing
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值