• SpringCloudAlibaba 微服务讲解(二)微服务环境搭建


    微服务环境搭建

    我们这次是使用的电商项目的商品、订单、用户为案例进行讲解

    2.1 案例准备

    2.1.1 技术选型

    • maven :3.3.9
    • 数据库:mysql
    • 持久层:SpringData JPA
    • SpringCloud Alibaba技术栈

    2.1.2 模块设计

    • springcloud-alibaba 父工程
    • shop-common 公共模块
    • shop-user 用户模块
    • shop-product 商品模块
    • shop-order 订单模块

    2.1.3 微服务调用

    在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为例来 演示微服务的调用:客户向订单微服务发起一下单的请求,在进行保存订单之前需要调用商品微服务 查询商品的信息
    我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者.

    2.2创建父工程

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
        </parent>
    
        <groupId>com.ityml</groupId>
        <artifactId>springcloud-alibaba</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
            <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    

    对应版本:

    https://www.cnblogs.com/onehm/p/13931711.html

    2.3创建基础模块

    2.3.1 shop-common模块

    1. 在pom中添加依赖

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <parent>
              <groupId>com.ityml</groupId>
              <artifactId>springcloud-alibaba</artifactId>
              <version>1.0-SNAPSHOT</version>
          </parent>
      
          <groupId>com.ityml</groupId>
          <artifactId>shop-common</artifactId>
          <version>1.0-SNAPSHOT</version>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-jpa</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.projectlombok</groupId>
                  <artifactId>lombok</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.alibaba</groupId>
                  <artifactId>fastjson</artifactId>
                  <version>1.2.79</version>
              </dependency>
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>5.1.49</version>
              </dependency>
      
          </dependencies>
      
      </project>
      
    2. 创建实体类

      /**
       * -----------------------------
       * PackageName: com.ityml.entity
       * ClassName:User
       * Description:用户
       *
       * @author:it-yml CreateTime:2022-03-22
       * -----------------------------
       */
      @Entity(name = "shop-user")
      @Data
      public class User {
          @Id
      
          @GeneratedValue(strategy = GenerationType.IDENTITY)
      
          private Integer uid;
      
          private String username;
      
          private String password;
      
          private String telephone;
      }
      
      /**
       * -----------------------------
       * PackageName: com.ityml.entity
       * ClassName:Product
       * Description:商品
       *
       * @author:it-yml CreateTime:2022-03-22
       * -----------------------------
       */
      @Entity(name = "shop-product")
      @Data
      public class Product {
      
          @Id
      
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private String pname;
          
          private Double price;
         
          private Integer stock;
      }
      
      /**
       * -----------------------------
       * PackageName: com.ityml.entity
       * ClassName:Order
       * Description:订单
       *
       * @author:it-yml CreateTime:2022-03-22
       * -----------------------------
       */
      @Entity(name = "shop-order")
      @Data
      public class Order {
          @Id
      
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Long oid;
      
          private Integer uid;
      
          private String username;
      }
      

    2.4 创建用户微服务

    步骤:

    1. 创建模块
    2. 创建SpringBoot主类
    3. 加入配置文件
    4. 创建必要的接口和实现类

    创建一个shop-user模块,然后进行下面操作

    1. 创建pom

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <parent>
              <groupId>com.ityml</groupId>
              <artifactId>springcloud-alibaba</artifactId>
              <version>1.0-SNAPSHOT</version>
          </parent>
      
          <groupId>com.ityml</groupId>
          <artifactId>shop-user</artifactId>
          <version>1.0-SNAPSHOT</version>
          <dependencies>
              <dependency>
                  <groupId>com.ityml</groupId>
                  <artifactId>shop-common</artifactId>
                  <version>1.0-SNAPSHOT</version>
              </dependency>
          </dependencies>
      
      
      </project>	
      
    2. 编写主类

      package com.ityml;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /**
       * -----------------------------
       * PackageName: com.ityml
       * ClassName:UserApplication
       * Description:启动类
       *
       * @author:it-yml CreateTime:2022-03-22
       * -----------------------------
       */
      @SpringBootApplication
      public class UserApplication {
          public static void main(String[] args) {
              SpringApplication.run(UserApplication.class,args);
          }
      }
      
      
    3. 创建配置文件

      server:
        port: 8080
      spring:
        application:
          name: service-user
        datasource:
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc://
          username:
          data-password:
      
        jpa:
          properties:
            hibernate:
              hbm2ddl:
                auto: update
              dialect: org.hibernate.dialect.MySQL.5InnoDDialect
      
      

    2.5 创建商品微服务

    1. 创建一个名为shop-product的模块,并添加springboot依赖

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <parent>
              <groupId>com.ityml</groupId>
              <artifactId>springcloud-alibaba</artifactId>
              <version>1.0-SNAPSHOT</version>
          </parent>
      
          <groupId>com.ityml</groupId>
          <artifactId>shop-product</artifactId>
          <version>1.0-SNAPSHOT</version>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.ityml</groupId>
                  <artifactId>shop-common</artifactId>
                  <version>1.0-SNAPSHOT</version>
              </dependency>
          </dependencies>
          
      </project>
      
    2. 创建工程主类

      package com.ityml;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /**
       * -----------------------------
       * PackageName: com.ityml
       * ClassName:ProductApplication
       * Description:商品主类
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      @SpringBootApplication
      public class ProductApplication {
          public static void main(String[] args) {
              SpringApplication.run(ProductApplication.class,args);
          }
      }
      
    3. 创建配置文件application.yml

      server:
        port: 8080
      spring:
        application:
          name: service-product
        datasource:
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc://
          username:
          data-password:
      
        jpa:
          properties:
            hibernate:
              hbm2ddl:
                auto: update
              dialect: org.hibernate.dialect.MySQL.5InnoDDialect
      
    4. 创建ProductDao接口

      package com.ityml.dao;
      
      import com.ityml.entity.Product;
      import org.springframework.data.jpa.repository.JpaRepository;
      
      /**
       * -----------------------------
       * PackageName: com.ityml.dao
       * ClassName:ProductDao
       * Description:
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      public interface ProductDao extends JpaRepository<Product,Integer> {
      }
      
    5. 创建ProductService接口和实现类

      package com.ityml.service;
      
      import com.ityml.entity.Product;
      
      /**
       * -----------------------------
       * PackageName: com.ityml.service
       * ClassName:ProductService
       * Description:
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      public interface ProductService {
          Product findById(Integer pid);
      }
      
      package com.ityml.impl;
      
      import com.ityml.dao.ProductDao;
      import com.ityml.entity.Product;
      import com.ityml.service.ProductService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      
      /**
       * -----------------------------
       * PackageName: com.ityml.impl
       * ClassName:ProductServiceImpl
       * Description:实现类
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      @Service
      public class ProductServiceImpl implements ProductService {
          @Autowired
          private ProductDao productDao;
          @Override
          public Product findById(Integer pid) {
              return productDao.findById(pid).get();
          }
      }
      
      
    6. 创建Controller

      package com.ityml.controller;
      
      import com.ityml.entity.Product;
      import com.ityml.service.ProductService;
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      /**
       * -----------------------------
       * PackageName: com.ityml.controller
       * ClassName:ProductController
       * Description:主类
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      @RestController
      @Slf4j
      public class  ProductController {
      
          @Autowired
          private ProductService productService;
      
          @GetMapping("/product/{pid}")
          public Product product(@PathVariable("pid") Integer pid){
              Product product = productService.findById(pid);
              return product;
          }
      
      
      }
      
    7. 启动工程

    2.6 创建订单微服务

    1. 创建一个名为shop-order的模块,并添加springboot依赖

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <parent>
              <groupId>com.ityml</groupId>
              <artifactId>springcloud-alibaba</artifactId>
              <version>1.0-SNAPSHOT</version>
          </parent>
      
          <groupId>com.ityml</groupId>
          <artifactId>shop-order</artifactId>
          <version>1.0-SNAPSHOT</version>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.ityml</groupId>
                  <artifactId>shop-common</artifactId>
                  <version>1.0-SNAPSHOT</version>
              </dependency>
          </dependencies>
      </project>
      
    2. 创建工程主类

      package com.ityml;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /**
       * -----------------------------
       * PackageName: com.ityml
       * ClassName:OrderApplication
       * Description:主类
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      @SpringBootApplication
      public class OrderApplication {
          public static void main(String[] args) {
              SpringApplication.run(OrderApplication.class,args);
          }
      }
      
    3. 创建配置文件

      server:
        port: 8080
      spring:
        application:
          name: service-order
        datasource:
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc://
          username:
          data-password:
      
        jpa:
          properties:
            hibernate:
              hbm2ddl:
                auto: update
              dialect: org.hibernate.dialect.MySQL.5InnoDDialect
      
    4. 创建OrderDao接口

      package com.ityml.dao;
      
      import com.ityml.entity.Order;
      import org.springframework.data.jpa.repository.JpaRepository;
      
      /**
       * -----------------------------
       * PackageName: com.ityml.dao
       * ClassName:ProductDao
       * Description:
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      public interface OrderDao extends JpaRepository<Order,Integer> {
      }
      
    5. 创建OrderService接口和实现类

      package com.ityml.service;
      
      import com.ityml.entity.Order;
      
      /**
       * -----------------------------
       * PackageName: com.ityml.service
       * ClassName:ProductService
       * Description:
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      public interface OrderService {
          void save(Order order);
      }
      
    6. 创建RestTemplate

      package com.ityml;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.context.annotation.Bean;
      import org.springframework.web.client.RestTemplate;
      
      /**
       * -----------------------------
       * PackageName: com.ityml
       * ClassName:OrderApplication
       * Description:主类
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      @SpringBootApplication
      public class OrderApplication {
          public static void main(String[] args) {
              SpringApplication.run(OrderApplication.class,args);
          }
          @Bean
          public RestTemplate getRestTemplate(){
              return new RestTemplate();
          }
      }
      
    7. 创建Controller

      package com.ityml.controller;
      
      import com.ityml.entity.Order;
      import com.ityml.entity.Product;
      import com.ityml.service.OrderService;
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RestController;
      import org.springframework.web.client.RestTemplate;
      
      /**
       * -----------------------------
       * PackageName: com.ityml.controller
       * ClassName:OrderController
       * Description:主类
       *
       * @author:it-yml CreateTime:2022-03-23
       * -----------------------------
       */
      @RestController
      @Slf4j
      public class OrderController {
      
          @Autowired
          private OrderService orderService;
      
          @Autowired
          private RestTemplate restTemplate;
      
          /**
           * 准备买一件商品
           * @param pid
           */
          @GetMapping("/order/prod/{pid}")
          public void Order(@PathVariable("pid") Integer pid) {
              Product product = restTemplate.getForObject("http:/localhost:8081/product" + pid, Product.class);
              Order order = new Order();
              orderService.save(order);
          }
      
      }
      
    8. 启动工程,通过浏览器访问服务进行测试

  • 相关阅读:
    WPF使用Iconfont字符串的操作方法
    Kubernetes容器状态探测的艺术
    将时间序列转成图像——小波变换方法 Matlab实现
    【已解决】matrix contains invalid numeric entries,记录bug修改
    数据分析-Pandas如何画自相关图
    开发板通过网线连接电脑而上网
    cdh3.6.2集成flink1.12.0
    固定资产模块事务代码
    宝塔安装python和openssl
    含文档+PPT+源码等]精品基于Uniapp实现的移动端的医生寻访平台的设计与实现[包运行成功]
  • 原文地址:https://www.cnblogs.com/ityml/p/16047373.html