完整项目代码地址参考: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控制台上的节点信息;