1 新建一个springboot的项目或模块,并添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot整合Eureka客户端组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--SpringCloud整合 zuul网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
2 启动类添加注解
@SpringBootApplication
@EnableEurekaClient //开启Eureka客户端
@EnableZuulProxy //开启zuul 网关
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(AppGateWay.class,args);
}
}
3 配置文件application.yml
server:
port: 9000
#服务的名称
spring:
application:
name: api-gateway
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.12:8761/eureka/
#自定义路由映射,设置 服务名称--url映射
zuul:
routes:
order-service: /apigateway/orderapi/**
product-service: /apigateway/productapi/**
#统一入口为上面的配置,其他入口忽略
ignored-patterns: /*-service/**
#处理http请求头为空的问题
sensitive-headers:
通过网关访问某接口的方法
http://网关服务的ip:网关服务的端口 /路由映射中服务名对应的url规则 /control层的UrlMapping
注意,这是本机测试,所有的服务在一台电脑上,所以都是localhost,但是是不同的服务或项目,不要混淆
比如之前访问 产品服务product-service项目中 的 产品列表接口
http://localhost:8771/productmapping/list
现在通过网关来访问
http://localhost:9000/apigateway/productapi/productmapping/list