- 导入依赖
<dependencies>
<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>
</dependencies>
- 配置application.yml文件
server:
port: 8086
#spring.application.name的优先级比eureka.instance.appname高
#两个同时设置则现实spring.application.name
spring:
application:
name: spring-cloud-zuul-8086 #项目服务名
#注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8082/eureka,http://localhost:8083/eureka,http://localhost:8084/eureka
instance:
appname: spring-cloud-zuul #注册到注册中心名称
zuul:
routes:
person: #自己随意定义,一般定义项目名称
path: /person
- 启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class AppZuulRun {
public static void main(String[] args) {
SpringApplication.run(AppZuulRun.class);
}
}
- 启动相关项目,进入eureka查看是否启动成功
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200831144808123.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpbGF5enp6,size_16,color_FFFFFF,t_70#pic_center)
- 正常访问地址->http://localhost:8081/selectPersonList
zuul代理后地址->http://localhost:8086//person/selectPersonList
or
zuul代理后地址->http://localhost:8086/spring-cloud-consume/selectPersonList
访问效果依旧
屏蔽原路径需在yml中加入该配置 :
ignored-services: * #表示禁用默认路由,只支持自己配置的地址