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

虚幻大学 xuhss 420℃ 0评论

Python微信订餐小程序课程视频

https://edu.csdn.net/course/detail/36074

Python实战量化交易理财系统

https://edu.csdn.net/course/detail/35475

微服务环境搭建

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

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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.3.RELEASEversion>
    parent>

    <groupId>com.itymlgroupId>
    <artifactId>springcloud-alibabaartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>pompackaging>
    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <spring-cloud.version>Greenwich.RELEASEspring-cloud.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASEspring-cloud-alibaba.version>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
project>

对应版本:

https://blog.csdn.net/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.0modelVersion>

    <parent>
        <groupId>com.itymlgroupId>
        <artifactId>springcloud-alibabaartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <groupId>com.itymlgroupId>
    <artifactId>shop-commonartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.79version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.49version>
        dependency>

    dependencies>

project>
  1. 创建实体类
/**
 * -----------------------------
 * 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.0modelVersion>

    <parent>
        <groupId>com.itymlgroupId>
        <artifactId>springcloud-alibabaartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <groupId>com.itymlgroupId>
    <artifactId>shop-userartifactId>
    <version>1.0-SNAPSHOTversion>
    <dependencies>
        <dependency>
            <groupId>com.itymlgroupId>
            <artifactId>shop-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

project> 
  1. 编写主类
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);
    }
}
  1. 创建配置文件
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.0modelVersion>

    <parent>
        <groupId>com.itymlgroupId>
        <artifactId>springcloud-alibabaartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <groupId>com.itymlgroupId>
    <artifactId>shop-productartifactId>
    <version>1.0-SNAPSHOTversion>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.itymlgroupId>
            <artifactId>shop-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

project>
  1. 创建工程主类
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);
    }
}
  1. 创建配置文件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
  1. 创建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 {
}
  1. 创建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();
    }
}
  1. 创建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;
    }

}
  1. 启动工程

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.0modelVersion>

    <parent>
        <groupId>com.itymlgroupId>
        <artifactId>springcloud-alibabaartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <groupId>com.itymlgroupId>
    <artifactId>shop-orderartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.itymlgroupId>
            <artifactId>shop-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>
project>
  1. 创建工程主类
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);
    }
}
  1. 创建配置文件
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
  1. 创建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 {
}
  1. 创建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);
}
  1. 创建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();
    }
}
  1. 创建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);
    }

}
  1. 启动工程,通过浏览器访问服务进行测试

转载请注明:xuhss » SpringCloudAlibaba 微服务讲解(二)微服务环境搭建

喜欢 (0)

您必须 登录 才能发表评论!