Springboot使用zuul进行负载均衡
完整项目代码地址参考:https://github.com/SimonHu1993/SpringbootZuul
1.这里我们使用Eureka来作为服务的注册与发现中心,首先看看Eureka client客户端配置文件
server:
port: 10013
contextPath: /cardmember
session:
timeout: 1800
logging:
config: classpath:log4j2-dev-spring.yml #spring配置
spring:
application:
name: cardmember
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@xxx:1521/xshdb
username: xxxx
password: xxx
type: com.zaxxer.hikari.HikariDataSource
hikari:
maxLifetime: 1765000 #一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上
maximumPoolSize: 20 #连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
minimumIdle: 1 #连接池中允许的最小空闲连接数
cache:
type: guava
cache-names: merchantDetail,selConfig
guava:
spec: maximumSize=500,expireAfterWrite=5m #模版引擎
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8 #mybatis
mybatis:
mapperLocations: classpath*:com/zhx/*/*/mapper/*Mapper.xml
typeAliasesPackage: com.zhx.web
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl eureka:
client:
# 开启健康检查(需要spring-boot-starter-actuator依赖)
healthcheck:
enabled: true
serviceUrl:
#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: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}:${random.value}
# 续约更新时间间隔(默认30秒)
lease-renewal-interval-in-seconds: 180
# 续约到期时间(默认90秒)
lease-expiration-duration-in-seconds: 200 management:
#关闭安全检测
security:
enabled: false
port: ${server.port} #监控监控信息设置为非敏感
endpoints:
health:
sensitive: false security:
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);
}
@Autowired
private RequestInterceptor requestInterceptor; /**
* 配置拦截器
* @param registry
*/
@Override
public 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: 15000 client:
registerWithEureka: true
fetchRegistry: true
service-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服务的节点ip instance:
#启用Ip注册
preferIpAddress: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}:${random.value}
# 续约更新时间间隔(默认30秒)
lease-renewal-interval-in-seconds: 10
# 续约到期时间(默认90秒)
lease-expiration-duration-in-seconds: 20 management:
#关闭安全检测
security:
enabled: false
port: ${server.port} endpoints:
health:
sensitive: false security:
basic:
enabled: true
user: #eureka登录账号密码
name: admin
password: 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: 8080 zuul:
routes:
#服务端负载均衡配置 coreapiread:
path: /coreapiread/**
stripPrefix: false
#服务的 application.name 不能带下划线
serviceId: coreapiread
coreapiwrite:
path: /coreapiwrite/**
stripPrefix: false
serviceId: coreapiwrite
didiAdmin:
path: /didi-admin/**
stripPrefix: false
url: http://127.0.0.1:6028
cardmember:
path: /cardmember/**
stripPrefix: false
serviceId: cardmember sensitive-headers:
add-host-header: true
add-proxy-headers: true #超时设置
hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 20000
ribbon:
ReadTimeout: 20000
ConnectTimeout: 20000
MaxAutoRetries: 0
MaxAutoRetriesNextServer: 1 eureka:
client:
# 开启健康检查(需要spring-boot-starter-actuator依赖)
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://admin:123456@xxx:8081/eureka/,http://admin:123456@xxx:9600/eureka/
# eureka用户名和密码
#设置拉取服务注册信息时间,默认60s 如果要迅速获取服务注册状态,可以缩小该值
registry-fetch-interval-seconds: 10 instance:
#启用Ip注册
preferIpAddress: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}:${random.value}
# 续约更新时间间隔(默认30秒)
lease-renewal-interval-in-seconds: 30
# 续约到期时间(默认90秒)
lease-expiration-duration-in-seconds: 90 management:
#关闭安全检测
security:
enabled: false
port: ${server.port} security:
basic:
enabled: true
user:
name: zhx22
password: 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控制台上的节点信息;
Springboot使用zuul进行负载均衡的更多相关文章
- springCloud搭建微服务集群+Zuul服务器端负载均衡
概述 最近研究了一下springCloud的微服务集群,主要用到了SpringCloud的服务发现和服务器端负载均衡,所有的项目都是用的springboot,可以和springCloud无缝对接. 技 ...
- SpringBoot(三) - Ribbon客户端负载均衡,Zuul网关,Config配置中心
1.Ribbon客户端负载均衡 1.1 依赖 1.2 配置信息 # feign默认加载了ribbon负载均衡,默认负载均衡机制是:轮询 # 负载均衡机制是添加在消费端(客户端)的,如果改为随机,指定服 ...
- spring cloud使用zuul实现反向代理和负载均衡
首先,这篇文章参考的是http://blog.didispace.com/springcloud5/这位大牛的博客.本人是通过这篇博客来学习zuul的,现在写的博客只是个人在学习时个人的一些感受和理解 ...
- Spring Cloud负载均衡:使用zuul作服务器端负载均衡
1.目的: 本文简述Spring Cloud负载均衡之服务器负载均衡模式,使用组件为zuul. zuul作为Spring Cloud中的网关组件,负责路由转发.身份验证.请求过滤等等功能,那么我们可以 ...
- Kubernetes部署SpringCloud(二) 部署ZUUL与服务 非host, 伸缩与负载均衡
因为服务需要可缩容,所以不能使用host部署. 涉及两个应用,zuul,basic-info-api 验证,在k8s任意一个node 从zuul 访问 basic-info-api 创建一个Sprin ...
- Spring Cloud微服务Ribbon负载均衡/Zuul网关使用
客户端负载均衡,当服务节点出现问题时进行调节或是在正常情况下进行 服务调度.所谓的负载均衡,就是当服务提供的数量和调用方对服务进行 取舍的调节问题,在spring cloud中是通过Ribbon来解决 ...
- spring cloud: zuul(三): ribbon负载均衡配置
zuul的routes配置下path/url组合不支持负载均衡 下面介绍zuul的routes配置下的path/serviceId负载均衡配置 spring-boot-user微服务开启了:7901, ...
- springboot+spring session+redis+nginx实现session共享和负载均衡
环境 centos7. jdk1.8.nginx.redis.springboot 1.5.8.RELEASE session共享 添加spring session和redis依赖 <depen ...
- Spring Cloud之Zuul负载均衡
Zuul网关默认是实现负载均衡的,不需要任何配置.默认开启ribbon效果的 可以启启动两个服务端口,访问下.
随机推荐
- 对SPI进行参数化结构设计
前言 为了避免每次SPI驱动重写,直接参数化,尽量一劳永逸. SPI master有啥用呢,你发现各种外围芯片的配置一般都是通过SPI配置的,只不过有3线和四线. SPI slave有啥用呢,当外部主 ...
- Java架构师告诉你Spring IoC有什么好处呢
前言: 这个问题也一直困惑我很久,毕竟其他语言没有IOC也活的很好. 但是Spring在当时能够一统江湖,跟IOC真的有很大的关系. 在没有IOC的时代,New代表一切,女朋友都是可以New出来的. ...
- 《浏览器工作原理与实践》<10>作用域链和闭包 :代码中出现相同的变量,JavaScript引擎是如何选择的?
在上一篇文章中我们讲到了什么是作用域,以及 ES6 是如何通过变量环境和词法环境来同时支持变量提升和块级作用域,在最后我们也提到了如何通过词法环境和变量环境来查找变量,这其中就涉及到作用域链的概念. ...
- C# 直播录制视频
//项目引用 ffmpeg.exe 下载地址http://ffmpeg.org/ var time = DateTime.Now; ; //录制分钟 var fileName = Guid.NewGu ...
- nginx简单反向代理实例
一.要做什么? 实例最后实现的效果图: 我们在浏览器地址栏上输入 wangtong,代理服务器获取请求,将请求转发至指定的 tomcat 上 二.怎样做? 1.准备环境 虚拟中中需要安装 JDK+To ...
- api的日志
package cn.com.acxiom.coty.wechat.ws.bean.po; import java.util.Date; public class WebserviceLogWecha ...
- SQL JOB实现数据库同步
数据库同步是一种比较常用的功能.以下结合我自己的体会整理的,如果有理解不完全或者有误的地方望大牛不理赐教.下面介绍的就是数据库同步的两种方式: 1.SQL JOB的方式 sql Job的方式同步数据 ...
- Nginx中ngx_http_upstream_module模块
用于将多个服务器器定义成服务器器组,⽽而由 proxy_pass , fastcgi_pass 等指令进⾏行行引⽤用upstream backend {server backend1.example. ...
- [唐胡璐]Selenium技巧- Highlight页面元素
大家都知道QTP的对象高亮显示功能特别强大, Selenium Webderiver也可以实现此功能。 高亮显示有时候对Debug还是相当有用的。 解决脚本: 调用脚本: 结果显示:
- Java集合--TreeSet
转载请注明出处:http://www.cnblogs.com/skywang12345/admin/EditPosts.aspx?postid=3311268 第1部分 TreeSet介绍 TreeS ...