spring cloud中网关Zuul使用

spring cloud中网关Zuul使用

文章目录

  • spring cloud中网关Zuul使用
      • 一、简介
      • 二、开发步骤
      • 三、示例
        • 3.1 注册中心
          • 3.1.1 maven依赖
          • 3.1.2 application.yaml配置
          • 3.1.3 启动类
        • 3.2 服务
          • 3.2.1 maven依赖
          • 3.2.2 application.yaml配置
          • 3.2.3 服务相关类
        • 3.3 网关
          • 3.3.1 maven依赖
          • 3.3.2 application.yaml配置
          • 3.3.3 网关相关类
        • 4.4 测试

一、简介

Zuul是spring cloud中的网关,可以聚合各类服务后,再集中对外提供服务。

二、开发步骤

网关开发步骤如下:

  1. 在网关启动类添加注解@EnableZuulProxy;

  2. 在网关的application.yaml添加路由配置,如下:

    zuul:
      routes:
        spring-cloud-provider: /api/**  #指定 /api/** 路径请求路由到 spring-cloud-provider 服务
        spring-cloud-provider-one: /apiOne/** #指定 /apiOne/** 路径请求路由到 spring-cloud-provider-one 服务
    

三、示例

这里使用注册中心spring_cloud_register_center、服务spring_cloud_provider(运行不同名称和端口的多实例)、网关spring_cloud_zuul进行示例。

3.1 注册中心

注册中心为spring_cloud_register_center项目。

3.1.1 maven依赖

<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.2.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.dragon.studygroupId>
    <artifactId>spring_cloud_register_centerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring_cloud_register_centername>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Hoxton.SR6spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>
3.1.2 application.yaml配置
server:
  port: 8010
spring:
  application:
    name: spring-cloud-register-center
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/
    fetchRegistry: false #是否拉取注册信息,由于注册中心是单个结点,不需要拉取
    registerWithEureka: false #是否注册自己,由于当前就是注册中心,帮不需要注册
3.1.3 启动类
package com.dragon.study.spring_cloud_register_center;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudRegisterCenterApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudRegisterCenterApplication.class, args);
    }
}

3.2 服务

服务为spring_cloud_provider项目。

3.2.1 maven依赖

<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.2.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.dragon.studygroupId>
    <artifactId>spring_cloud_providerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring_cloud_providername>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Hoxton.SR6spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.6version>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>
3.2.2 application.yaml配置
server:
  port: 8011
spring:
  application:
    name: spring-cloud-provider
eureka:
  instance:
    prefer-ip-address: true #true表示使用ip注册,若为false,则表示使用系统hostname注册
  client:
    service-url:
      default-zone: http://localhost:8010/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
3.2.3 服务相关类

定义实体bean类Stu.java

package com.dragon.study.spring_cloud_provider.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Stu {
    private String stuId;
    private  Integer age;
}

定义服务controller类StuController.java

package com.dragon.study.spring_cloud_provider.controller;
import com.dragon.study.spring_cloud_provider.bean.Stu;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("stu")
public class StuController {
    @RequestMapping("getStu")
    private Object getStu(Stu stu) {
        System.out.println(stu);
        return stu;
    }
}

定义启动类SpringCloudProviderApplication.java

package com.dragon.study.spring_cloud_provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudProviderApplication.class, args);
    }
}

这里启动两个服务实例,端口不同,服务名分别为:spring-cloud-provider,spring-cloud-provider-one。

3.3 网关

网关为spring_cloud_zuul项目。

3.3.1 maven依赖

<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.1.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.dragon.studygroupId>
    <artifactId>spring_cloud_zuulartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring_cloud_zuulname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Hoxton.SR6spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-zuulartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

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

project>

3.3.2 application.yaml配置
server:
  port: 8013
spring:
  application:
    name: spring-cloud-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8010/eureka/
zuul:
  routes:
    spring-cloud-provider: /api/**  #指定 /api/** 路径请求路由到 spring-cloud-provider 服务
    spring-cloud-provider-one: /apiOne/** #指定 /apiOne/** 路径请求路由到 spring-cloud-provider-one 服务

3.3.3 网关相关类
  1. 定义启动类:
package com.dragon.study.spring_cloud_zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class SpringCloudZuulApplication {

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

}

4.4 测试

依次启动注册中心spring_cloud_register_center、服务spring_cloud_provider(两个实例,服务名分别为spring-cloud-provider,spring-cloud-provider-one)、网关spring_cloud_zuul。

测试:

#调用第一个路由服务
GET http://localhost:8013/api/stu/getStu?stuId=001&age=17
Accept: application/json

#调用第二个路由服务
GET http://localhost:8013/apiOne/stu/getStu?stuId=001&age=17
Accept: application/json

上面均能正常输出如下:

{
  "stuId": "001",
  "age": 17
}

你可能感兴趣的:(微服务)