zoukankan      html  css  js  c++  java
  • gulimall分布式商城(二)

      书接上回,前面我们利用逆向工程生成了基本的服务module,能进行正常的CRUD进行测试。具体可回头参考gulimall分布式商城的前世今生(一)。在这里推荐使用postman进行测试,非常方便。

      说明:本文旨在分享学习经历,有错误的地方欢迎大家指出,本人会虚心接受。原创不易,不喜勿喷。

    4、分布式组件之Spring Cloud Alibaba   

      前面第三部分可以看做一个springboot的简单应用。好了,我们闲言少叙,正式开始我们的分布式之旅:Spring Cloud Alibaba

      4.1、Spring Cloud Alibaba简介

      自行百度,略。。。

      4.2、nacos注册中心 

        1) 下载nacos服务器

        2) 在公共模块gulimall-common中引入spring-cloud-Alibaba依赖

    <!-- alibaba版本管理-->
    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.1.0.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
     </dependencyManagement>

        3) 在公共模块中添加nacos-discovery-spring-boot-starter依赖

    <!--spring-cloud-alibaba-nacos作注册中心-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>

        4) 在application.yml中配置nacos server地址:

    #nacos中中注册的服务名称
    application:
    name: gulimall-product

    #nacos server 本机地址
    cloud:
    nacos:
    discovery:
    server-addr: 127.0.0.1:8848

        5) 在application.yml中配置服务名字: application-name: gulimall-product

        6) 在对应启动类上添加: @EnableDiscoveryClient注解开启服务的注册和发现

    @EnableDiscoveryClient
    @SpringBootApplication
    public class GulimallProductApplication {
    
        public static void main(String[] args) {
    
            SpringApplication.run(GulimallProductApplication.class, args);
        }
    
    }

      4.3、openfeign远程调用

        eg:会员要调用优惠券的服务
        1) 引入相关依赖

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

        2) 在member中编写一个接口,告诉SpringCloud 这个接口需要调用远程服务声明接口的每一个方法都是调用哪个远程服务的哪个请求

    @FeignClient("gulimall-coupon")
    public interface CouponFeignService{
         //接口中写方法的完整签名,即不带方法体
         @RequestMapping(/coupon/coupon)
         publi R listCoupon(...);
    }

        3)开启注解: @EnableFeignClients("com.atguigu.gulimall.product.feign")

      4.4、nacos配置中心

        1) 引入依赖(在common模块中)

    <!--spring-cloud-alibaba-nacos作注册中心-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>

        2) 在应用的src/main/resource中添加bootstrap.properties

    spring.application.name=gulimall-product
    
    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    spring.cloud.nacos.config.namespace=cea8f0e3-ba06-4ef7-8556-77bc61104641

        3) 使用@value将对应的配置注入到AttrController的username和age字段,并添加@RefreshScope注解动态刷新功能

        4) 在application.properties中配置用户名,密码.在controller中编写测试方法

    /**
    *    测试从application.properties配置文件中取值 @Value("${product.name}")
    */
    @RefreshScope
    @RestController
    @RequestMapping("product/attr")
    public class AttrController {
    
        @Value("${product.name}")
        private String name;
        @Value("${product.age}")
        private int age;
    
        @RequestMapping("/test")
        public R test(){
            return R.ok().put("name",name).put("age",age);
        }
    } 

      4.5、gateway网关

        1) 新建module
        2) 添加gulimall-common依赖
        3) 在启动类上添加@EnableDiscoveryClient注解开启服务的注册和发现
        4) 在application.properties中配置   

    #配置端口号
    server.port=88
    #nacos的注册中心地址:4f6d-89cc-9af7c7667fc2
    spring.cloud.nacos.discovery.server-addr = 127.0.0.1:8848
    #服务的名称:
    #spring.application.name = gulimall-gateway

        5)在bootstrap.properties中配置:

    #配置服务名称:
    spring.application.name=gulimall-gateway
    #nacos配置中心地址:
    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    #网关的命名空间
    spring.cloud.nacos.config.namespace = 1a8ded77-03af-4f6d-89cc-9af7c7667fc2

        6) 在启动类的@SpringBootApplication注解上加上(excute=${DataSourceAutoConfiguration.class}):

    @EnableDiscoveryClient
    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    public class GulimallGatewayApplication {
        public static void main(String[] args) {
            SpringApplication.run(GulimallGatewayApplication.class, args);
        }
    }

        7)创建application.yml,在其中配置:

    spring:
      cloud:
        gateway:
          routes:
            - id: taobao_route
              uri: https://www.taobao.com
              predicates:
                - Query=url,taobao

        8) 网关创建及测试通过

      此处404是因为我们测试的url为localhost:88/hello?url=taobao。淘宝根本没有/hello这个api。

      小结:我们现在初步了解了Spring Cloud Alibaba分布式部分组件,后续业务开发中会充分利用。后续前端我不准备写了,大家自己去撸视频吧。下节我们正式进入业务开发之商品管理。麻雀虽小,五脏俱全。莫要小看电商系统,还是值得你去撸一把的。

  • 相关阅读:
    httpcontext in asp.net unit test
    initialize or clean up your unittest within .net unit test
    Load a script file in sencha, supports both asynchronous and synchronous approaches
    classes system in sencha touch
    ASP.NET MVC got 405 error on HTTP DELETE request
    how to run demo city bars using sencha architect
    sencha touch mvc
    sencha touch json store
    sencha touch jsonp
    51Nod 1344:走格子(贪心)
  • 原文地址:https://www.cnblogs.com/gaoyangsixue/p/13274409.html
Copyright © 2011-2022 走看看