spring cloud 入门实践系列 - zuul

Zuul is the front door for all requests from devices and web sites to the backend of the Netflix streaming application. As an edge service application, Zuul is built to enable dynamic routing, monitoring, resiliency and security. It also has the ability to route requests to multiple Amazon Auto Scaling Groups as appropriate.

Zuul是从设备和网站到Netflix流应用的后端的所有请求的前门。 作为边缘服务应用程序,Zuul旨在实现动态路由,监控,弹性和安全性。 它还可以根据需要将请求路由到多个Amazon Auto Scaling Groups。

启动eureka注册中心

可参见之前文章

创建两个eureka服务

service-first 暴露在8961端口
@RestController
@RequestMapping("firstapi")
public class FirstApiController {

    @RequestMapping("speak")
    public String doSpeak(@RequestParam String words){
        return "port:8961,woooooo,I am speaking of ["+words+"]";
    }

}
service-second 暴露在8962端口
@RestController
@RequestMapping("secondApi")
public class SecondApiController {
    @RequestMapping("sing")
    public String sing(@RequestParam String song){
        return "port:8962,hoooooooo,I am singing of ["+song+"]";
    }
}

创建zuul服务

项目结构

这里写图片描述

zuul启动类
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ZuulwareApplication {

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

@EnableZuulServer - 普通网关,只支持基本的route与filter.
@EnableZuulProxy - 配合上服务发现与熔断开关的上面这位增强版,具有反向代理功能.

zuul配置文件application.yml,zuul服务暴露在8963端口
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8963
spring:
  application:
    name: service-zuul
zuul:
  routes:
   firstapi:
     path: /firstapi/**
     serviceId: service-first
   secondapi:
          path: /secondapi/**
          serviceId: service-second

上面配置的含义是请求路径uri为/firstapi开头的转发至service-first服务
请求路径uri为/firstapi开头的转发至service-second服务
特别注意:1. routes节点下的元素要与path的前缀一致
2. path 后面的/**是指服务对应的全路径,比如我在service-first中定义的FirstApiController,访问的路径是http://172.16.153.1:8961/firstapi/speak?words=zxl,如果我们想通过zuul服务转发出去,则访问路径应该是http://172.16.153.1:8961/firstapi/firstapi/speak?words=zxl 第一个firstapi代表zuul配置文件中配置的route地址,第二个firstapi代表FirstApiController类的访问路径配置,这点一定要注意。千万不要使用http://172.16.153.1:8961/firstapi/speak?words=zxl 这种方式去访问,因为zuul将该路径请求转发给service-first后,是使用/speak去匹配,这样是找不到对应处理类的。

启动2个eureka服务和zuul服务

这里写图片描述

http://172.16.153.1:8963/firstapi/firstapi/speak?words=zxlhgggggggg
这里写图片描述

http://172.16.153.1:8963/secondapi/secondApi/sing?song=zxl
这里写图片描述

可见我们通过zuul的8693端口访问出去的请求,最终被zuul转发到8691和8692对应的服务上去了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于Spring Cloud入门教程,你可以参考以下步骤: 1. 安装Java开发环境:确保你的机器上已经安装了Java Development Kit(JDK)。 2. 创建Spring Boot项目:使用Spring Initializr(https://start.spring.io/)创建一个新的Spring Boot项目。在这个页面上,你可以选择使用的Spring Boot版本、添加的依赖以及其他项目配置。 3. 添加Spring Cloud依赖:在创建项目时,你可以选择添加Spring Cloud的依赖。选择适合你需求的Spring Cloud组件,如Eureka、Zuul、Feign等。 4. 配置Spring Cloud组件:根据你选择的Spring Cloud组件,进行相应的配置。比如使用Eureka作为服务注册中心,需要配置Eureka Server;使用Feign进行服务调用,需要配置Feign Client等。 5. 创建微服务:根据你的业务需求,创建相应的微服务。每个微服务都是一个独立的Spring Boot应用。 6. 注册微服务到服务注册中心:在每个微服务启动时,将自己注册到服务注册中心。这样其他微服务就可以通过服务注册中心来发现和调用该微服务。 7. 实现服务间通信:使用Spring Cloud提供的组件进行服务间通信。比如使用Feign调用其他微服务提供的接口。 8. 配置负载均衡:可以使用Spring Cloud提供的负载均衡组件(如Ribbon)来实现负载均衡。 9. 实现服务容错:使用Spring Cloud提供的熔断器(如Hystrix)来实现服务容错。 10. 部署和测试:将微服务部署到服务器上,并进行测试验证。 这只是一个简单的入门教程的大致流程,具体的实现细节和配置可能因你的需求和环境而有所不同。你可以根据这个流程逐步学习和实践Spring Cloud,进一步深入了解和应用它。 希望对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值