spring-cloud配置ribbon负载均衡

ribbon提供的负载均衡就是开箱即用的,简单的不能再简单了

  • 为了顺利演示此demo,你需要如下

需要提前配置eureka服务端,具体看 https://www.cnblogs.com/ye-hcj/p/10292944.html

需要提前配置eureka客户端,具体看 https://www.cnblogs.com/ye-hcj/p/10293048.html

目录结构

依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

Application

package test1.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient; import test1.test.Config.TestConfig; @RibbonClient(name = "stores", configuration = TestConfig.class)
@EnableEurekaClient
@SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }

配置

spring:
application:
name: eureka-ribbon-clustered server:
port: 7000 # -- Configure for Ribbon: stores:
ribbon:
eureka:
enabled: false # Disable Default Ping
listOfServers: localhost:8000,localhost:8001,localhost:8002,,localhost:8003
ServerListRefreshInterval: 15000 # -- Configure Discovery Client (Eureka Client).
eureka:
instance:
appname: ribbion-service
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://eureka1:9001/eureka

Config

package test1.test.Config;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.WeightedResponseTimeRule; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; public class TestConfig {
@Autowired
private IClientConfig ribbonClientConfig; @Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
} @Bean
public IRule ribbonRule(IClientConfig config) {
return new WeightedResponseTimeRule();
}
}

Controller

package test1.test.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class TestController {
@Autowired
private DiscoveryClient discoveryClient; @Autowired
private LoadBalancerClient loadBalancer; @ResponseBody
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() { return "<a href='testCallAbcService'>/testCallAbcService</a>";
} @ResponseBody
@RequestMapping(value = "/testCallAbcService", method = RequestMethod.GET)
public String showFirstService() { String serviceId = "EUREKA-CLIENT-CLUSTERED".toLowerCase(); // (Need!!) eureka.client.fetchRegistry=true
List<ServiceInstance> instances = this.discoveryClient.getInstances(serviceId); if (instances == null || instances.isEmpty()) {
return "No instances for service: " + serviceId;
}
String html = "<h2>Instances for Service Id: " + serviceId + "</h2>"; for (ServiceInstance serviceInstance : instances) {
html += "<h3>Instance :" + serviceInstance.getUri() + "</h3>";
} // Create a RestTemplate.
RestTemplate restTemplate = new RestTemplate(); html += "<br><h4>Call /hello of service: " + serviceId + "</h4>"; try {
// May be throw IllegalStateException (No instances available)
ServiceInstance serviceInstance = this.loadBalancer.choose(serviceId); html += "<br>===> Load Balancer choose: " + serviceInstance.getUri(); String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/loadBalancer"; html += "<br>Make a Call: " + url;
html += "<br>"; String result = restTemplate.getForObject(url, String.class); html += "<br>Result: " + result;
} catch (IllegalStateException e) {
html += "<br>loadBalancer.choose ERROR: " + e.getMessage();
e.printStackTrace();
} catch (Exception e) {
html += "<br>Other ERROR: " + e.getMessage();
e.printStackTrace();
}
return html;
}
}

运行

先启动eureka服务端和客户端,再用spring-boot运行此程序

然后访问,即可看到其他同时注册的程序
http://localhost:7000 ribbon的负载均衡是它自己完成的,开箱即用

spring-cloud配置ribbon负载均衡的更多相关文章

  1. spring cloud: 关闭ribbon负载均衡

    spring cloud: 关闭ribbon负载均衡 1.eureka服务 2.2个user服务:7900/7901 3,movie服务 movie服务去请求 user的用户信息,而此时只想请求790 ...

  2. API网关spring cloud gateway和负载均衡框架ribbon实战

    通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...

  3. spring cloud 之 客户端负载均衡 Ribbon

    一.负载均衡 负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意 ...

  4. 三、Spring Cloud之软负载均衡 Ribbon

    前言 上一节我们已经学习了Eureka 注册中心,其实我们也使用到了Ribbon ,只是当时我们没有细讲,所以我们现在一起来学习一下Ribbon. 什么是Ribbon 之前接触到的负载均衡都是硬负载均 ...

  5. SpringCloud学习笔记(8)----Spring Cloud Netflix之负载均衡-Ribbon的负载均衡的策略

    一. 内置 负载均衡策略的介绍的 IRule的实现类 2. 通过代码实现负载均衡 在第六节Riddom的使用的工程中,随机策略配置类 package com.wangx.cloud.springclo ...

  6. SpringCloud学习笔记(7)----Spring Cloud Netflix之负载均衡-Ribbon的深入理解

    1. 注解@LoadBalanced 作用:识别应用名称,并进行负载均衡. 2. 入口类:LoadBalancerAutoConfiguration 说明:类头上的注解可以知道Ribbon 实现的负载 ...

  7. 【Spring Cloud】客户端负载均衡组件——Ribbon(三)

    一.负载均衡 负载均衡技术是提高系统可用性.缓解网络压力和处理能力扩容的重要手段之一. 负载均衡可以分为服务器负载均衡和客户端负载均衡,服务器负载均衡由服务器实现,客户端只需正常访问:客户端负载均衡技 ...

  8. SpringCloud学习笔记(6)----Spring Cloud Netflix之负载均衡-Ribbon的使用

    1. 什么是负责均衡? 负载均衡,就是分发请求流量到不同的服务器. 负载均衡一般分为两种 1. 服务器端负载均衡(nginx) 2. 客户端负载均衡(Ribbon) 2. 服务提供者(spring-c ...

  9. Spring Cloud 2-Ribbon 客户端负载均衡(二)

    Spring Cloud Eureka  1.Hello-Service服务端配置 pom.xml application.yml 启动两个service 2.Ribbon客户端配置 pom.xml ...

随机推荐

  1. Gradle 一(Android)

    参考一:Gradle 完整指南(Android) 参考二:深入理解Android(一):Gradle详解 参考三:Gradle for Android 第一篇( 从 Gradle 和 AS 开始 ) ...

  2. shell编程学习1

    1.shell是操作系统的终端命令行 (1)shell可以理解为软件系统提供给用户操作的命令行界面,可以说它是人机交互的一种方式.    (2)我们可以使用shell和操作系统.uboot等软件系统进 ...

  3. Django rest_framewok框架的基本组件

    快速实例 序列化 视图三部曲 认证与权限组件 解析器 分页 回到顶部 快速实例 Quickstart 回到顶部 序列化 创建一个序列化类 简单使用 开发我们的Web API的第一件事是为我们的Web ...

  4. mybatis分页插件使用注意

    之前的项目用的数据库是mysql,在pom.xml引入这一个就能分页了. 后来又开了一个项目,使用的是oracle数据库,我写分页的时候发现不能实现,在网上找到资料说是必须要5.0以上的.我就导了这依 ...

  5. ElasticSearch6.0 Java API 使用 排序,分组 ,创建索引,添加索引数据,打分等(一)

    ElasticSearch6.0  Java API  使用     排序,分组 ,创建索引,添加索引数据,打分等 如果此文章对你有帮助,请关注一下哦 1.1 搭建maven 工程  创建web工程 ...

  6. WebForms UnobtrusiveValidationMode 需要“jQuery”ScriptResourceMapping。

    .net framework4.5开发中, Unobtrusive ValidationMode是一种隐式的验证方式,需要前端调用jquery来进行身份验证.且默认启用. 解决方法如下 方法一: 修改 ...

  7. C++友元类实现

    C++中的友元既可以实现友元函数,也可以实现友元类,也就是说一个类也可以作为另外一个类的友元.当作为一个类的友元时,它的所有成员函数都是另一个类的友元函数,都可以访问另一个类的私有或者公有成员. 请看 ...

  8. Unity3D开发之Matrix4x4矩阵变换

    在Unity开发中时常会用到Matrix4x4矩阵来变换场景中对象的位置.旋转和缩放.但是很多人都不太理解这儿Matrix4x4变换矩阵.通过DX中的变换矩阵我来讲一讲在unity中这个变换矩阵是怎么 ...

  9. 关于list.extend(iterable)

    extend内的参数只要是iterable就可以,那么也可以添加定制的iterable,开整. class A(object): def __init__(self): self.a = 0 def ...

  10. NOI 2018 你的名字

    因为机房里的小伙伴都在看<你的名字.>而我不想看 所以来写了这道题... 给一个 $S$ 串,$q$ 次询问,每次一个 $T$ 串,问 $T$ 有多少没在 $S[l,r]$ 中以子串形式出 ...