zuul服务网关
由于网关是通过注册中心找寻服务地址,所以需要注册到注册中心;
pom主要依赖如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
yml配置如下:
server:
port: 7020
spring:
application:
name: springcloud-zuul
eureka:
client:
serviceUrl:
defaultZone: http://localhost:4020/eureka/
启动类:
@SpringBootApplication @EnableEurekaClient @EnableZuulProxy //网关 public class ZuulMain { public static void main(String[] args) { SpringApplication.run(ZuulMain.class, args); } }
启动eureka服务,然后再启动其他服务:访问eureka,观察注册信息:
用zuul访问微服务(服务消费者)
结果如下:
如果不想暴露目标微服务的服务名,可以如下配置yml:
zuul: ignored-services: - open-feign-consumer #拒绝用目标微服务的名称来访问 prefix: /main #访问路径前缀 routes: #自定义路由配置 employee: service-id: open-feign-consumer #目标微服务的名称 path: /zuul-emp/** #新的访问路径