Springboot使用zuul进行负载均衡
完整项目代码地址参考:https://github.com/SimonHu1993/SpringbootZuul
1.这里我们使用Eureka来作为服务的注册与发现中心,首先看看Eureka client客户端配置文件
server:port: 10013contextPath: /cardmembersession:timeout: 1800 logging:config: classpath:log4j2-dev-spring.yml#spring配置 spring:application:name: cardmemberdatasource:driver-class-name: oracle.jdbc.driver.OracleDriverurl: jdbc:oracle:thin:@xxx:1521/xshdbusername: xxxxpassword: xxxtype: com.zaxxer.hikari.HikariDataSourcehikari:maxLifetime: 1765000 #一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上maximumPoolSize: 20 #连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)minimumIdle: 1 #连接池中允许的最小空闲连接数cache:type: guavacache-names: merchantDetail,selConfigguava:spec: maximumSize=500,expireAfterWrite=5m#模版引擎thymeleaf:cache: falseprefix: classpath:/templates/suffix: .htmlencoding: UTF-8#mybatis mybatis:mapperLocations: classpath*:com/zhx/*/*/mapper/*Mapper.xmltypeAliasesPackage: com.zhx.webconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpleureka:client:# 开启健康检查(需要spring-boot-starter-actuator依赖)healthcheck:enabled: trueserviceUrl:#defaultZone: http://127.0.0.1:9700/eureka/,http://127.0.0.1:9600/eureka/,http://127.0.0.1:9500/eureka/ 可以配置多个eureka节点defaultZone: http://admin:123456@127.0.0.1:9800/eureka/instance:#启用Ip注册preferIpAddress: trueinstance-id: ${spring.cloud.client.ipAddress}:${server.port}:${random.value}# 续约更新时间间隔(默认30秒)lease-renewal-interval-in-seconds: 180# 续约到期时间(默认90秒)lease-expiration-duration-in-seconds: 200management:#关闭安全检测security:enabled: falseport: ${server.port}#监控监控信息设置为非敏感 endpoints:health:sensitive: falsesecurity:basic:enabled: false#配置属性 site:#测试errorNumber: 1
启动类中添加@EnableEurekaClient注解
@SpringBootApplication @EnableCaching // 启用缓存 @EnableEurekaClient public class Application extends WebMvcConfigurerAdapter {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Autowiredprivate RequestInterceptor requestInterceptor;/*** 配置拦截器* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(requestInterceptor).addPathPatterns("/**");} }
2.配置eureka服务端
server:port: 9800 spring:application:name: eureka_server eureka:server:# 设为false,关闭自我保护enable-self-preservation: false# 清理间隔(单位毫秒,默认是60*1000)eviction-interval-timer-in-ms: 15000client:registerWithEureka: truefetchRegistry: trueservice-url:defaultZone: http://${security.user.name}:${security.user.password}@xxxx:8081/eureka/,http://${security.user.name}:${security.user.password}@xxx:9600/eureka/ # defaultZone: http://localhost:8762/eureka/ 部署eureka服务的节点ipinstance:#启用Ip注册preferIpAddress: trueinstance-id: ${spring.cloud.client.ipAddress}:${server.port}:${random.value}# 续约更新时间间隔(默认30秒)lease-renewal-interval-in-seconds: 10# 续约到期时间(默认90秒)lease-expiration-duration-in-seconds: 20management:#关闭安全检测security:enabled: falseport: ${server.port}endpoints:health:sensitive: falsesecurity:basic:enabled: trueuser: #eureka登录账号密码name: adminpassword: 123456
pom文件添加eureka-server依赖
<!--增加eureka-server的依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>
eureka-server启动类
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);} }
3.zuul做负载均衡
spring:application:name: zuul_server server:port: 8080zuul:routes:#服务端负载均衡配置coreapiread:path: /coreapiread/**stripPrefix: false#服务的 application.name 不能带下划线serviceId: coreapireadcoreapiwrite:path: /coreapiwrite/**stripPrefix: falseserviceId: coreapiwritedidiAdmin:path: /didi-admin/**stripPrefix: falseurl: http://127.0.0.1:6028cardmember:path: /cardmember/**stripPrefix: falseserviceId: cardmembersensitive-headers:add-host-header: trueadd-proxy-headers: true#超时设置 hystrix:command:default:execution:timeout:enabled: trueisolation:thread:timeoutInMilliseconds: 20000 ribbon:ReadTimeout: 20000ConnectTimeout: 20000MaxAutoRetries: 0MaxAutoRetriesNextServer: 1eureka:client:# 开启健康检查(需要spring-boot-starter-actuator依赖)healthcheck:enabled: trueserviceUrl:defaultZone: http://admin:123456@xxx:8081/eureka/,http://admin:123456@xxx:9600/eureka/# eureka用户名和密码#设置拉取服务注册信息时间,默认60s 如果要迅速获取服务注册状态,可以缩小该值registry-fetch-interval-seconds: 10instance:#启用Ip注册preferIpAddress: trueinstance-id: ${spring.cloud.client.ipAddress}:${server.port}:${random.value}# 续约更新时间间隔(默认30秒)lease-renewal-interval-in-seconds: 30# 续约到期时间(默认90秒)lease-expiration-duration-in-seconds: 90management:#关闭安全检测security:enabled: falseport: ${server.port}security:basic:enabled: trueuser:name: zhx22password: fdg2222#监控监控信息设置为非敏感 endpoints:health:sensitive: false
zuul启动类
@EnableEurekaClient @EnableZuulProxy @SpringBootApplication public class ZuulApplication {public static void main(String[] args) {SpringApplication.run(ZuulApplication.class, args);} }
pom文件
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency>
eureka控制台信息
进行节点移除更新时,在eureka服务所在服务器上先移除待更新节点,在kill节点服务:
移除节点 curl -u admin:123456 -X DELETE http://127.0.0.1:8081/eureka/apps/ZUUL_SERVER/127.22.2.124:10011:cf6d9fd513f57727e57c8bc213767fc3
参数解释,curl -u eureka用户名:密码,第一个ip为eureka服务所在ip,第二个为client所在ip,最后的md5信息为eureka控制台上的节点信息;
转载于:https://www.cnblogs.com/SimonHu1993/p/11584410.html
Springboot使用zuul进行负载均衡相关推荐
- 一.Docker之springboot+docker swam实现负载均衡
首先准备三台机器,这里选择216.248.249 选其中一台机器作为主节点,作为Manager,这里选216 1.在216上初始化swam集群,在终端执行 docker swarm init --ad ...
- SpringBoot+Euraka+Zuul实现服务路由的服务
这个标题有点儿绕,咋 还是服务路由的服务了? 首先,Zuul最后也会做成一个Eureka Client 服务,注册到 Eureka Server 上,所以,后一个服务说的是Zuul 项目本身也是一个 ...
- Zuul指定Path+url以及指定可用的服务节点时如何负载均衡
你这个东西可以是serviceId,也可以是url,那serviceId刚刚我们已经研究过了,就是注册中心的serviceId,我们试试url,url: https://example.com/use ...
- springboot gateway 负载均衡_05_Springbootamp;Springcloud面试题
点击上方"蓝色字体",选择"设为星标", 可以防止走失! 1 简单说一下springboot? Spring Boot是由Pivotal团队提供的全新框架,其设 ...
- 跟着狂神学SpringCloud(Rest环境搭建+Eureka服务注册与发现+ribbon+Feign负载均衡+Hystrix+服务熔断+Zuul路由网关+SpringCloud config分布)
跟着狂神学SpringCloud SpringCloud 回顾之前的知识- JavaSE 数据库 前端 Servlet Http Mybatis Spring SpringMVC SpringBoot ...
- java B2B2C Springboot仿淘宝电子商城系统-负载均衡之ribbon+feign
一. feign简介 Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign注解和JAX-RS注 ...
- So easy!Nginx+SpringBoot 实现负载均衡
在介绍Nginx的负载均衡实现之前,先简单的说下负载均衡的分类,主要分为硬件负载均衡和软件负载均衡,硬件负载均衡是使用专门的软件和硬件相结合的设备,设备商会提供完整成熟的解决方案,比如F5,在数据的稳 ...
- Nginx实现负载均衡(整合SpringBoot小demo)
目录 1.前言 2.什么是负载均衡 3.准备工作 3.1 关于Mac系统安装Nginx 3.2 测试项目说明 4.Nginx负载均衡的集中方式介绍 4.1 轮询 4.2 权重 4.3 iphash 4 ...
- Zuul和Eureka的负载均衡示例
Zuul和Eureka的负载均衡示例 1.概述 在本文中,我们将介绍如何通过Zuul和Eureka一起使用来实现负载均衡. 我们将请求路由到注册在Spring Cloud Eureka,并通过Zuul ...
最新文章
- Linux学习笔记(十二)usermod、passwd、mkpasswd
- 实现多条件模糊查询SQL语句
- D. Steps to One(概率DP,莫比乌斯反演)
- inno setup打包的安装包如何在卸载完程序后可以继续安装_这两个方法就够了!快速制作Python程序Windows安装包...
- eclispe dev tools
- linux monit安装配置
- 深度学习知识抽取:属性词、品牌词、物品词
- windows c++ 流读取文件长度 ios::in ios::binary
- 常见并发工具的使用和原理解析——Condition(重点在第五节)
- HTML页面跳转的5种方法分析介绍
- 图灵机和冯洛伊曼体系结构
- 怎样快速开发属于自己的微信小程序?
- Docker + Gitlab + Gitlab CI(三)
- 如何用保险抵御人生中的死亡风险【全攻略】
- pygame.mask原理及使用pygame.mask实现精准碰撞检测
- 【数据结构 严蔚敏版】 二叉树 基本操作
- 金蝶云星空插件开发调试教程示例
- 基于禁忌搜索算法的三维装箱问题
- 第七届全球云计算大会-中国站9月宁波举办
- Python 习题练习