Spring Cloud --zuul搭建

 Zuul的作用就是路由转发和过滤, 即将请求转发到微服务或拦截请求; Zuul默认集成了负载均衡功能。

      下面创建一个zuul工程:

      打开IntelliJ Idea ---> New Project ---> 选择Spring Initializr ---> 设置包名 ---> 勾选web、zuul、Eureka Discovery -> 设置存储路径。

一、路由:

 在入口类添加注解@EnableZuulProxy,打开Zuul 功能

package com.springcloud.zuul;

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

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulApplication {

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

二、 修改配置文件,指定注册中心和路由规则


#网关端口
server.port=13000
#服务名称
spring.application.name=zuul
#注册地址
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
# 路由规则配置
zuul.routes.server1.path=/server1/**
# 需要和映射的Module配置文件中 spring.application.name 一致
zuul.routes.server1.serviceId=server1

三、在 模块server1中 增加controller

/**
 * Created by joe强 on 2018/9/27 15:13
 */
@RestController(value = "/server1")
public class ZuulController {
    @Value("${server.port}")
    String port;

    @RequestMapping("/server1Zuul")
    public String serverZuul() {
        return "port is" + port;
    }
}

 server1 配置如下:

#这里 和 网关 配置中的 zuul.routes.server1.serviceId 保持一致
spring.application.name=server1
server.port=8081
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8080/eureka/

    四、启动 Euaeka 注册中心,server1 和当前Zuul 网关

    访问 注册中心:http://localhost:8080/

五、访问 接口 :

http://localhost:13000/server1/server1Zuul

可以看到 zuul 进行了转发。 zuul还可以实现验签功能和自定义过滤功能, 功能跟SpringMVC的拦截器一样 后面补充!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值