微服务实战系列(六)-网关springcloud zuul

1. 场景描述

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

2. 解决方案

2.1 官网架构图

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

微服务实战系列(六)-网关springcloud zuul_第1张图片

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

2.2 开始撸码

2.2.1 new—>project

微服务实战系列(六)-网关springcloud zuul_第2张图片

2.2.2 选择组件

(1)注册客户端

微服务实战系列(六)-网关springcloud zuul_第3张图片

(2)路由Zuul

微服务实战系列(六)-网关springcloud zuul_第4张图片

next->next ->finish完成创建

2.2.3 代码介绍

说明:Zuul其实也是注册中心的客户端,主要也是3个类或文件。

微服务实战系列(六)-网关springcloud zuul_第5张图片

(1)pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.spc
    gateway
    0.0.1-SNAPSHOT
    gateway
    Demo project for Spring Boot

    
        1.8
        Greenwich.SR1
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



说明:

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

微服务实战系列(六)-网关springcloud zuul_第6张图片

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

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

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

微服务实战系列(六)-网关springcloud zuul_第7张图片


更多知识请关注公众号:「软件老王」,IT技术与相关干货分享,回复关键字获取对应干货,java,送必看的10本“武功秘籍”;图片,送100多万张可商用高清图片;面试,送刚毕业就能月薪“20k”的java面试题,软考,送官方pdf书籍与通关论文,后续会不断更新,比如“工具”,“视频“等,已经在整理中。

你可能感兴趣的:(微服务实战系列(六)-网关springcloud zuul)