Spring cloud示例源码,网关Zuul示例代码

参考https://blog.csdn.net/rishengcsdn/article/details/89956473

本章演示一下Zuul的功能,

 

4。zuul 网关,动态路由,端口:2222

 

网关可以将分散于各个端口的分散的服务变成一个统一的端口访问,spring boot不能在统一端口部署不同应用的问题在这个网关就能解决一些。当然,还有自定义一些过滤规则,来实现复杂的分发。

首先是zuul的pom.xml

=======================================================================

<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>com.ecctest</groupId>
  <artifactId>zuul</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>zuul</name>


<dependencies>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.5.3.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>

    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    <version>1.3.5.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</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>
   <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <version>1.5.3.RELEASE</version>

</dependency>
   <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>1.5.3.RELEASE</version>

</dependency>
   <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    <version>1.5.3.RELEASE</version>

</dependency>

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-sts</artifactId>
    <version>1.11.125</version>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-autoscaling</artifactId>
    <version>1.11.125</version>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-ec2</artifactId>
    <version>1.11.125</version>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-route53</artifactId>
    <version>1.11.125</version>
</dependency>
<dependency>
    <groupId>org.hdrhistogram</groupId>
    <artifactId>HdrHistogram</artifactId>
    <version>2.1.9</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-cbor</artifactId>
  <version>2.8.8</version>
  </dependency>
 <dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
  <version>2.8.8</version>  
   </dependency>
 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>1.5.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.5.12</version>
</dependency>
<dependency>
    <groupId>com.netflix.archaius</groupId>
    <artifactId>archaius-core</artifactId>
    <version>0.7.4</version>
<!--     <exclusions> -->
<!--         <exclusion> -->
<!--             <groupId>commons-logging</groupId> -->
<!--             <artifactId>commons-logging</artifactId> -->
<!--         </exclusion> -->
<!--     </exclusions> -->
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-crypto</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>
<!-- <dependency> -->
<!--     <groupId>com.fasterxml</groupId> -->
<!--     <artifactId>classmate</artifactId> -->
<!--     <version>1.3.3</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!--     <groupId>com.google.guava</groupId> -->
<!--     <artifactId>guava</artifactId> -->
<!--     <version>18.0</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!--     <groupId>com.google.code.gson</groupId> -->
<!--     <artifactId>gson</artifactId> -->
<!--     <version>2.8.0</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!--   <groupId>commons-codec</groupId> -->
<!--   <artifactId>commons-codec</artifactId> -->
<!--   <version>1.10</version> -->
<!-- </dependency> -->

<!-- <dependency> -->
<!--   <groupId>commons-collections</groupId> -->
<!--   <artifactId>commons-collections</artifactId> -->
<!--   <version>3.2.2</version>  -->
<!--  </dependency> -->
 </dependencies>

<!-- <dependencyManagement>   -->
<!--       <dependencies>   -->
<!--             <dependency>   -->
<!--                 <groupId>org.springframework.cloud</groupId>   -->
<!--                 <artifactId>spring-cloud-dependencies</artifactId>   -->
<!--                 <version>Dalston.SR4</version>   -->
<!--             </dependency>   -->
<!--     </dependencies>   -->
<!-- </dependencyManagement>   -->
</project>

====================================================================

启动类

package com.ecctest.zuul;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;


@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ZuulApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ZuulApplication.class).run(args);
    }


}


===================================================================

application.yml内容:

 

spring:
  application:
    name: zuul

server:
  port: 2222

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1112/eureka
      
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 300000

ribbon:
  ReadTimeout: 150000
  SocketTimeout: 150000
  ConnectTimeout: 150000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1
  eureka:
    enabled: true

zuul:
  routes:
    fs:
      path: /fs/**
      url: http://localhost:8083
    fc:
      path: /fc/**
      url: http://localhost:8084

================================================================

由于网关已经配置了固定路由规则,现在可以通过2222端口访问eFeignClient和eFeignServer的rest服务了。

现在访问注册中心,可以看到多了一个Zuul的服务注册上了了。

 

现在通过下面链接可以访问到eFeignClient和eFeignServer的rest服务:

http://localhost:2222/fc/hello

http://localhost:2222/fs/consumer

 

现在可以看到,通过2222端口可以访问到所有的应用了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值