Zuul是一个api网关管理组价,解决一个页面调用多个服务,则需要配置多个服务ip和端口,这个可以统一交给zuul管理,
直接使用zuul,有zuul给页面分配服务的地址(ip+端口),zuul默认会反向代理Eureka上的所有服务。
使用Zuul的好处是:若服务提供者的ip端口后,所有的服务消费者都需要改,这样比较麻烦,配置了Zuul只需该Zuul配置服务提供者的地址就行了
一、简单的使用
1、pom.xml依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
</dependencies>
2、application.yml
server:
port: 8280
spring:
application:
name: geteway-zuul
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://user:zj123@localhost:8761/eureka
zuul:
ignored-services: '*'
routes:
provider-user: /user/**
#第一次超时错误的解决
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
3、启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class GatewayZuulApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayZuulApplication.class, args);
}
}
微服务配置了Zuul后,消费者调用的原地址是:http://PROVIDER-USER/simple/{id} 当交给Zuul管理后调用地址是:http://geteway-zuul/user/simple/{id}
二、指定server_id的配置
zuul:
ignored-services: '*'
routes:
user:
path: /user/**
service-id: provider-user
三、指定url的
zuul:
ignored-services: '*'
routes:
user:
path: /user-url/**
url: http://localhost:8098/
四、负载均衡
zuul:
ignored-services: '*'
routes:
user:
path: /user-url/**
service-id: provider-user
ribbon:
eureka:
enabled: false
provider-user: # 这边是ribbon要请求的微服务的serviceId
ribbon:
listOfServers: http://localhost:8098,http://localhost:8100