博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud负载均衡笔记
阅读量:6808 次
发布时间:2019-06-26

本文共 9973 字,大约阅读时间需要 33 分钟。

SpringCloud的负载均衡组件用的是Ribbon,这个东西就是服务消费者。它只是一个配置用的中转器,放在 Zuul 和 Eureka Client 之间用来转发的,它里面写了好多规则,用来指定负载的规则。

首先,Zuul:

application.yml

eureka:  client:    serviceUrl:      defaultZone: http://localhost:7087/eureka/server:  port: 7089spring:  application:    name: service-zuulzuul:  host:    maxTotalConnections: 10000    maxPerRouteConnections: 60000  routes:    api-a:      path: /dbmeta/**      serviceId: service-ribbon    api-b:      path: /kylin/**      serviceId: service-kylinribbon:    eureka:      enabled: falseservice-ribbon:  ribbon:    listOfServers: http://localhost:7088,http://localhost:7091    ConnectTimeout: 10000    ReadTimeout: 30000    MaxTotalHttpConnections: 10000    MaxConnectionsPerHost: 60000

Application

package com.shinho;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy@SpringBootApplicationpublic class DbzuulApplication {    public static void main(String[] args) {        SpringApplication.run(DbzuulApplication.class,args);    }}

pom.xml

4.0.0
com.shinho
dbzuul
0.0.1
jar
dbzuul
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
Finchley.M9
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.boot
spring-boot-starter-test
test
org.springframework
spring-expression
4.3.14.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

然后,Ribbon客户端

application.yml

eureka:  client:    serviceUrl:      defaultZone: http://localhost:7087/eurekaserver:  port: 7090  spring:  application:    name: service-ribbon

Config类

package com.config;import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.netflix.client.config.IClientConfig;import com.netflix.loadbalancer.BestAvailableRule;import com.netflix.loadbalancer.IPing;import com.netflix.loadbalancer.IRule;import com.netflix.loadbalancer.PingUrl;import com.netflix.loadbalancer.Server;import com.netflix.loadbalancer.ServerList;import com.netflix.loadbalancer.ServerListSubsetFilter;import com.shinho.DbribbonApplication;@Configurationpublic class MyConfig {    @Bean    public IRule ribbonRule() {        return new BestAvailableRule();    }    @Bean    public IPing ribbonPing() {        return new PingUrl();    }    @Bean    public ServerList
ribbonServerList(IClientConfig config) { return new DbribbonApplication.BazServiceList(config); } @Bean public ServerListSubsetFilter serverListFilter() { ServerListSubsetFilter filter = new ServerListSubsetFilter(); return filter; }}

Application

package com.shinho;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.ribbon.RibbonClient;import com.config.MyConfig;import com.netflix.client.config.IClientConfig;import com.netflix.loadbalancer.ConfigurationBasedServerList;@SpringBootApplication@RibbonClient(name = "service-ribbon", configuration = MyConfig.class)public class DbribbonApplication {    public static void main(String[] args) {        SpringApplication.run(DbribbonApplication.class, args);    }        public static class BazServiceList extends ConfigurationBasedServerList {        public BazServiceList(IClientConfig config) {            super.initWithNiwsConfig(config);        }    }}

pom.xml

4.0.0
com.shinho
dbribbon
0.0.1
jar
dbribbon
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
Finchley.M9
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework
spring-expression
4.3.14.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

客户端就和其它的一样了,注意客户端的Service-id不要和Ribbon的重复,Ribbon的Service-id是要单独的哦。

转载地址:http://jstwl.baihongyu.com/

你可能感兴趣的文章
Postgresql创建数据库及用户然后执行初始化脚本
查看>>
随手写了个android应用
查看>>
CMake 手册详解(七)
查看>>
Apache 配置里面使用 Win32DisableAcceptEx ,Apache 启动不了
查看>>
新装好SQL2005时SA无法登陆的解决办法
查看>>
只返回一个实例的类
查看>>
企业如何培养新型员工队伍
查看>>
一道笔试题
查看>>
自定义一个序列化表单的方法2+提示语
查看>>
C#正则表达式获取html标签之间的内容
查看>>
Spring4新特性——泛型限定式依赖注入
查看>>
Tomcat(一):基础配置详解
查看>>
网页后门危害大 网站安全狗帮助查杀
查看>>
Docker存储驱动之总览
查看>>
java获取当前系统时间
查看>>
Hibernate上路_18-Hibernate查询方式
查看>>
Linux vi 命令大全
查看>>
使用border制作的css三角形
查看>>
【转帖】Java并发编程:volatile关键字解析
查看>>
Tomcat 下面使用软连接指向真实的上传文件夹
查看>>