SpringCloudAlibaba|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创建父工程
4.0.0org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.ityml springcloud-alibaba 1.0-SNAPSHOTpom1.8UTF-8UTF-8 Greenwich.RELEASE 2.1.0.RELEASE 8 8 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import

对应版本:
https://www.cnblogs.com/onehm/p/13931711.html
2.3创建基础模块 2.3.1 shop-common模块
  1. 在pom中添加依赖
    4.0.0com.ityml springcloud-alibaba 1.0-SNAPSHOT com.ityml shop-common 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-data-jpa org.projectlombok lombok com.alibaba fastjson 1.2.79 mysql mysql-connector-java 5.1.49

  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
    4.0.0com.ityml springcloud-alibaba 1.0-SNAPSHOT com.ityml shop-user 1.0-SNAPSHOT com.ityml shop-common 1.0-SNAPSHOT

  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依赖
    4.0.0com.ityml springcloud-alibaba 1.0-SNAPSHOT com.ityml shop-product 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web com.ityml shop-common 1.0-SNAPSHOT

  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 { }

  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 classProductController {@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依赖
    4.0.0com.ityml springcloud-alibaba 1.0-SNAPSHOT com.ityml shop-order 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web com.ityml shop-common 1.0-SNAPSHOT

  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 { }

  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. 【SpringCloudAlibaba|SpringCloudAlibaba 微服务讲解(二)微服务环境搭建】启动工程,通过浏览器访问服务进行测试

    推荐阅读