zoukankan      html  css  js  c++  java
  • springcloud-路由Zuul-copy

    1. 场景描述

    今天接着介绍springcloud,今天介绍下springcloud的路由网关-Zuul,外围系统或者用户通过网关访问服务,网关通过注册中心找到对应提供服务的客户端,网关也需要到注册中心进行注册。

    2. 解决方案

    2.1 官网架构图

    先把官网的图在贴一下,便于理解。

    说明: gateway负责与外部进行交互,是sprincloud微服务对外的窗口。

    2.2 开始撸码

    2.2.1 new—>project

    2.2.2 选择组件

    (1)注册客户端

    (2)路由Zuul

    next->next ->finish完成创建

    2.2.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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.spc</groupId>
        <artifactId>gateway</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>gateway</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <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>
            </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>
    
    

    说明:

    springboot两个starter:client与Zuul。

    (2)application启动类

    package com.spc.gateway;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableZuulProxy
    public class GatewayApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(GatewayApplication.class, args);
        }
    
    }
    
    

    说明:

    启动类主要三个标签:@SpringBootApplication、@EnableEurekaClient、@EnableZuulProxy。

    (3)application.yml

    spring:
      application:
        name: gateway
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 9000
    

    说明:

    主要就一个地址:http://localhost:8761/eureka/,这个是注册中心地址。

    2.2.4 路由访问

    (1)启动注册中心、客户端、路由Zuul

    (2)通过路由访问client客户端服务

    路由地址:http://localhost:9000/client

    访问格式:路由地址+客户端服务注册名称,对应上面的application。


    I'm 软件老王,如果觉得还可以的话,关注下呗!如有不准确或疑问的地方,可通过讨论区、QQ沟通,多谢!

  • 相关阅读:
    Bootstrap4(28): 滚动监听(Scrollspy)
    Bootstrap4(27): 弹出框
    Bootstrap4(26): 提示框
    Bootstrap4(25): 模态框
    Bootstrap4(24): 轮播
    Bootstrap4(23): 自定义表单
    Bootstrap4(22): 输入框组
    Bootstrap4(21): 表单控件
    Bootstrap4(20): 表单
    Bootstrap4(19): 面包屑导航(Breadcrumb)
  • 原文地址:https://www.cnblogs.com/hanease/p/14520663.html
Copyright © 2011-2022 走看看