隨筆:13 文章:7 評論:0 引用:0
          BlogJava 首頁 發(fā)新隨筆
          發(fā)新文章 聯(lián)系 聚合管理

          2022年5月9日

          網(wǎng)關(guān)
          發(fā)送請求需要知道商品服務(wù)的地址,如果商品服務(wù)器有100服務(wù)器,1號掉線后,
          還得改,所以需要網(wǎng)關(guān)動態(tài)地管理,他能從注冊中心中實時地感知某個服務(wù)上
          線還是下線。
          請求也要加上詢問權(quán)限,看用戶有沒有權(quán)限訪問這個請求,也需要網(wǎng)關(guān)。
          所以我們使用spring cloud的gateway組件做網(wǎng)關(guān)功能。
          網(wǎng)關(guān)是請求瀏覽的入口,常用功能包括路由轉(zhuǎn)發(fā)權(quán)限校驗限流控制等。springcloud gateway取代了zuul網(wǎng)關(guān)。
          三大核心概念:
          Route: The basic building block of the gateway. It is defined by an ID, a 
          destination URI, a collection of predicates斷言, and a collection of filters. 
          A route is matched if the aggregate predicate is true.
          發(fā)一個請求給網(wǎng)關(guān),網(wǎng)關(guān)要將請求路由到指定的服務(wù)。
          路由有id,
          目的地uri,
          斷言的集合,
          匹配了斷言就能到達(dá)指定位置,
          Predicate斷言:
          This is a Java 8 Function Predicate. The input type is a Spring 
          Framework ServerWebExchange. This lets you match on anything from the 
          HTTP request, such as headers or parameters.就是java里的斷言函數(shù),匹配請求里的任何信息,包括請求頭等
          Filter:
          These are instances of Spring Framework GatewayFilter that have been 
          constructed with a specific factory. Here, you can modify requests and
          responses before or after sending the downstream request.
          過濾器請求和響應(yīng)都可以被修改。
          客戶端發(fā)請求給服務(wù)端。中間有網(wǎng)關(guān)。先交給映射器,如果能處理就交給handler
          處理,然后交給一系列filer,然后給指定的服務(wù),再返回回來給客戶端。
          12.1 創(chuàng)建模塊gulimall-gateway
          <dependency>
                      <groupId>com.zyn.glmall</groupId>
                      <artifactId>glmall-common</artifactId>
                      <version>0.0.1-SNAPSHOT</version>
          </dependency>
          1 在pom.xml引入
          版本環(huán)境需保持一致
          <spring-boot.version>2.1.8.RELEASE</spring-boot.version>
          <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
          2 開啟注冊服務(wù)發(fā)現(xiàn)@EnableDiscoveryClient
          @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
          @EnableDiscoveryClient
          public class GulimallGatewayApplication {
              public static void main(String[] args) {
                  SpringApplication.run(GulimallGatewayApplication.class, args);
              }
          }
          3 配置nacos注冊中心地址applicaion.properties
          spring.application.name=glmall-gateway
          spring.cloud.nacos.discovery.server-addr=192.168.11.1:8848
          server.port=88
          4 bootstrap.properties 填寫配置中心地址
          spring.application.name=glmall-coupon
          spring.cloud.nacos.config.server-addr=192.168.11.1:8848
          spring.cloud.nacos.config.namespace=a791fa0e-cef8-47ee-8f07-5ac5a63ea061
          5 nacos里創(chuàng)建命名空間gateway,然后在命名空間里創(chuàng)建文件glmall-gateway.yml
          spring:
              application:
                  name: glmall-gateway
          6 在項目里創(chuàng)建application.yml
          spring:
            cloud:
              gateway:
                routes:
                  - id: baidu_route
                    uri: http://www.baidu.com
                    predicates:
                      - Query=url,baidu

                  - id: test_route
                    uri: http://www.qq.com
                    predicates:
                      - Query=url,qq
          測試 localhost:8080?url=baidu # 跳到百度頁面
          測試 localhost:8080?url=baidu # 跳到qq頁面
          posted @ 2022-05-10 15:15 zzsuje 閱讀(194) | 評論 (0)編輯 收藏
           
               摘要: Nacos配置中心我們還可以用nacos作為配置中心。配置中心的意思是不在application.properties等文件中配置了,而是放到nacos配置中心公用,這樣無需每臺機(jī)器都改。11.1 引入配置中心依賴,放到common中Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHigh...  閱讀全文
          posted @ 2022-05-09 14:55 zzsuje 閱讀(168) | 評論 (0)編輯 收藏
          CALENDER
          <2022年5月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜


          Powered By: 博客園
          模板提供滬江博客

          主站蜘蛛池模板: 汤原县| 鄂伦春自治旗| 彭泽县| 华亭县| 兴国县| 娄底市| 阳原县| 柘荣县| 盐池县| 诏安县| 白河县| 乌兰浩特市| 兴业县| 石家庄市| 宜阳县| 扶沟县| 柳州市| 株洲县| 平武县| 饶平县| 逊克县| 青铜峡市| 瓮安县| 广丰县| 游戏| 望谟县| 曲阜市| 泰来县| 武乡县| 威远县| 柏乡县| 田东县| 莎车县| 武宁县| 资阳市| 江城| 武夷山市| 咸丰县| 北海市| 潮州市| 凯里市|