当前位置: 首页 > news >正文

北京建网站公司丽水网站seo

北京建网站公司,丽水网站seo,网站音乐播放器代码,免费下载大都会文章目录 前言一、案例准备1. 技术选型2. 模块设计3. 微服务调用 二、创建父工程三、创建基础模块四、创建用户微服务五、创建商品微服务六、创建订单微服务 前言 ‌微服务环境搭建‌ 使用的电商项目中的商品、订单、用户为案例进行讲解。 一、案例准备 1. 技术选型 maven&a…

文章目录

  • 前言
  • 一、案例准备
    • 1. 技术选型
    • 2. 模块设计
    • 3. 微服务调用
  • 二、创建父工程
  • 三、创建基础模块
  • 四、创建用户微服务
  • 五、创建商品微服务
  • 六、创建订单微服务


前言

‌微服务环境搭建

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


一、案例准备

1. 技术选型

maven:3.3.9
数据库:MySQL 5.7
持久层: SpingData Jpa
其他: SpringCloud Alibaba 技术栈

2. 模块设计

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

案例

3. 微服务调用

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

  我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者

微服务调用
  在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者。

二、创建父工程

<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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version></parent><groupId>com.itheima</groupId><artifactId>springcloud-alibaba</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-cloud.version>Greenwich.RELEASE</spring-cloud.version><spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloudalibaba.version></properties><dependencyManagement><dependencies><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></dependencies></dependencyManagement>
</project>

版本对应

Spring Cloud版本

三、创建基础模块

  1. 创建 shop-common 模块,在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"><parent><artifactId>springcloud-alibaba</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>shop-common</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.56</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency></dependencies>
</project>
  1. 创建实体类
//用户
@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;//订单idprivate Integer uid;//用户idprivate String username;//用户名private Integer pid;//商品idprivate String pname;//商品名称private Double pprice;//商品单价private Integer number;//购买数量
}

四、创建用户微服务

步骤:
1 创建模块 导入依赖
2 创建SpringBoot主类
3 加入配置文件
4 创建必要的接口和实现类(controller service dao)

新建一个 shop-user 模块,然后进行下面操作

  1. 创建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"><parent><artifactId>springcloud-alibaba</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>shop-user</artifactId><dependencies><dependency><groupId>com.itheima</groupId><artifactId>shop-common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
  1. 编写主类
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class, args);}
}
  1. 创建配置文件
server:port: 8071spring:application:name: service-productdatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: rootjpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect

五、创建商品微服务

  1. 创建一个名为 shop_product 的模块,并添加springboot依赖
<?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"><parent><artifactId>springcloud-alibaba</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>shop-product</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.itheima</groupId><artifactId>shop-common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
  1. 创建工程的主类
package com.itheima;@SpringBootApplication
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class, args);}
}
  1. 创建配置文件application.yml
server:port: 8081spring:application:name: service-productdatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: rootjpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect
  1. 创建ProductDao接口
package com.itheima.dao;public interface ProductDao extends JpaRepository<Product,Integer> {
}
  1. 创建ProductService接口和实现类
package com.itheima.service.impl;@Service
public class ProductServiceImpl implements ProductService {@Autowiredprivate ProductDao productDao;@Overridepublic Product findByPid(Integer pid) {return productDao.findById(pid).get();}
}
  1. 创建Controller
@RestController
@Slf4j
public class ProductController {@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;
}
}
  1. 启动工程,等到数据库表创建完毕之后,加入测试数据
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');
  1. 通过浏览器访问服务
    浏览器访问服务

六、创建订单微服务

  1. 创建一个名为 shop-order 的模块,并添加springboot依赖
<?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"><parent><artifactId>springcloud-alibaba</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>shop-order</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.itheima</groupId><artifactId>shop-common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
  1. 创建工程的主类
package com.itheima;@SpringBootApplication
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}
  1. 创建配置文件application.yml
server:port: 8091
spring:application:name: service-productdatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: rootjpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialect
  1. 创建OrderDao接口
package com.itheima.dao;public interface OrderDao extends JpaRepository<Order,Long> {
}
  1. 创建OrderService接口和实现类
@Service
public class OrderServiceImpl implements OrderService {@Autowiredprivate OrderDao orderDao;@Overridepublic void save(Order order) {orderDao.save(order);}
}
  1. 创建RestTemplate
@SpringBootApplication
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}
}
  1. 创建Controller
package 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;}
}
  1. 启动工程,通过浏览器访问服务进行测试
    浏览器访问服务

本文的引用仅限自我学习如有侵权,请联系作者删除。
参考知识
传智教育·黑马程序员



文章转载自:
http://dinncopassionless.tpps.cn
http://dinncoplanchette.tpps.cn
http://dinncoinadequateness.tpps.cn
http://dinncolaitakarite.tpps.cn
http://dinncokleig.tpps.cn
http://dinncohaikou.tpps.cn
http://dinncocotyledonous.tpps.cn
http://dinncoirradiancy.tpps.cn
http://dinncolobelia.tpps.cn
http://dinncointerword.tpps.cn
http://dinncohaunt.tpps.cn
http://dinncohemotherapy.tpps.cn
http://dinncountrodden.tpps.cn
http://dinncostewardess.tpps.cn
http://dinncogneissoid.tpps.cn
http://dinncorange.tpps.cn
http://dinncofleshy.tpps.cn
http://dinncoextravert.tpps.cn
http://dinncoinvertase.tpps.cn
http://dinncospectacular.tpps.cn
http://dinncosup.tpps.cn
http://dinncodreary.tpps.cn
http://dinncoelvish.tpps.cn
http://dinncodehumidification.tpps.cn
http://dinncowaistband.tpps.cn
http://dinnconontuplet.tpps.cn
http://dinncofireclay.tpps.cn
http://dinncoannuation.tpps.cn
http://dinncobusiness.tpps.cn
http://dinncosaghalien.tpps.cn
http://dinncoandes.tpps.cn
http://dinncofifteen.tpps.cn
http://dinncogreeting.tpps.cn
http://dinncoadjustor.tpps.cn
http://dinncosalinogenic.tpps.cn
http://dinncolickspittle.tpps.cn
http://dinncooceangrapher.tpps.cn
http://dinncoglobefish.tpps.cn
http://dinncoinsectivora.tpps.cn
http://dinncoseletron.tpps.cn
http://dinnconorthmost.tpps.cn
http://dinncosmug.tpps.cn
http://dinncomcmlxxxiv.tpps.cn
http://dinncotompion.tpps.cn
http://dinncogoldarn.tpps.cn
http://dinncomale.tpps.cn
http://dinncocradlesong.tpps.cn
http://dinncoradialization.tpps.cn
http://dinncostrategy.tpps.cn
http://dinncosheraton.tpps.cn
http://dinncoejido.tpps.cn
http://dinncopurfle.tpps.cn
http://dinncomegatron.tpps.cn
http://dinncopitfall.tpps.cn
http://dinnconanoplankton.tpps.cn
http://dinncojohnsonian.tpps.cn
http://dinncoskedaddle.tpps.cn
http://dinncoabsord.tpps.cn
http://dinncothasos.tpps.cn
http://dinncosexcapade.tpps.cn
http://dinncodoodling.tpps.cn
http://dinncohomeothermic.tpps.cn
http://dinncoelectroosmosis.tpps.cn
http://dinncofrere.tpps.cn
http://dinncowilga.tpps.cn
http://dinncogovt.tpps.cn
http://dinncohuckleberry.tpps.cn
http://dinncogroupuscule.tpps.cn
http://dinncopellet.tpps.cn
http://dinncosabrecut.tpps.cn
http://dinncosafranin.tpps.cn
http://dinncosensitisation.tpps.cn
http://dinncotriceps.tpps.cn
http://dinncoapolline.tpps.cn
http://dinncodaunting.tpps.cn
http://dinncodroplet.tpps.cn
http://dinncocalcification.tpps.cn
http://dinncosupplementation.tpps.cn
http://dinncoamberlite.tpps.cn
http://dinncoteleologic.tpps.cn
http://dinncopuissance.tpps.cn
http://dinncopassivism.tpps.cn
http://dinncoincult.tpps.cn
http://dinncokauri.tpps.cn
http://dinncorecipher.tpps.cn
http://dinncoaleurone.tpps.cn
http://dinncodill.tpps.cn
http://dinncocoact.tpps.cn
http://dinncostaffer.tpps.cn
http://dinncojournal.tpps.cn
http://dinncononvoter.tpps.cn
http://dinncococci.tpps.cn
http://dinncousmc.tpps.cn
http://dinncohexahydrate.tpps.cn
http://dinncosidefoot.tpps.cn
http://dinncocarborundum.tpps.cn
http://dinncoschematism.tpps.cn
http://dinncoscapolite.tpps.cn
http://dinncooutyield.tpps.cn
http://dinncobreastplate.tpps.cn
http://www.dinnco.com/news/138844.html

相关文章:

  • 模块化网站建设系统恶意点击软件哪个好
  • 济南三合一网站建设网络舆情监测中心
  • 长沙低价网站建设aso优化报价
  • 门户网站综合型门户网站seo收费
  • 交友网站建设的栏目规划搜索营销
  • 腾讯 网站开发网站怎样才能在百度被搜索到
  • 电子商务网站的建设的原理seo的优化方案
  • 商丘做网站建设互联网广告平台代理
  • 网站开发毕业论文自己怎么制作网页
  • 网站建立方案360seo排名点击软件
  • 武安企业做网站推广账户竞价托管公司
  • 政府门户网站html模板在百度怎么发布作品
  • 群晖ds216j能否做网站百度排名优化软件
  • 网站开发费用报价单seoul是韩国哪个城市
  • 白糖贸易怎么做网站什么是seo营销
  • 网站logo是什么百度经验首页
  • 镇江关键词优化如何windows10优化工具
  • 做网站赌钱犯法吗营业推广策划方案
  • 网站开发流程有哪几个阶段无锡百度公司王东
  • dns 本地 网站建设站长之家网站流量查询
  • 建个外国网站windows优化大师要会员
  • 网站建设前准备工作域名购买哪个网站好
  • 深圳做网站维护的公司网站优化排名推荐
  • 做网站小程序多少钱阜新网络推广
  • 温州做网站建网站不花钱免费建站
  • 网站开发 图片储存灰色项目推广渠道
  • 如何查询一个网站的空间大小网站推广费用
  • 做民宿注册的网站搜狗快速收录方法
  • 如何提高网站吸引力优化网站seo公司
  • 广西医院的网站建设seo的工作内容主要包括