## 原理 [15.api-gateway模块](12.api-gateway%E6%A8%A1%E5%9D%97.md) ## 架构图 ![](https://img.kancloud.cn/25/98/2598eba52de023ad18afae8acba689b1_912x670.png) ## 改依赖 将eureka-client依赖替换alibaba服务发现组件 ![](https://img.kancloud.cn/3d/4e/3d4ec4f05df155d93e0d5e82743d75da_1907x463.png) ![](https://img.kancloud.cn/95/bb/95bb2f20c842a03a9ded5770ad44aaed_1505x399.png) ## 改配置 ![](https://img.kancloud.cn/c1/39/c13933e82c18954a76c4edf52f9327b3_1574x312.png) ![](https://img.kancloud.cn/b5/58/b55867e58889870b85f9a7b8e45d92ae_1916x491.png) ![](https://img.kancloud.cn/f8/ed/f8edbe5f69100bb821247bbca2e22384_1920x534.png) api-gateway.yaml ``` #端口 server: port: 9200 #metrics management: endpoints: web: exposure: include: "*" endpoint: health: show-details: always spring: cloud: sentinel: # 指定sentinel 控制台的地址 dashboard: 127.0.0.1:8080 zuul: enabled: true order: pre: 10000 post: 1000 error: -1 filter: enabled: false datasource: # 名称随意 flow: nacos: server-addr: 119.27.173.249:8848 dataId: ${spring.application.name}-sentinel-gw-flow groupId: SENTINEL_GROUP # 规则类型,取值见: # com.alibaba.cloud.sentinel.datasource.RuleType rule-type: gw-flow api: nacos: server-addr: 119.27.173.249:8848 dataId: ${spring.application.name}-sentinel-gw-api-group groupId: SENTINEL_GROUP # 规则类型,取值见: # com.alibaba.cloud.sentinel.datasource.RuleType rule-type: gw-api-group datasource: druid: url: jdbc:mysql://59.110.164.254:3306/oauth-center?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false username: root password: root initial-size: 1 max-active: 20 min-idle: 1 # 配置获取连接等待超时的时间 max-wait: 60000 #打开PSCache,并且指定每个连接上PSCache的大小 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 validation-query: SELECT 'x' test-on-borrow: false test-on-return: false test-while-idle: true #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 time-between-eviction-runs-millis: 60000 #配置一个连接在池中最小生存的时间,单位是毫秒 min-evictable-idle-time-millis: 300000 filters: stat,wall # WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter #是否启用StatFilter默认值true web-stat-filter.enabled: true web-stat-filter.url-pattern: /* web-stat-filter.exclusions: "*.js , *.gif ,*.jpg ,*.png ,*.css ,*.ico , /druid/*" web-stat-filter.session-stat-max-count: 1000 web-stat-filter.profile-enable: true # StatViewServlet配置 #展示Druid的统计信息,StatViewServlet的用途包括:1.提供监控信息展示的html页面2.提供监控信息的JSON API #是否启用StatViewServlet默认值true stat-view-servlet.enabled: true #根据配置中的url-pattern来访问内置监控页面,如果是上面的配置,内置监控页面的首页是/druid/index.html例如: #http://110.76.43.235:9000/druid/index.html #http://110.76.43.235:8080/mini-web/druid/index.html stat-view-servlet.url-pattern: /druid/* #允许清空统计数据 stat-view-servlet.reset-enable: true stat-view-servlet.login-username: admin stat-view-servlet.login-password: admin #StatViewSerlvet展示出来的监控信息比较敏感,是系统运行的内部情况,如果你需要做访问控制,可以配置allow和deny这两个参数 #deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝。如果allow没有配置或者为空,则允许所有访问 #配置的格式 #<IP> #或者<IP>/<SUB_NET_MASK_size>其中128.242.127.1/24 #24表示,前面24位是子网掩码,比对的时候,前面24位相同就匹配,不支持IPV6。 #stat-view-servlet.allow= #stat-view-servlet.deny=128.242.127.1/24,128.242.128.1 # Spring监控配置,说明请参考Druid Github Wiki,配置_Druid和Spring关联监控配置 #aop-patterns= # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔 ################### mysq end ########################## # zipkin: # base-url: http://127.0.0.1:11008 redis: ################### redis 单机版 start ########################## host: 59.110.164.254 port: 6379 timeout: 6000 database: 8 lettuce: pool: max-active: -1 # 连接池最大连接数(使用负值表示没有限制),如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽) max-idle: 8 # 连接池中的最大空闲连接 ,默认值也是8 max-wait: -1 # # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException min-idle: 2 # 连接池中的最小空闲连接 ,默认值也是0 shutdown-timeout: 100ms ################### redis 单机版 end ########################## # cluster: # nodes: 130.75.131.237:7000,130.75.131.238:7000,130.75.131.239:7000,130.75.131.237:7001,130.75.131.238:7001,130.75.131.239:7001 # #130.75.131.237:7000,130.75.131.238:7000,130.75.131.239:7000,130.75.131.237:7001,130.75.131.238:7001,130.75.131.239:7001 # #192.168.3.157:7000,192.168.3.158:7000,192.168.3.159:7000,192.168.3.157:7001,192.168.3.158:7001,192.168.3.159:7001 # timeout: 1000 # 连接超时时间(毫秒) # lettuce: # pool: # max-active: 10 # 连接池最大连接数(使用负值表示没有限制),如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽) # max-idle: 8 # 连接池中的最大空闲连接 ,默认值也是8 # max-wait: 100 # # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException # min-idle: 2 # 连接池中的最小空闲连接 ,默认值也是0 # shutdown-timeout: 100ms mybatis-plus: global-config: banner: false configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath*:com/open/**/dao/*.xml security: oauth2: ignored: /api-file/statics/** , /test163/** , /api-auth/** , /doc.html ,/test111 ,/api-user/users-anon/login, /api-user/users/save, /user-center/users-anon/login,/document.html,**/v2/api-docs,/oauth/** ,/login.html ,/user/login,/**/**.css ,/**/**.js ,/getVersion token: store: type: redis #设置最大超时时间 ribbon: eager-load: enabled: true ServerListRefreshInterval: 10 #刷新服务列表源的间隔时间 httpclient: enabled: false okhttp: enabled: true ReadTimeout: 90000 ConnectTimeout: 90000 OkToRetryOnAllOperations: true MaxAutoRetries: 1 MaxAutoRetriesNextServer: 1 #设置最大容错超时时间 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 90000 #设置最大容错超时时间 hystrix: threadpool: default: coreSize: 100 maximumSize: 5000 allowMaximumSizeToDivergeFromCoreSize: true maxQueueSize: -1 command: default: circuitBreaker: forceOpen: false execution: timeout: enabled: true isolation: thread: timeoutInMilliseconds: 900000 zuul: ribbon-isolation-strategy: thread # 提高吞吐量 host: max-per-route-connections: 60000 # 适用于ApacheHttpClient,如果是okhttp无效。每个服务的http客户端连接池最大连接,默认是200 max-total-connections: 60000 # 适用于ApacheHttpClient,如果是okhttp无效。每个route可用的最大连接数,默认值是20 ribbon: eager-load: enabled: true threadPool: useSeparateThreadPools: true threadPoolKeyPrefix: api-gateway include: debug: header: true debug: request: true parameter: debugRequest retryable: false #关闭重试 sensitive-headers: #/oauth/token需要请求头处理 ignored-headers: Access-Control-Allow-Credentials,Access-Control-Allow-Origin,Access-Control-Allow-Methods add-host-header: true # add-proxy-headers: true #授权码跳转问题 routes: # 演示配置 # baidu: # path: /test163/** # url: http://59.110.164.254:9999 auth: path: /api-auth/** service-id: auth-server strip-prefix: false custom-sensitive-headers: true #http://blog.didispace.com/spring-cloud-zuul-cookie-redirect/ zuul授权码跳转问题 user: path: /api-user/** service-id: user-center strip-prefix: true custom-sensitive-headers: true #http://blog.didispace.com/spring-cloud-zuul-cookie-redirect/ zuul授权码跳转问题 file: path: /api-file/** service-id: file-center strip-prefix: true sms: path: /api-sms/** service-id: sms-center strip-prefix: true eureka: path: /api-eureka/** service-id: eureka-server strip-prefix: true generator: path: /api-generator/** service-id: generator-center strip-prefix: true log: path: /api-log/** service-id: log-center strip-prefix: true ssl-hostname-validation-enabled: false ratelimit: enabled: false #默认不限流 repository: REDIS policy-list: user: - limit: 10 refresh-interval: 60 type: - origin auth: - limit: 10 refresh-interval: 60 type: - origin file: - limit: 10 refresh-interval: 60 type: - origin log: - limit: 10 refresh-interval: 60 type: - origin #swagger: # butler: # api-docs-path: /v2/api-docs # swagger-version: 2.0 # resources: # auth: # name: auth-server # api-docs-path: /auth/v2/api-docs # swagger-version: 2.0 # client: # name: eureka-client # api-docs-path: /client/v2/api-docs # swagger-version: 2.0 # auth_client: # name: auth-client # api-docs-path: /auth-client/v2/api-docs # swagger-version: 2.0 swagger: butler: api-docs-path: v2/api-docs auto-generate-from-zuul-routes: true ignore-routes: eureka , eureka-server ,user-center,auth-server ,auth-login-page ,auth-js-process01,auth-js-process02,auth-css-process01,auth-css-process02,auth-login-process,auth-login-token,auth-login-authorize,auth-check-process # generate-routes: auth-server, auth-client logging: level: com.open.capacity: INFO org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE ``` ## 启动 ![](https://img.kancloud.cn/52/ac/52ac66096943db38f9637bc39861d008_1920x506.png) ## api-gateway 内部原理请参考03.模块详解部分