iyaya

导航

微服务网关概述(自用)

1.什么是网关?所有请求的出入口 (gateway 对内网关,所有微服务请求的出入口)

微服务网关就是一个系统,通过暴露该微服务网关系统,方便我们进行相关的鉴
权,路由转发,安全控制,日志统一处理,易于监控的相关功能。

核心组件:优瑞卡 ,瑞本 ,网关,熔断器,配置中心

2.搭建网关

创建heima-leadnews-admin-gateway 网关微服务 (负责admin后台的网关服务)

2.引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

3.构建网关的启动类

@SpringBootApplication
@EnableDiscoveryClient
public class AdminGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminGatewayApplication.class,args);
    }
}

4.配置网关的核心配置文件(路由转发规则)

server:
  port: 6001
spring:
  application:
    name: leadnews-user-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.200.130:8848
    gateway:
      globalcors:
        cors-configurations:
          '[/**]': # 匹配所有请求
            allowedOrigins: "*" #跨域处理 允许所有的域
            allowedMethods: # 支持的方法
            - GET
            - POST
            - PUT
            - DELETE
      routes:
      # 平台管理
      - id: admin
        uri: lb://leadnews-admin
        predicates:
        - Path=/admin/**  #http://localhost:6001/admin/api/v1/channel/list -->http://localhost:9001/api/v1/channel/list
        filters:
        - StripPrefix= 1
       # user微服务
      - id: user
        uri: lb://leadnews-user
        predicates:
        - Path=/user/**  #http://localhost:6001/admin/api/v1/channel/list -->http://localhost:9001/api/v1/channel/list
        filters:
        - StripPrefix= 1

posted on 2020-12-26 20:50  iyaya  阅读(215)  评论(0编辑  收藏  举报