第五章 spring cloud zuul

1. 服务路由

将zuul自身注册为eureka服务,和eureka结合使用,eureka自动维护服务实例

2.path+serviceId及路径+服务名的方式实现路由

spring.application.name=mis_zuul_server_1
server.port=端口
##生产环境
#zuul.routes.mis_dingding_consumer.path=/mis_dingding_consumer/**
#zuul.routes.mis_dingding_consumer.serviceId=mis_dingding_consumer
#zuul.routes.mis_baoxiao_service.path=/mis_baoxiao_service/**
#zuul.routes.mis_baoxiao_service.serviceId=mis_baoxiao_service
##开发+测试环境
zuul.routes.mis_dingding_consumer_1.path=/mis_dingding_consumer_1/**
zuul.routes.mis_dingding_consumer_1.serviceId=mis_dingding_consumer_1
zuul.routes.mis_baoxiao_service_1.path=/mis_baoxiao_service_1/**
zuul.routes.mis_baoxiao_service_1.serviceId=mis_baoxiao_service_1

eureka.client.serviceUrl.defaultZone=http://用吗名:密码@localhost:1119/eureka
zuul.host.connect-timeout-millis=660000
zuul.host.socket-timeout-millis=1200000

ribbon.ReadTimeout=300000
ribbon.ConnectTimeout=600000

zuul.sensitive-headers=Access-Control-Allow-Origin
zuul.ignored-headers=Access-Control-Allow-Origin

和spring cloud config动态刷新机制结合使用,可以实现动态路由(动态修改路由规则、动态添加或删除过滤器)

3.zuul搭建完整过程

3.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>mis_zuul_server</groupId>
  <artifactId>mis_zuul_server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mis_zuul_server Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <springCloud.version>Greenwich.RELEASE</springCloud.version>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      <version>2.0.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${springCloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

3.2 application.properties

spring.application.name=mis_zuul_server_1
server.port=1118
##生产环境
#zuul.routes.mis_dingding_consumer.path=/mis_dingding_consumer/**
#zuul.routes.mis_dingding_consumer.serviceId=mis_dingding_consumer
#zuul.routes.mis_baoxiao_service.path=/mis_baoxiao_service/**
#zuul.routes.mis_baoxiao_service.serviceId=mis_baoxiao_service
##开发+测试环境
zuul.routes.mis_dingding_consumer_1.path=/mis_dingding_consumer_1/**
zuul.routes.mis_dingding_consumer_1.serviceId=mis_dingding_consumer_1
zuul.routes.mis_baoxiao_service_1.path=/mis_baoxiao_service_1/**
zuul.routes.mis_baoxiao_service_1.serviceId=mis_baoxiao_service_1

eureka.client.serviceUrl.defaultZone=http://用户名:密码@localhost:1119/eureka
zuul.host.connect-timeout-millis=660000
zuul.host.socket-timeout-millis=1200000

ribbon.ReadTimeout=300000
ribbon.ConnectTimeout=600000

zuul.sensitive-headers=Access-Control-Allow-Origin
zuul.ignored-headers=Access-Control-Allow-Origin



 3.3 启动类

package rest;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableDiscoveryClient
@EnableZuulProxy
@SpringCloudApplication
public class ZuulApplication {
    public static void main(String args[]){
        SpringApplication.run(ZuulApplication.class,args);
    }
}

3.4 启动测试

正确跳转。 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值