Zuul网关设置
先来介绍一些zuul
简单来说就是一个拦截器,使用这个以后,可以让众多的微服务可以通过一个固定的URL访问服务;
导入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> <version>1.4.7.RELEASE</version> </dependency>
|
启动类:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.saxon.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication @EnableZuulProxy public class SpringcloudZull_9527 { public static void main (String[] args) { SpringApplication.run (SpringcloudZull_9527.class,args); } }
|
application.yaml:
1 2 3 4 5 6 7 8 9 10 11 12
| eureka: client: service-url: defaultZone: http://localhost7001.com:7001/eureka/,http://localhost7002.com:7002/eureka/,http://localhost7003.com:7003/eureka/ instance: instance-id: zuul server: port: 9527 spring: application: name: zuul
|
由于这个也是一个服务,所以需要我们在注册中心注册一下;其他的就是配置一些应用名字,一些端口号,实例ID
配置以上我们可以使用自己的地址加端口号加上服务名字访问相同的服务;例如下面两个的访问的服务结果是一样的
1 2
| http://localhost:9527/saxon/get/1 http://localhost:8001/get/1
|
结果都是一样的,但是如果我们不想让外界知道我们的微服务的名字,我们就可以选择隐藏微服务的名字
配置文件:
1 2 3 4
| zuul: routes: mydept.path: /saxonmo/** mydept.serviceId: saxon
|
以下的结果是一样的:
这个时候我们的加上服务名也可以直接访问,如果我们想要让服务名无法访问可以做以下的配置
1 2 3 4 5 6
| zuul: routes: mydept.path: /saxonmo/** mydept.serviceId: saxon ignored-services: "*" prefix: /saxon
|
做了这个配置就只有下面的格式才可以访问对应的服务:
http://localhost:9527 /saxon /saxonmo /get/1
==最前面的是我们的地址,第一个/Saxon是我们自己加的前缀,第二个/saxonmo是我们的path,最后一个是controller请求的格式==