环境搭建
1、创建工程zuul-server
2、引入pom文件:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
3. 创建启动类并开启zuul网关
@SpringBootApplication
@EnableZuulProxy //开启zuul网关
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
4、创建配置文件
\#端口
server:
port: 8080
#服务名称
spring:
application:
name: zuul-service
路由配置
1、基础路由配置
zuul:
routes:
product-service: product-service #如果路由id和对应的服务serviceId一致的话
2、添加eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3、配置启动类添加eureka服务发现功能
4、在zuul网关服务中添加eureka相关配置
\#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册