zoukankan      html  css  js  c++  java
  • Spring cloud Zuul 服务网关

    上面的是一个最基本的微服务架构

    1:Eureka Server注册中心

    2:服务集群里面的应用ServiceA,ServiceB向注册中心进行注册和订阅服务

    3:Open ServiceA 和Open ServiceB是RestFullAPI服务,通过负载均衡等,对外提供接口的调用

    基本功能都能实现,但实际上还是有很多缺陷

    1:运维:系统的路由等配置需要运维人员手工配置,易出错

    2:开发:每个服务都要有相应的权限控制,当接口数量比较大的时候,权限控制会产生很大的代码量

    3:接口无法实现复用

    springCloud的服务网关可以解决上面的问题SpringCloud Zuul

     @SpringCloudApplication 注解在zuul依赖包下面 可以完美代替@SpringBootApplication,@EnableDiscoveryClient,@EnableCircuitBreaker三个注解

    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>com.smart</groupId>
        <artifactId>zuul</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>zuul</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.RC1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-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>Brixton.RELEASE</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>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
    
    </project>

    2:springCloud主程序部分

    package com.smart;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.SpringCloudApplication;
    import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
    
    //@SpringBootApplication
    @EnableZuulProxy
    @SpringCloudApplication
    public class ZuulApplication {
    
        public static void main(String[] args) {
    
            SpringApplication.run(ZuulApplication.class, args);
        }
    }

    3:配置文件

    spring.application.name=api-gateway
    server.port=5555
    
    # routes to serviceId
    zuul.routes.api-a.path=/api-a/**
    zuul.routes.api-a.serviceId=service-A
    zuul.routes.spi-a.name=service-A
    
    zuul.routes.api-b.path=/api-b/**
    zuul.routes.api-b.serviceId=service-B
    
    # routes to url
    zuul.routes.api-a-url.path=/api-a-url/**
    zuul.routes.api-a-url.url=http://localhost:2222/
    
    eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

    配置文件这里映射两个服务,service-A  service-B,

    4:启动eureka,service-A ,service-B,zuul

    访问http://localhost:5555/api-a-url/add?a=1&b=2,

     

     

    映射方式url映射,serviceId映射,这里发现serviceId映射方式并不能实现,报错,然而我并不清楚发生了什么

     

     

     注:关于映射

    这里就说下url映射吧

    # routes to url

    zuul.routes.api-a-url.path=/api-a-url/**

    zuul.routes.api-a-url.url=http://localhost:2222/

    当访问http://localhost:5555/api-a-url/add?a=1&b=2会涉及到一个转发,实际访问的是http://localhost:2222/add?a=1&b=2

     

     

    
    

     

  • 相关阅读:
    【Tomcat】使用Eclipse发布项目时,项目启动路径错误。
    Unsupported major.minor version 52.0
    【转载】设置event.cancelBubble,使触发子元素的onclick不同时触发父元素的onclick
    【MG】测试
    【Tomcat】使用Eclipse运行Tomcat7源码
    【微信公众号】WeixinJSBridge.call('closeWindow')无效
    【微信公众号】使用a标签打开链接显示空白
    【JAVA】导出jar包时,Class files on classpath not found
    【JAVA】修改项目包名
    lesson3.1:java公平锁和非公平锁及读写锁
  • 原文地址:https://www.cnblogs.com/shuzhongruyu/p/8968766.html
Copyright © 2011-2022 走看看