<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    2022年5月6日

    網(wǎng)關(guān)
    發(fā)送請求需要知道商品服務(wù)的地址,如果商品服務(wù)器有100服務(wù)器,1號掉線后,
    還得改,所以需要網(wǎng)關(guān)動態(tài)地管理,他能從注冊中心中實(shí)時地感知某個服務(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 閱讀(183) | 評論 (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 閱讀(158) | 評論 (0)編輯 收藏
     
    10.0 Feign與注冊中心
    聲明式遠(yuǎn)程調(diào)用
    feign是一個聲明式的HTTP客戶端,他的目的就是讓遠(yuǎn)程調(diào)用更加簡單。
    給遠(yuǎn)程服務(wù)發(fā)的是HTTP請求。
    會員服務(wù)(member)調(diào)優(yōu)惠券(coupon)服務(wù)
    會員服務(wù)通過openFeign先去注冊中心找優(yōu)惠券服務(wù)
    10.1 引入 openfeign 依賴
    會員服務(wù)想要遠(yuǎn)程調(diào)用優(yōu)惠券服務(wù),只需要給會員服務(wù)里引入openfeign依賴,他就有了遠(yuǎn)程調(diào)用其他服務(wù)的能力。
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    10.2 在coupon服務(wù)(被調(diào)用服務(wù))中修改如下的內(nèi)容
    @RequestMapping("coupon/coupon")
    public class CouponController {
        @Autowired
        private CouponService couponService;
        @RequestMapping("/member/list")
        public R membercoupons(){    //全系統(tǒng)的所有返回都返回R
            
    // 應(yīng)該去數(shù)據(jù)庫查用戶對于的優(yōu)惠券,但這個我們簡化了,不去數(shù)據(jù)庫查了,構(gòu)造了一個優(yōu)惠券給他返回
            CouponEntity couponEntity = new CouponEntity();
            couponEntity.setCouponName("滿100減10");//優(yōu)惠券的名字
            return R.ok().put("coupons",Arrays.asList(couponEntity));
        }
    10.3 這樣我們準(zhǔn)備好了優(yōu)惠券的調(diào)用內(nèi)容
    在member的配置類上加注解@EnableFeignClients(basePackages="com.yxj.gulimall.member.feign"),
    告訴spring這里面是一個遠(yuǎn)程調(diào)用客戶端,member要調(diào)用的接口
    package com.yxj.gulimall.member;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    @SpringBootApplication
    @MapperScan("com.yxj.gulimall.member.dao")
    @EnableDiscoveryClient
    @EnableFeignClients(basePackages="com.yxj.gulimall.member.feign")
    public class GulimallMemberApplication {
        public static void main(String[] args) {
            SpringApplication.run(GulimallMemberApplication.class, args);
        }
    }

    10.4
     那么要調(diào)用什么東西呢?就是我
    們剛才寫的優(yōu)惠券的功能,
    復(fù)制函數(shù)部分,在member的com.yxj.gulimall.member.feign包下新建類:
    package com.yxj.gulimall.member.feign;
    import com.yxj.common.utils.R;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    @FeignClient("gulimall-coupon") //告訴spring cloud這個接口是一個遠(yuǎn)程客戶端,要調(diào)用coupon服務(wù),再去調(diào)用coupon服務(wù)/coupon/coupon/member/list對應(yīng)的方法
    public interface CouponFeignService {
        @RequestMapping("/coupon/coupon/member/list") 
        public R membercoupons();//得到一個R對象
    }
    10.5 然后我們在member的控制層寫一個測試請求
    @RestController
    @RequestMapping("member/member")
    public class MemberController {
        @Autowired
        private MemberService memberService;
        @Autowired
        CouponFeignService couponFeignService;
        @RequestMapping("/coupons")
        public R test(){
            MemberEntity memberEntity = new MemberEntity();
            memberEntity.setNickname("張三");
            R membercoupons = couponFeignService.membercoupons(); //假設(shè)張三去數(shù)據(jù)庫查了后返回了張三的優(yōu)惠券信息
            
    // 打印會員和優(yōu)惠券信息
            return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
        }
     
    10.6 重新啟動服務(wù)
    http://localhost:8000/member/member/coupons
    {"msg":"success","code":0,"coupons":[{"id":null,"couponType":null,"couponImg":null,"couponName":"滿100減10","num":null,"amount":null,"perLimit":null,"minPoint":null,"startTime":null,"endTime":null,"useType":null,"note":null,"publishCount":null,"useCount":null,"receiveCount":null,"enableStartTime":null,"enableEndTime":null,"code":null,"memberLevel":null,"publish":null}],"member":{"id":null,"levelId":null,"username":null,"password":null,"nickname":"張三","mobile":null,"email":null,"header":null,"gender":null,"birth":null,"city":null,"job":null,"sign":null,"sourceType":null,"integration":null,"growth":null,"status":null,"createTime":null}}

    10.7 上面內(nèi)容很重要,我們停留5分鐘體會一下
    coupon里的R.ok()是什么 # coupon里的控制層就是new了個couponEntity然后放到hashmap(R)里而已。
    public class R extends HashMap<String, Object> {
        public static R ok() {
            return new R();
        }
        public R put(String key, Object value) {
            super.put(key, value);
            return this;
        }
    }
    posted @ 2022-05-06 14:45 zzsuje 閱讀(112) | 評論 (0)編輯 收藏
     
         摘要:   閱讀全文
    posted @ 2022-05-06 11:35 zzsuje 閱讀(96) | 評論 (0)編輯 收藏
     
         摘要: 1、拉取鏡像 1 docker pull nacos/nacos-server ...  閱讀全文
    posted @ 2022-05-06 09:10 zzsuje 閱讀(113) | 評論 (0)編輯 收藏
    CALENDER
    <2022年5月>
    24252627282930
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜


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

    主站蜘蛛池模板: 久久久久亚洲av无码尤物| 亚洲成a人片在线观看播放| 三上悠亚亚洲一区高清| 亚洲精品GV天堂无码男同| 真实乱视频国产免费观看| 亚洲中文字幕无码中文| 四色在线精品免费观看| 国产亚洲av片在线观看16女人| 国产精品免费在线播放| 亚洲色欲久久久综合网东京热| 手机看片国产免费永久| 亚洲阿v天堂在线| 人与禽交免费网站视频| 国产成人亚洲精品| 国产免费怕怕免费视频观看| 永久免费无码网站在线观看个 | 精品久久久久国产免费| 亚洲精品无码专区在线| 亚洲成?Ⅴ人在线观看无码| a级毛片在线视频免费观看| 亚洲专区先锋影音| 在线a毛片免费视频观看| fc2成年免费共享视频18| 亚洲av午夜成人片精品网站| 亚洲天堂免费在线| 免费观看四虎精品成人| 亚洲ⅴ国产v天堂a无码二区| 成人片黄网站A毛片免费| 七次郎成人免费线路视频| 成人免费视频一区| www成人免费观看网站| 久久亚洲AV成人无码电影| 女人18毛片水真多免费看| 国产高潮流白浆喷水免费A片 | 亚洲久本草在线中文字幕| 在线视频免费观看www动漫 | **真实毛片免费观看| 白白色免费在线视频| 亚洲高清不卡视频| 亚洲片国产一区一级在线观看| **一级毛片免费完整视|