1.新建maven项目
2. 导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
3. 编写配置
server:
port: 9527
spring:
application:
name: springcloud-zuul
eureka:
client:
service-url:
defaultZone: http://eureka7101.com:7001/eureka,http://eureka7102.com:7002/eureka
instance:
instance-id: zuul9527
prefer-ip-address: true
zuul:
routes:
# 访问地址 http://localhost:9527/dept/dept/get/1
# name: springcloud-provieder-dept
name: # 这个name可以随便取
serviceId: springcloud-provieder-dept
# /dept/** 代替springcloud-provieder-dept
path: /dept/**
# 访问地址 http://localhost:9527//mydept/dept/get/1
prefix: /mydept
# 访问地址中含有springcloud-provieder-dept的将被忽略
# http://localhost:9527/mydept/dept/get/1
ignoredServices: springcloud-provieder-dept
4. 启动类注解
@SpringBootApplication
//声明一个Zuul代理
@EnableZuulProxy
public class ApplicationDeptZuul_9527 {
public static void main(String[] args) {
SpringApplication.run(ApplicationDeptZuul_9527.class,args);
}
}
5. 启动生产者消费者 及注册中心 在启动网关类
6. 测试
根据yml配置中的访问地址可以去测试