微服务环境搭建 |
您所在的位置:网站首页 › 端口8081 › 微服务环境搭建 |
微服务 我们本次是使用的电商项目中的商品、订单、用户为案例进行讲解. 案例准备技术选型maven:3.3.9 **数据库:**MySQL 5.7 持久层:** SpingData Jpa 其他:SpringCloud Alibaba 技术栈 模块设计springcloud-alibaba 父工程 shop-common 公共模块【实体类】 shop-user 用户微服务 【端口: 8071】 shop-product 商品微服务 【端口: 8081】 shop-order 订单微服务 【端口: 8091】 微服务调用在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为例来 演示微服务的调用:客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微服务 查询商品的信息。 我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。 在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者。 创建父工程创建一个maven工程,然后在pom.xml文件中添加下面内容 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE cn.maruifu springcloud-alibaba 1.0-SNAPSHOT pom 1.8 UTF-8 UTF- 8 Greenwich.RELEASE 2.1.0.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import版本对应: 创建基础模块创建 shop-common 模块: 在pom.xml中添加依赖 springcloud-alibaba cn.maruifu 1.0-SNAPSHOT 4.0.0 shop-common org.springframework.boot spring-boot-starter-data-jpa org.projectlombok lombok com.alibaba fastjson 1.2.56 mysql mysql-connector-java 5.1.6创建实体类 @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;//手机号 } //商品 @Entity(name = "shop_product") @Data public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer pid;//主键 private String pname;//商品名称 private Double pprice;//商品价格 private Integer stock;//库存 } //订单 @Entity(name = "shop_order") @Data public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long oid;//订单id private Integer uid;//用户id private String username;//用户名 }创建用户微服务新建一个 shop-user 模块,然后进行下面操作 创建pom.xml springcloud-alibaba cn.maruifu 1.0-SNAPSHOT 4.0.0 shop-user com.itheima shop-common 1.0-SNAPSHOT 编写主类@SpringBootApplication @EnableDiscoveryClient public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }创建配置文件server: port: 8071 spring: application: name: service-product datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: root jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect创建商品微服务创建pom.xml springcloud-alibaba cn.maruifu 1.0-SNAPSHOT 4.0.0 shop-product org.springframework.boot spring-boot-starter-web com.itheima shop-common 1.0-SNAPSHOT 编写主类@SpringBootApplication public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } }创建配置文件server: port: 8081 spring: application: name: service-product datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: root jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect创建ProductDao接口package com.itheima.dao; public interface ProductDao extends JpaRepository { } 复制代码创建ProductService类package cn.maruifu.service.impl; @Service public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override public Product findByPid(Integer pid) { return productDao.findById(pid).get(); } } 复制代码创建Controller@RestController @Slf4j public class ProductController { @Autowired private ProductService productService; @GetMapping("/product/{pid}") public Product product(@PathVariable("pid") Integer pid) { Product product = productService.findByPid(pid); log.info("查询到商品:" + JSON.toJSONString(product)); return product; } } 复制代码启动工程等到数据库表创建完毕之后,加入测试数据 INSERT INTO shop_product VALUE(NULL,'小米','1000','5000'); INSERT INTO shop_product VALUE(NULL,'华为','2000','5000'); INSERT INTO shop_product VALUE(NULL,'苹果','3000','5000'); INSERT INTO shop_product VALUE(NULL,'OPPO','4000','5000');通过浏览器访问服务创建订单微服务创建pom.xml springcloud-alibaba cn.maruifu 1.0-SNAPSHOT 4.0.0 shop-order org.springframework.boot spring-boot-starter-web com.itheima shop-common 1.0-SNAPSHOT 编写主类package cn.maruifu; @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }创建配置文件server: port: 8091 spring: application: name: service-product datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: root jpa: properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect创建OrderDao接口package com.itheima.dao; public interface OrderDao extends JpaRepository { } 复制代码创建OrderService类@Service public class OrderServiceImpl implements OrderService { @Autowired private OrderDao orderDao; @Override public void save(Order order) { orderDao.save(order); } }6创建RestTemplate@SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }创建Controllerpackage com.itheima.controller; @RestController @Slf4j public class OrderController { @Autowired private RestTemplate restTemplate; @Autowired private OrderService orderService; //准备买1件商品 @GetMapping("/order/prod/{pid}") public Order order(@PathVariable("pid") Integer pid) { log.info(">>客户下单,这时候要调用商品微服务查询商品信息"); //通过restTemplate调用商品微服务 Product product = restTemplate.getForObject( "http://localhost:8081/product/" + pid, Product.class); log.info(">>商品信息,查询结果:" + JSON.toJSONString(product)); Order order = new Order(); order.setUid(1); order.setUsername("测试用户"); order.setPid(product.getPid()); order.setPname(product.getPname()); order.setPprice(product.getPprice()); order.setNumber(1); orderService.save(order); return order; } }浏览器访问服务进行测试本文由 小马哥 创作,采用 知识共享署名4.0 国际许可协议进行许可 本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名 最后编辑时间为: 2021/01/27 21:27 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |