springcloud zuul使用

本文介绍如何使用Zuul作为微服务架构中的统一网关,实现对多个服务端口的分发功能。通过配置文件,将产品和服务订单的请求分别路由到对应的服务端口,同时设置跨域请求支持。

主要使用了分发的功能

 

三个服务端口分别是9091 9092 9093

现在使用zuul 统一对外暴露端口是9090

新建一个zuul服务

pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>1.5.4.RELEASE</version>
        </dependency>

    </dependencies>
View Code

 

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableEurekaServer
@EnableZuulProxy
public class ZuulServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication.class, args);
    }
    
    /**
     * 项目可以进行跨域请求。
     * @return
     */
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedOrigins("*").allowedMethods("*");
            }
        };
    }
View Code

 

配置文件

server.port=9090
eureka.client.serviceUrl.defaultZone=http://10.38.0.5:8761/eureka/
spring.application.name=zuulserver
zuul.routes.product.path=/product/**
zuul.routes.product.service-id=product
zuul.routes.order.path=/order/**
zuul.routes.order.service-id=order

 

另外的服务是product 和 order 

service-id 是其他服务在ererka上的服务名

转载于:https://www.cnblogs.com/lyon91/p/10065672.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值