pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
bootstrap.xml
server:
port: 9600
spring:
application:
name: gateway #指定服务名
servlet:
multipart:
max-request-size: 20MB
max-file-size: 20MB
eureka:
instance:
hostname: cetc-gateway
prefer-ip-address: true
client:
registerWithEureka: true #是否将自己注册到Eureka服务中
fetchRegistry: true #要在本地缓存注册表信息
service-url:
defaultZone: http://cetc-eureka:1111/eureka
zuul:
sensitive-headers: #敏感header的设置,此处为空,默认Authorization不被转发
# ignored-services: '*' #该配置关闭,则zuul会默认代理所有注册在Eureka上的微服务
routes:
cetc-admin: #路由的一个名称,可以任意起名
path: /api-admin/** #拦截到的路径
serviceId:admin #模块的服务名
cetc-log:
path: /api-log/**
serviceId: cetc-log
zuul还可以设置某些路径,不暴露给请求客户端->详细信息
application.java
//在启动类上添加注解@EnableZuulProxy,声明一个zuul代理。
// 代码中的@EnableDiscoveryClient注解为Eureka的客户端注解。
@SpringBootApplication
@EnableZuulProxy
@EnableFeignClients
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class);
}
}