隨筆:13 文章:7 評論:0 引用:0
          BlogJava 首頁 發新隨筆
          發新文章 聯系 聚合管理

          2022年5月10日

          網關
          發送請求需要知道商品服務的地址,如果商品服務器有100服務器,1號掉線后,
          還得改,所以需要網關動態地管理,他能從注冊中心中實時地感知某個服務上
          線還是下線。
          請求也要加上詢問權限,看用戶有沒有權限訪問這個請求,也需要網關。
          所以我們使用spring cloud的gateway組件做網關功能。
          網關是請求瀏覽的入口,常用功能包括路由轉發權限校驗限流控制等。springcloud gateway取代了zuul網關。
          三大核心概念:
          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.
          發一個請求給網關,網關要將請求路由到指定的服務。
          路由有id,
          目的地uri,
          斷言的集合,
          匹配了斷言就能到達指定位置,
          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里的斷言函數,匹配請求里的任何信息,包括請求頭等
          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.
          過濾器請求和響應都可以被修改。
          客戶端發請求給服務端。中間有網關。先交給映射器,如果能處理就交給handler
          處理,然后交給一系列filer,然后給指定的服務,再返回回來給客戶端。
          12.1 創建模塊gulimall-gateway
          <dependency>
                      <groupId>com.zyn.glmall</groupId>
                      <artifactId>glmall-common</artifactId>
                      <version>0.0.1-SNAPSHOT</version>
          </dependency>
          1 在pom.xml引入
          版本環境需保持一致
          <spring-boot.version>2.1.8.RELEASE</spring-boot.version>
          <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
          2 開啟注冊服務發現@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里創建命名空間gateway,然后在命名空間里創建文件glmall-gateway.yml
          spring:
              application:
                  name: glmall-gateway
          6 在項目里創建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 閱讀(182) | 評論 (0)編輯 收藏

          2022年5月9日

               摘要: Nacos配置中心我們還可以用nacos作為配置中心。配置中心的意思是不在application.properties等文件中配置了,而是放到nacos配置中心公用,這樣無需每臺機器都改。11.1 引入配置中心依賴,放到common中Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHigh...  閱讀全文
          posted @ 2022-05-09 14:55 zzsuje 閱讀(157) | 評論 (0)編輯 收藏

          2022年5月6日

          10.0 Feign與注冊中心
          聲明式遠程調用
          feign是一個聲明式的HTTP客戶端,他的目的就是讓遠程調用更加簡單。
          給遠程服務發的是HTTP請求。
          會員服務(member)調優惠券(coupon)服務
          會員服務通過openFeign先去注冊中心找優惠券服務
          10.1 引入 openfeign 依賴
          會員服務想要遠程調用優惠券服務,只需要給會員服務里引入openfeign依賴,他就有了遠程調用其他服務的能力。
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-openfeign</artifactId>
          </dependency>

          10.2 在coupon服務(被調用服務)中修改如下的內容
          @RequestMapping("coupon/coupon")
          public class CouponController {
              @Autowired
              private CouponService couponService;
              @RequestMapping("/member/list")
              public R membercoupons(){    //全系統的所有返回都返回R
                  
          // 應該去數據庫查用戶對于的優惠券,但這個我們簡化了,不去數據庫查了,構造了一個優惠券給他返回
                  CouponEntity couponEntity = new CouponEntity();
                  couponEntity.setCouponName("滿100減10");//優惠券的名字
                  return R.ok().put("coupons",Arrays.asList(couponEntity));
              }
          10.3 這樣我們準備好了優惠券的調用內容
          在member的配置類上加注解@EnableFeignClients(basePackages="com.yxj.gulimall.member.feign"),
          告訴spring這里面是一個遠程調用客戶端,member要調用的接口
          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
           那么要調用什么東西呢?就是我
          們剛才寫的優惠券的功能,
          復制函數部分,在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這個接口是一個遠程客戶端,要調用coupon服務,再去調用coupon服務/coupon/coupon/member/list對應的方法
          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(); //假設張三去數據庫查了后返回了張三的優惠券信息
                  
          // 打印會員和優惠券信息
                  return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
              }
           
          10.6 重新啟動服務
          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 上面內容很重要,我們停留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 閱讀(109) | 評論 (0)編輯 收藏
           
               摘要:   閱讀全文
          posted @ 2022-05-06 11:35 zzsuje 閱讀(95) | 評論 (0)編輯 收藏
           
               摘要: 1、拉取鏡像 1 docker pull nacos/nacos-server ...  閱讀全文
          posted @ 2022-05-06 09:10 zzsuje 閱讀(112) | 評論 (0)編輯 收藏

          2022年5月5日

          逆向工程搭建
          7.1 product
          git clone https://gitee.com/renrenio/renren-generator.git
          下載到桌面后,同樣把里面的.git文件刪除,然后移動到我們IDEA項目目錄中,同樣配置好pom.xml(root)
          在common項目中增加module
          <modules>
          <module>gulimall-coupon</module>
          <module>gulimall-member</module>
          <module>gulimall-order</module>
          <module>gulimall-product</module>
          <module>gulimall-ware</module>
          <module>renren-fast</module>
          <module>renren-generator</module>
          </modules>
          修改renren-generator的application.yml
          url: jdbc:mysql://192.168.1.103:3306/gulimall-pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          username: root
          password: root
          修改generator.properties
          mainPath=com.yxj # 主目錄
          package=com.yxj.gulimall # 包名
          moduleName=product   # 模塊名
          author=yxj  # 作者
          email=xxx@qq.com  # email
          tablePrefix=pms_   # 我們的pms數據庫中的表的前綴都有pms,
          如果寫了表前綴,每一張表對于的javaBean就不會添加前綴了
          運行RenrenApplication。如果啟動不成功,修改application中是port為80。訪問http://localhost:80
          然后點擊全部,點擊生成代碼。下載了壓縮包
          解壓壓縮包,把main放到gulimall-product的同級目錄下。
          在common項目的pom.xml(我們把每個微服務里公共的類和依賴放到common里。)中添加
          <!-- mybatisPLUS-->
          <dependency>
              <groupId>com.baomidou</groupId>
              <artifactId>mybatis-plus-boot-starter</artifactId>
              <version>3.3.2</version>
          </dependency>
          <!--簡化實體類,用@Data代替getset方法-->
          <dependency>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>1.18.8</version>
          </dependency>
          <!-- httpcomponent包https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
          <dependency>
              <groupId>org.apache.httpcomponents</groupId>
              <artifactId>httpcore</artifactId>
              <version>4.4.13</version>
          </dependency>
          <dependency>
              <groupId>commons-lang</groupId>
              <artifactId>commons-lang</artifactId>
              <version>2.6</version>
          </dependency>
          然后在product項目中的pom.xml中加入下面內容
          <dependency>
              <groupId>com.atguigu.gulimall</groupId>
              <artifactId>gulimall-common</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
          復制
          renren-fast----utils包下的Query和PageUtils、R、Constant復制到common項目的java/com.yxj.common.utils下
          把@RequiresPermissions這些注解掉,因為是shiro的
          復制renren-fast中的xss包粘貼到common的java/com.yxj.common目錄下。
          還復制了exception文件夾,對應的位置關系自己觀察一下就行
          注釋掉product項目下類中的//import org.apache.shiro.authz.annotation.RequiresPermissions;,他是shiro的東西
          注釋renren-generator\src\main\resources\template/Controller中所有的
          # @RequiresPermissions。
          # import org.apache.shiro.authz.annotation.RequiresPermissions;
          總之什么報錯就去renren-fast里面找。
          測試
          測試與整合商品服務里的mybatisplus
          在common的pom.xml中導入
          <!-- 數據庫驅動 https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>8.0.17</version>
          </dependency>
          <!--tomcat里一般都帶-->
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>servlet-api</artifactId>
              <version>2.5</version>
              <scope>provided</scope>  # Tomcat有帶,所以provided
          </dependency>
          刪掉common里xss/xssfiler和XssHttpServletRequestWrapper
          在product項目的resources目錄下新建application.yml
          spring:
            datasource:
              driver-class-name: com.mysql.cj.jdbc.Driver
              url: jdbc:mysql://192.168.1.103:3306/gulimall_pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
              username: root
              password: root
          # MapperScan
          # sql映射文件位置
          mybatis-plus:
            mapper-locations: classpath:/mapper/**/*.xml
            global-config:
              db-config:
                id-type: auto
          然后在主啟動類上加上注解@MapperScan()
          @MapperScan("com.yxj.gulimall.product.dao")
          @SpringBootApplication
          public class gulimallProductApplication {
              public static void main(String[] args) {
                  SpringApplication.run(gulimallProductApplication.class, args);
              }
          }
          然后去測試,先通過下面方法給數據庫添加內容
          @SpringBootTest
          class gulimallProductApplicationTests {
              @Autowired
              BrandService brandService;
              @Test
              void contextLoads() {
                  BrandEntity brandEntity = new BrandEntity();
                  brandEntity.setDescript("hello");
                  brandEntity.setName("華為");
                  brandService.save(brandEntity);
                  System.out.println("保存成功");
              }
          }
          3.12.2 coupon
          重新打開generator逆向工程,修改generator.properties
          # 主目錄 
          mainPath=com.yxj
          package=com.yxj.gulimall
          moduleName=coupon
          autho=yxj
          email=xxx@qq.com
          tablePrefix=sms_
          修改yml數據庫信息
          spring:
            datasource:
              username: root
              password: root
              url: jdbc:mysql://192.168.1.103:3306/gulimall_sms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          mybatis-plus:
            mapper-locations: classpath:/mapper/**/*.xml
            global-config:
              db-config:
                id-type: auto
                logic-delete-value: 1
                logic-not-delete-value: 0
          server:
            port: 7000
          啟動生成RenrenApplication.java,運行后去瀏覽器80端口查看,同樣讓他一
          頁全顯示后選擇全部后生成。生成后解壓復制到coupon項目對應目錄下。
          讓coupon也依賴于common,修改pom.xml
          <dependency>
              <groupId>com.atguigu.gulimall</groupId>
              <artifactId>gulimall-common</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
          resources下src包先刪除
          添加application.yml
          spring:
            datasource:
              username: root
              password: root
              url: jdbc:mysql://192.168.1.103:3306/gulimall_sms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
              driver-class-name: com.mysql.cj.jdbc.Driver
          mybatis-plus:
            mapper-locations: classpath:/mapper/**/*.xml
            global-config:
              db-config:
                id-type: auto
                logic-delete-value: 1
                logic-not-delete-value: 0
          運行gulimallCouponApplication.java
          http://localhost:8080/coupon/coupon/list
          {"msg":"success","code":0,"page":{"totalCount":0,"pageSize":10,"totalPage":0,"currPage":1,"list":[]}}
          3.12.3 member
          重新使用代碼生成器生成ums
          模仿上面修改下面兩個配置
          代碼生成器里:
          url: jdbc:mysql://192.168.1.103:3306/gulimall_sms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          mainPath=com.yxj 
          package=com.yxj.gulimall
          moduleName=member
          author=yxj
          email=xxx@qq.com
          tablePrefix=ums_
          重啟RenrenApplication.java,然后同樣去瀏覽器獲取壓縮包解壓到對應member項目目錄
          member也導入依賴
          <dependency>
              <groupId>com.atguigu.gulimall</groupId>
              <artifactId>gulimall-common</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
          同樣新建application.yml
          spring:
            datasource:
              username: root
              password: root
              url: jdbc:mysql://192.168.1.103:3306/gulimall-ums?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
              driver-class-name: com.mysql.cj.jdbc.Driver
          mybatis-plus:
            mapper-locations: classpath:/mapper/**/*.xml
            global-config:
              db-config:
                id-type: auto
                logic-delete-value: 1
                logic-not-delete-value: 0
          server:
            port: 8000
          order端口是9000,product是10000,ware是11000。
          以后比如order系統要復制多份,他的端口計算9001、9002。。。
          重啟web后,http://localhost:8000/member/growthchangehistory/list
          測試成功:{"msg":"success","code":0,"page":{"totalCount":0,"pageSize":10,"totalPage":0,"currPage":1,"list":[]}}
          3.12.4 order
          修改代碼生成器
          jdbc:mysql://192.168.1.103:3306/gulimall_oms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          #代碼生成器,配置信息
          mainPath=com.yxj
          package=com.yxj.gulimall
          moduleName=order
          author=yxj
          email=xxx@qq.com
          tablePrefix=oms_
          運行RenrenApplication.java重新生成后去下載解壓放置。
          application.yml
          spring:
            datasource:
              username: root
              password: root
              url: jdbc:mysql://192.168.1.103:3306/gulimall_oms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
              driver-class-name: com.mysql.cj.jdbc.Driver
          mybatis-plus:
            mapper-locations: classpath:/mapper/**/*.xml
            global-config:
              db-config:
                id-type: auto
                logic-delete-value: 1
                logic-not-delete-value: 0
                
          server:
            port: 9000
          在pom.xml添加
          <dependency>
              <groupId>com.atguigu.gulimall</groupId>
              <artifactId>gulimall-common</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
          啟動gulimallOrderApplication.java
          http://localhost:9000/order/order/list
          {"msg":"success","code":0,"page":{"totalCount":0,"pageSize":10,"totalPage":0,"currPage":1,"list":[]}}
          3.12.5 ware
          修改代碼生成器
          jdbc:mysql://192.168.1.103:3306/gulimall_wms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          #代碼生成器,配置信息
          mainPath=com.yxj
          package=com.yxj.gulimall
          moduleName=ware
          author=yxj
          email=xxx@qq.com
          tablePrefix=wms_
          運行RenrenApplication.java重新生成后去下載解壓放置。
          application.yml
          spring:
            datasource:
              username: root
              password: root
              url: jdbc:mysql://192.168.1.103:3306/gulimall_wms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
              driver-class-name: com.mysql.cj.jdbc.Driver
          mybatis-plus:
            mapper-locations: classpath:/mapper/**/*.xml
            global-config:
              db-config:
                id-type: auto
                logic-delete-value: 1
                logic-not-delete-value: 0
                
          server:
            port: 11000
          在pom.xml添加
          <dependency>
              <groupId>com.atguigu.gulimall</groupId>
              <artifactId>gulimall-common</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
          啟動gulimallWareApplication.java
          http://localhost:11000/ware/wareinfo/list
          {"msg":"success","code":0,"page":{"totalCount":0,"pageSize":10,"totalPage":0,"currPage":1,"list":[]}}
          posted @ 2022-05-05 14:10 zzsuje 閱讀(127) | 評論 (0)編輯 收藏

          2022年4月26日

          6.1 git clone 人人項目

          在碼云上搜索人人開源,我們使用renren-fast,renren-fast-vue項目。
          git clone https://gitee.com/renrenio/renren-fast.git

          git clone https://gitee.com/renrenio/renren-fast-vue.git

          下載到了桌面,我們把renren-fast移動到我們的項目文件夾(刪掉.git文件),而renren-fast-vue是用VSCode打開的(后面再弄)

          6.2修改配置文件 啟動項目
              然后修改項目里renren-fast中的application.yml
              修改application-dev.yml中的數庫庫的
              url: jdbc:mysql://192.168.1.103:3306/gulimall_admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
              username: root password: root
              然后執行java下的RenrenApplication
              瀏覽器輸入http://localhost:8080/renren-fast/
              得到{“msg”:“invalid token”,“code”:401}就代表無誤

          6.3用VSCode打開renren-fast-vue
              6.3.1 安裝node:
                      版本為v10.16.3
                      設置node鏡像倉庫
                      npm config set registry http://registry.npm.taobao.org/  # 設置node倉庫。提高下載速度

               6.3.2  在終端中輸入命令:npm install,安裝項目所需依賴。
               6.3.3  安裝完成后,輸入命令:npm run dev,運行項目。
                        瀏覽器輸入localhost:8001 就可以看到內容了,登錄賬號admin 密碼admin
          posted @ 2022-04-26 16:49 zzsuje 閱讀(149) | 評論 (0)編輯 收藏
           
               摘要: 5.1sql語句Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->gulimall-oms.sqldrop table if exists oms_order;drop table if&nbs...  閱讀全文
          posted @ 2022-04-26 15:39 zzsuje 閱讀(68) | 評論 (0)編輯 收藏
           
               摘要: 4.1從gitee上導入項目 Idea配置Git4.2新建項目成功4.3創建項目微服務商品服務 倉儲服務 訂單服務 優惠券服務 用戶服務共同:1)web,openFeign2)  每一個服務,包名:com.zyn.glmall.XXX(product,order,ware,coupon,member)3)  模塊名 glmall-coupon4.3.1拷貝一個pom文件給聚合項...  閱讀全文
          posted @ 2022-04-26 11:17 zzsuje 閱讀(68) | 評論 (0)編輯 收藏

          2022年4月25日

               摘要: 3.1安裝Git3.1.1設置自己的git信息進入右鍵 git bashgit config --global user.name "Firstname Lastname" (此處name可修改也不是用于登錄github的登錄名)git config --global user.email "your_email@yourema...  閱讀全文
          posted @ 2022-04-25 17:22 zzsuje 閱讀(96) | 評論 (0)編輯 收藏
          CALENDER
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜


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

          主站蜘蛛池模板: 瑞安市| 容城县| 沙洋县| 洞头县| 锡林浩特市| 海城市| 华阴市| 三台县| 远安县| 新绛县| 九江县| 横山县| 台湾省| 天镇县| 栾川县| 竹溪县| 荃湾区| 中宁县| 财经| 离岛区| 梁河县| 定结县| 余庆县| 崇左市| 淮阳县| 化州市| 东台市| 丰宁| 龙里县| 昌邑市| 长宁区| 韩城市| 汝州市| 霍城县| 湘乡市| 外汇| 铁岭市| 衡东县| 彩票| 新巴尔虎右旗| 孝义市|