微服务环境搭建SpringCloud入门
创始人
2024-02-15 11:42:13
0

目录

案例准备

技术选型

模块设计

微服务调用

创建父工程

创建基础模块

创建用户微服务

创建商品微服务

创建订单微服务


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

案例准备

技术选型

maven:3.5.4 数据库:MySQL 5.7 持久层: SpingData Jpa/Mybatis-plus 其他: SpringCloud Alibaba 技术栈

模块设计

springcloud-shop父工程 shop-common 公共模块【实体类】 shop-user 用户微服务 【端口: 807x】 shop-product 商品微服务 【端口: 808x】 shop-order 订单微服务 【端口: 809x】

 

微服务调用

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

创建父工程

创建一个maven工程,然后在pom.xml文件中添加下面内容


4.0.0org.examplespringcloud-shops1.0-SNAPSHOTshop-commonpom1.8UTF-8UTF-82.3.2.RELEASEHoxton.SR92.2.6.RELEASEorg.springframework.bootspring-boot-dependencies${spring-boot.version}pomimport

 

版本对应:

版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub

 

创建基础模块

1 创建shop-common 模块,在pom.xml中添加依赖


springcloud-shopsorg.example1.0-SNAPSHOT4.0.0shop-commonorg.projectlomboklombokcom.alibabafastjson1.2.56mysqlmysql-connector-java5.1.44

2 创建实体类

//用户
@Entity(name = "shop_user")//实体类跟数据表的对应
@Data//不再去写set和get方法
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")@Datapublic class Order {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long oid;//订单id​    //用户    private Integer uid;//用户id    private String username;//用户名​    //商品    private Integer pid;//商品id    private String pname;//商品名称    private Double pprice;//商品单价​    //数量    private Integer number;//购买数量}

创建用户微服务

步骤: 1 创建模块 导入依赖 2 创建SpringBoot主类 3 加入配置文件 4 创建必要的接口和实现类(controller service dao) 新建一个shop-user 模块,然后进行下面操作 1 创建pom.xml


springcloud-shopsorg.example1.0-SNAPSHOT4.0.0shop-user1.8UTF-8UTF-82.3.7.RELEASEorg.springframework.bootspring-boot-starter-weborg.exampleshop-common1.0-SNAPSHOTorg.springframework.bootspring-boot-starter-webRELEASEcompileorg.springframework.bootspring-boot-starter-webRELEASEcompile

2 编写主类

package com.zjy.shopuser;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ShopUserApplication {public static void main(String[] args) {SpringApplication.run(ShopUserApplication.class, args);}}

3 创建配置文件(没有连接数据库的话server下面的删掉)

spring:application:name: shop-user
server:port: 8071datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: sasajpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect

4 编写控制层

package com.zjy.shopuser.controller;import com.zjy.model.User;import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author zjy* @site Bi8boYin* @company xxx公司* @create  2022-11-25 15:27*/
@RestController
@RequestMapping("/user")
public class UserController {@RequestMapping("/get/{id}")public User get(@PathVariable("id") Integer id){return new User(id,"小猪","xiaozhu111","17346958024");}
}

 

创建商品微服务

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


springcloud-shopsorg.example1.0-SNAPSHOT4.0.0shop-product1.8UTF-8UTF-82.3.7.RELEASEorg.springframework.bootspring-boot-starter-weborg.exampleshop-common1.0-SNAPSHOT

2 创建工程的主类

@SpringBootApplication
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class);}
}

3 创建配置文件application.yml

spring:application:name: shop-product
server:port: 8081
datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: sasajpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect

4、创建controller

package com.zjy.shopproduct.controller;import com.zjy.model.Product;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author zjy* @site Bi8boYin* @company xxx公司* @create  2022-11-25 16:28*/@RestController
@RequestMapping("/product")
public class ProductController {@RequestMapping("/get/{pid}")public Product get(@PathVariable("pid") Integer pid){return  new Product(pid,"小猪历险记",100d,23);}
}

 

创建订单微服务

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


springcloud-shopsorg.example1.0-SNAPSHOT4.0.0shop-order1.8UTF-8UTF-82.3.7.RELEASEorg.springframework.bootspring-boot-starter-weborg.exampleshop-common1.0-SNAPSHOT

2 创建工程的主类

package com.zjy.shoporder;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
public class ShopOrderApplication {public static void main(String[] args) {SpringApplication.run(ShopOrderApplication.class, args);}@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}
}

3、yml文件

server:port: 8091
spring:application:name: shop-orderdatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: sasajpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect

4编写controller层

package com.zjy.shoporder.controller;import com.zjy.model.Order;
import com.zjy.model.Product;
import com.zjy.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;/*** @author zjy* @site Bi8boYin* @company xxx公司* @create  2022-11-25 16:54*/
@RestController
@RequestMapping("/order")
public class OrderController {@Autowiredprivate RestTemplate restTemplate;@RequestMapping("/get/{uid}/{pid}")public Order get(@PathVariable("uid") Integer uid,@PathVariable("pid") Integer pid){/*** 要在订单微服务中去调用 用户微服务 以及我们的 商品微服务 ,也就以为这跨项目调用httpClients*/User user = restTemplate.getForObject("http://localhost:8071/user/get/" + uid, User.class);Product product = restTemplate.getForObject("http://localhost:8081/product/get/" + pid, Product.class);Order order=new Order();order.setUsername(user.getUsername());order.setUid(user.getUid());order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setOid(System.currentTimeMillis());order.setNumber(product.getStock());return order;}}

 

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...