SpringCloudAlibaba|SpringCloudAlibaba 微服务讲解(二)微服务环境搭建
微服务环境搭建
我们这次是使用的电商项目的商品、订单、用户为案例进行讲解
2.1 案例准备
2.1.1 技术选型
- maven :3.3.9
- 数据库:mysql
- 持久层:SpringData JPA
- SpringCloud Alibaba技术栈
- springcloud-alibaba 父工程
- shop-common 公共模块
- shop-user 用户模块
- shop-product 商品模块
- shop-order 订单模块
在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为例来 演示微服务的调用:客户向订单微服务发起一下单的请求,在进行保存订单之前需要调用商品微服务 查询商品的信息
我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者.
2.2创建父工程
4.0.0 org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.ityml
springcloud-alibaba
1.0-SNAPSHOT pom1.8 UTF-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模块
- 在pom中添加依赖
4.0.0 com.ityml springcloud-alibaba1.0-SNAPSHOT com.ityml shop-common1.0-SNAPSHOT org.springframework.boot spring-boot-starter-data-jpaorg.projectlombok lombokcom.alibaba fastjson1.2.79 mysql mysql-connector-java5.1.49
- 创建实体类
/** * ----------------------------- * 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; }
- 创建模块
- 创建SpringBoot主类
- 加入配置文件
- 创建必要的接口和实现类
- 创建pom
4.0.0 com.ityml springcloud-alibaba1.0-SNAPSHOT com.ityml shop-user1.0-SNAPSHOT com.ityml shop-common1.0-SNAPSHOT
- 编写主类
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); } }
- 创建配置文件
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
- 创建一个名为shop-product的模块,并添加springboot依赖
4.0.0 com.ityml springcloud-alibaba1.0-SNAPSHOT com.ityml shop-product1.0-SNAPSHOT org.springframework.boot spring-boot-starter-webcom.ityml shop-common1.0-SNAPSHOT
- 创建工程主类
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); } }
- 创建配置文件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
- 创建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 { }
- 创建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(); } }
- 创建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; }}
- 启动工程
- 创建一个名为shop-order的模块,并添加springboot依赖
4.0.0 com.ityml springcloud-alibaba1.0-SNAPSHOT com.ityml shop-order1.0-SNAPSHOT org.springframework.boot spring-boot-starter-webcom.ityml shop-common1.0-SNAPSHOT
- 创建工程主类
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); } }
- 创建配置文件
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
- 创建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
{ }
- 创建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); }
- 创建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(); } }
- 创建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); }}
- 【SpringCloudAlibaba|SpringCloudAlibaba 微服务讲解(二)微服务环境搭建】启动工程,通过浏览器访问服务进行测试
推荐阅读
- 领域驱动模型DDD(一)——服务拆分策略
- Linux 下一代架构基金会宣布(正式成立 NextArch 基金会微服务技术组!联手腾讯等企业/社区共同发力微服务标准化建设)
- hadoop|Zookeeper 应用案例(一)之服务器上下线动态感知
- 中间件|ZooKeeper学习笔记(八):服务器动态上下线原理
- 利用NGINX搭建部署直播流媒体服务器
- 【FAQ】关于分析服务错误获取所选日期前一天事件数据的解决方法
- 实现与区块链外的服务进行无缝交互的中间件
- 图解|图解 K8S(07)(调度利器之亲和与反亲和(服务容灾))
- 技术平台&应用开发专题月|如何保证业务服务稳定运行—用友云原生技术平台高可用能力介绍
- centos|docker 配置hosts无法启动docker服务