SpringCloud总结--Zuul的使用

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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值