微服务学习1——微服务环境搭建

微服务学习1——微服务环境搭建

(参考黑马程序员项目)
个人仓库地址:https://gitee.com/jkangle/springboot-exercise.git

微服务就是将单体应用进一步拆分,拆成更小的服务,拆完之后怎么调用,主流的技术有RESTful和RPC,(首先以RESTful为例子)

1.模块设计

shop-common 公共模块——相当于工具类

shop-user 用户微服务——存储用户的信息

shop-product 商品微服务——存储商品的所有信息

shop-order 订单微服务——存储订单的所有信息

【注意】所有的对数据库的操作是结合JPA

2.实现目标

通过订单微服务来查询商品的信息,也就是通过订单的微服务调用商品的微服务

在这里插入图片描述

3.具体实现

在这里插入图片描述

3.1创建一个父工程test

父工程做的内容就是规定好这个项目所有的版本号这些,因此只需要pom.xml文件即可

<?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.0</modelVersion><groupId>org.example</groupId><artifactId>test</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>shop-common</module><module>shop-users</module><module>shop-product</module><module>shop-order</module></modules><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-boot.version>2.7.3</spring-boot.version><spring-cloud.version>2021.0.7</spring-cloud.version><spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version><fastjson.version>1.2.57</fastjson.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency></dependencies></dependencyManagement></project>
3.2在父工程中创建公共模块

公共模块就是相当于是一个工具类,里面有数据库信息的实体类

package pojo;import lombok.Data;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;/*** 订单的实体类*/
@Entity(name = "shop_order")
@Data
public class Order {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)/*** 订单id*/private int oid;/*** 用户id*/private Integer uid;/*** 物品id*/private Integer pid;/*** 物品名称*/private String pname;/*** 物品价格*/private Double pprice;/*** 用户姓名*/private String username;/*** 购买数量*/private Integer number;
}
package pojo;import lombok.Data;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;/***** @author jkk* @since 2020-03-20-20:25*/
@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;}
3.3在父工程中创建用户模块

用户模块就是实现与用户的表相关的操作,对用户的业务就放到这个模块中,所以每一个模块中必须有一个启动类Application,当然需要配置它的yml文件

package org.example.users;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @author jkk*/
@SpringBootApplication
@EnableDiscoveryClient
public class ShopUsersApplications {public static void main(String[] args) {SpringApplication.run(ShopUsersApplications.class, args);}
}
server:port: 8071
spring:application:name: service-productdatasource:url: jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8username: rootpassword:driver-class-name: com.mysql.cj.jdbc.Driverjpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect

当前微服务并没有使用,所以没有写相关的具体业务


3.4在父工程中创建商品模块

同理将商品所需要的业务写在里面

controller

package org.example.product.controller;import com.alibaba.fastjson.JSON;
import org.example.product.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 pojo.Product;@RestController
public class ProductController {private static final Logger log = LoggerFactory.getLogger(ProductController.class);@Autowiredprivate ProductService productService;@GetMapping("/product/{pid}")public Product product(@PathVariable("pid") Integer pid) {Product product = productService.findByPid(pid);log.info("查询到商品:" + JSON.toJSONString(product));return product;}
}

dao

package org.example.product.dao;import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import pojo.Product;@Repository
public interface ProductDao extends JpaRepository<Product,Integer> {}

service

package org.example.product.service;import pojo.Product;public interface ProductService {Product findByPid(Integer pid);
}
package org.example.product.service.impl;import org.example.product.dao.ProductDao;
import org.example.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Product;import java.util.Optional;@Service
public class ProductServiceImpl implements ProductService {@Autowiredprivate ProductDao productDao;@Overridepublic Product findByPid(Integer pid) {Optional<Product> optional = productDao.findById(pid);if (optional.isPresent()) {return optional.get();}return null;}}

启动类

package org.example.product;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;@EnableJpaRepositories
@SpringBootApplication
@EntityScan(basePackages = "pojo")
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class,args);}
}

配置文件

server:port: 8081spring:application:name: service-productdatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8username: rootpassword:jpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect
3.5在父工程中创建订单模块

controller

package org.example.controller;import com.alibaba.fastjson.JSON;
import org.example.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import pojo.Order;
import pojo.Product;/*** 使用restTemplate对象调用shop-product的请求方法*/
@RestController
public class OrderController {private static final Logger log = LoggerFactory.getLogger(OrderController.class);@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate OrderService orderService;@GetMapping("/order/prod/{pid}")public Order order(@PathVariable("pid") Integer pid){Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));Order order = new Order();order.setOid(1);order.setUid(1);order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setNumber(1);orderService.save(order);return order;}}

dao

package org.example.dao;import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import pojo.Order;
@Repository
public interface OrderDao extends JpaRepository<Order,Integer> {
}

service

package org.example.service;import pojo.Order;/*** 订单的service层接口*/
public interface OrderService {/*** 将订单存到数据库* @param order*/public void save(Order order);
}
package org.example.service.impl;import org.example.dao.OrderDao;
import org.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Order;/*** service接口的实现*/
@Service
public class OrderServiceImpl implements OrderService {@Autowiredprivate OrderDao orderDao;/*** 存储订单* @param order*/@Overridepublic void save(Order order) {orderDao.save(order);}
}

启动类

package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.client.RestTemplate;/*** shop-order的启动类* @author jkk*/
@SpringBootApplication
@EnableJpaRepositories
@EntityScan(basePackages = "pojo")
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class,args);}@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}
}

yml配置文件

server:port: 8091spring:application:name: service-productdatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8username: rootpassword:jpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect

4.运行

可以注意到,当前调用另一个微服务的时候是通过把服务提供者的网络地址 (ip,端口)等硬编码到了代码中

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/18097.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

spring boot+MySQL福聚苑社区团商品购系统

开发语言&#xff1a;Java 框架&#xff1a;springboot JDK版本&#xff1a;JDK1.8 服务器&#xff1a;tomcat7 数据库&#xff1a;mysql 5.7 数据库工具&#xff1a;Navicat11 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;Maven3.3.9

R语言——字符串处理

paste(abc, def, gh, sep ) #粘贴字符串 substr(abcdefg, 2, 3) # 取特定字符串 gsub(abc, , c(abc, abcc, abcbc)) # 将字符串中abc替换为空 strsplit(a;b;c, ;, fixed T) # 按照;切分字符串 strsplit(a222b2.2c, 2.2, fixed F) # 按照正则表达式分隔&#xff0c;这里的.是…

RAID5更换磁盘操作导致数据丢失的数据恢复案例

服务器数据恢复环境&#xff1a; 曙光某型号光纤存储柜&#xff0c;16块光纤磁盘组建了2组RAID5磁盘阵列&#xff0c;每组raid5阵列中有7块成员盘&#xff0c;另外2块磁盘配置为全局热备盘使用。 第一组RAID5阵列划分了3个LUN&#xff1a;1个LUN分配给linux主机、第2个LUN分配给…

【InnoDB 存储引擎】15.7.1 InnoDB Locking(锁实验,包含了如 记录锁、间隙锁、Next-Key Lock 算法等,重要)

文章目录 1 关于 Record Lock 的实验1.1 实验 1&#xff1a;没有主键时的如何锁定1.2 实验 1&#xff08;续&#xff09;&#xff1a;带着问题继续实验1.3 实验 2&#xff1a;有主键时如何锁定 2 关于 Next-Key Lock 的实验2.1 实验 3&#xff1a;如何确定算法的锁定范围2.2 实…

沃尔玛(Walmart)跨境电商大额采退如何提高成功率(系统详解)

市面上的网络环境有非常多种&#xff0c;纯ip类的有luminati&#xff0c;922&#xff0c;G3&#xff0c;M5&#xff0c;谷歌fi&#xff0c;纯环境类的有VM&#xff0c;ads, 巨象&#xff0c;林肯法球等等。还有VPS比如无间道&#xff0c;手机AWZ /ALS 还有一些环境IP的比如云手…

Flink DataStream之使用filter实现分流

新建类 package test01;import org.apache.flink.api.common.JobExecutionResult; import org.apache.flink.configuration.Configuration; import org.apache.flink.streaming.api.datastream.DataStreamSource; import org.apache.flink.streaming.api.datastream.SingleOut…

技术导向下的业务测试何去何从?

前两天我发了篇鼓励测试人员学编程&#xff08;思维&#xff09;的文章《做测试到底要不要学编程&#xff1f;》&#xff0c;有不少同学在后台问我&#xff0c;自己底子差&#xff0c;实在跟不上怎么办&#xff1f; 看起来&#xff0c;应该是我没说清楚&#xff0c;导致大家有…

UFS 14 - UFS RPMB安全读写命令

UFS 14 - UFS RPMB安全读写命令 1 SECURITY PROTOCOL IN/OUT Commands1.1 CDB format of SECURITY PROTOCOL IN/OUT commands1.2 Supported security protocols list description1.3 Certificate data description 2 CDB format of SECURITY PROTOCOL IN/OUT commands demo2.1 …

地下饮用水除硝酸盐技术、生活用水提质增效

项目名称 北京某地下水除硝酸盐项目&#xff0c;出水未检出 工艺选择 两处水源&#xff0c;运行方式为一用一备 工艺原理 树脂官能团耐受硫酸盐、氯离子的干扰&#xff0c;实现选择性吸附硝酸根 项目背景 近年来由于农业活动及排污物的影响&#xff0c;部分地表…

string——find(),rfind()

文章目录 find&#xff08;正向查找&#xff09; 正向查抄&#xff0c;会返回要找的子串中第一字符再父串中的下标值 rfind&#xff08;逆向查找&#xff09; std::string url "https://img.bizhizu.com/2015/1231/hskdkfl.jpg";int begin url.find(/); int end …

vue2基础用法原理

本篇转载&#xff1a;http://t.csdn.cn/KtidI 文章目录 1. vue基础知识和原理1.1 初识Vue1.2 模板语法1.3 数据绑定1.4 el与data的两种写法1.5 Vue中的MVVM1.6 数据代理1.7 事件处理1.8 键盘事件1.9 计算属性1.10 监视属性1.11 绑定样式class样式style样式 1.12 条件渲染v-ifv-…

【设计模式】设计模式简介+七大设计原则介绍

设计模式介绍 简介 设计模式是对软件设计中普遍存在(反复出现) 的各种问题&#xff0c;所提出的解决方案。 为什么学习设计模式 软件要做大&#xff0c;要先进行设计&#xff0c;才能保证其有高的稳定性、扩展性&#xff08;容易按照新的需求添加功能&#xff09;、复用性&…