完整项目代码地址参考: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进行负载均衡的更多相关文章

  1. springCloud搭建微服务集群+Zuul服务器端负载均衡

    概述 最近研究了一下springCloud的微服务集群,主要用到了SpringCloud的服务发现和服务器端负载均衡,所有的项目都是用的springboot,可以和springCloud无缝对接. 技 ...

  2. SpringBoot(三) - Ribbon客户端负载均衡,Zuul网关,Config配置中心

    1.Ribbon客户端负载均衡 1.1 依赖 1.2 配置信息 # feign默认加载了ribbon负载均衡,默认负载均衡机制是:轮询 # 负载均衡机制是添加在消费端(客户端)的,如果改为随机,指定服 ...

  3. spring cloud使用zuul实现反向代理和负载均衡

    首先,这篇文章参考的是http://blog.didispace.com/springcloud5/这位大牛的博客.本人是通过这篇博客来学习zuul的,现在写的博客只是个人在学习时个人的一些感受和理解 ...

  4. Spring Cloud负载均衡:使用zuul作服务器端负载均衡

    1.目的: 本文简述Spring Cloud负载均衡之服务器负载均衡模式,使用组件为zuul. zuul作为Spring Cloud中的网关组件,负责路由转发.身份验证.请求过滤等等功能,那么我们可以 ...

  5. Kubernetes部署SpringCloud(二) 部署ZUUL与服务 非host, 伸缩与负载均衡

    因为服务需要可缩容,所以不能使用host部署. 涉及两个应用,zuul,basic-info-api 验证,在k8s任意一个node 从zuul 访问 basic-info-api 创建一个Sprin ...

  6. Spring Cloud微服务Ribbon负载均衡/Zuul网关使用

    客户端负载均衡,当服务节点出现问题时进行调节或是在正常情况下进行 服务调度.所谓的负载均衡,就是当服务提供的数量和调用方对服务进行 取舍的调节问题,在spring cloud中是通过Ribbon来解决 ...

  7. spring cloud: zuul(三): ribbon负载均衡配置

    zuul的routes配置下path/url组合不支持负载均衡 下面介绍zuul的routes配置下的path/serviceId负载均衡配置 spring-boot-user微服务开启了:7901, ...

  8. springboot+spring session+redis+nginx实现session共享和负载均衡

    环境 centos7. jdk1.8.nginx.redis.springboot 1.5.8.RELEASE session共享 添加spring session和redis依赖 <depen ...

  9. Spring Cloud之Zuul负载均衡

    Zuul网关默认是实现负载均衡的,不需要任何配置.默认开启ribbon效果的 可以启启动两个服务端口,访问下.

随机推荐

  1. php实现命令行里输出带颜色文字

    今天执行composer的时候看到命令窗口出现的提示里面有的关键性部分带有颜色,于是很好奇研究了一下,在这里记录下来 其实在命令行输出带颜色字体主要是使用的 ANSI 转义字符实现的,我们先看个例子: ...

  2. css之弹性盒模型

    弹性盒子(Flexible Box/filebox)是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式.引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子 ...

  3. 使用Canvas压缩图片

    讲干货,不啰嗦,当涉及对图片有质量压缩要求的时候,可以使用Canvas实现图片压缩. 步骤: 1.获取img元素,既要压缩的图片 2.创建canvas对象 3.使用canvas的drawImage方法 ...

  4. friend

    #include <iostream> using namespace std; //friend 友元,效率的问题 //get 方法和set方法,是标准封装的结果,friend破坏了这种 ...

  5. MySQL增量备份与恢复实例

    小量的数据库可以每天进行完整备份,因为这也用不了多少时间,但当数据库很大时,就不太可能每天进行一次完整备份了,这时候就可以使用增量备份.增量备份的原理就是使用了mysql的binlog日志. 本次操作 ...

  6. XSS学习收集

    XSS platform supporting HTTPShttps://www.w0ai1uo.org/xss/xss.php?do=loginhttps://x.secbox.cn/index.p ...

  7. flash多进程写操作

    1 应用场景介绍   硬件条件:使用stm32 MCU   软件条件:协议栈应用   协议栈简单介绍如下:   类似于OSI七层模型,所涉及的协议栈包括应用层,网络层,链路层,物理层,如下图:   在 ...

  8. sh make.sh fatal error: opencv2/opencv.hpp: No such file or directory

    问题: sh make.sh fatal error: opencv2/opencv.hpp: No such file or directory 解决: sudo apt-get install l ...

  9. Java 基础 enum枚举类 的创建/使用/接口继承 ,以及手动创建枚举类的对象为:public static final

    笔记: import java.lang.*; /**一:枚举类 : enum Season implements info { s1(),s2(),s3(),s4() }; //s1--s4 放在S ...

  10. 彻底解决matplotlib中文乱码问题

    1.环境查看a.系统版本查看 [hadoop@p168 ~]$ cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) b.系统中文字 ...