spring cloud 学习笔记--网关zuul学习

  微服务是多个服务共同完成一件事情,那么“一致对外”就很有必要,就像我们去买面包,不可能先去找农民买小麦,再。。。。

盗图

spring cloud 引入zuul方式来实现这一功能

添加依赖

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

 

配置文件

单纯的url转发

#这里的配置表示,访问/app/** 直接重定向到(url路径后面是相同的,zuul只是做了转发)
http://127.0.0.1:8090/**
zuul.routes.baidu.path=/app/**
zuul.routes.baidu.url=http://127.0.0.1:8090/

通过注册来实现转发

#当请求/api/**会直接交给listOfServers配置的服务器处理  
#当stripPrefix=true的时候 (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/user/list)  
#当stripPrefix=false的时候(http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/api/user/list)  
zuul.routes.api.path=/api/**  
zuul.routes.api.stripPrefix=false  
api.ribbon.listOfServers=192.168.1.100:8080,192.168.1.101:8080,192.168.1.102:8080 
#zuul.routes.api-a.path=/producer/**
#zuul.routes.api-a.serviceId=spring-cloud-producer
serviceId服务的id
 

3、启动类

@SpringBootApplication
@EnableZuulProxy
public class GatewayServiceZuulApplication {

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

 

posted @ 2018-01-31 20:21  钟政123  阅读(160)  评论(0编辑  收藏  举报