springCloud(十三) spring cloud api网关 -zuul

Posted 顾~小诺

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springCloud(十三) spring cloud api网关 -zuul相关的知识,希望对你有一定的参考价值。

1: 相关图片

图片说明 图片说明 图片说明 图片说明 图片说明

2:项目配置

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itlaoqi.springcloud</groupId>
    <artifactId>zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuul</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
    </properties>


    <repositories>
        <repository>
            <id>aliyun-nexus</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>aliyun-public</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>

        <repository>
            <id>aliyun-jcenter</id>
            <url>https://maven.aliyun.com/repository/jcenter</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2: ZuulApplication.java

package com.itlaoqi.springcloud.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication {

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

}

3: 配置文件

spring:
  application:
    name: zuul-proxy
eureka:
  client:
    service-url:
      defaultZone:
        http://localhost:8761/eureka
zuul:
  routes:
    book-service-api:
      path: /book/**                 #从zuul这端做的映射
      serviceId: book-service  #微服务名称
    member-service-api:
      path: /member/**
      serviceId: member-service  
server:
  port: 80

3: 浏览器访问

http://localhost/book/test?param=123
{"param":"123","port":8081}

http://localhost/member/sendMess?phone=123
{"code":0,"message":"发送短信成功,手机号为:123"}

3: Zuul 进行负载均衡设置

图片说明 图片说明 图片说明

以上是关于springCloud(十三) spring cloud api网关 -zuul的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud - Spring Cloud 之 Gateway网关(十三)

SpringCloud - Spring Cloud 之 Gateway网关,Route路由,Predicate 断言,Filter 过滤器(十三)

springCloud(十三) spring cloud api网关 -zuul

企业分布式微服务云SpringCloud SpringBoot mybatis (十三)Spring Boot整合MyBatis

SpringCloud - Spring Cloud Alibaba 之 Seata分布式事务服务;TCC事务模式机制(二十三)

SpringCloud - Spring Cloud Alibaba 之 SkyWalking 分布式链路跟踪;服务,端点,实例;IDEA配置;告警(十三)