日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

模拟微服务业务场景

發(fā)布時(shí)間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模拟微服务业务场景 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、創(chuàng)建步驟

模擬開(kāi)發(fā)過(guò)程中的服務(wù)間關(guān)系。抽象出來(lái),開(kāi)發(fā)中的微服務(wù)之間的關(guān)系是生產(chǎn)者和消費(fèi)者關(guān)系。

總目標(biāo):模擬一個(gè)最簡(jiǎn)單的服務(wù)調(diào)用場(chǎng)景,場(chǎng)景中保護(hù)微服務(wù)提供者(Producer)和微服務(wù)調(diào)用者(Consumer),方便后面使用微服務(wù)架構(gòu)

注意:每個(gè)微服務(wù)為一個(gè)獨(dú)立的SpringBoot工程。

(1)創(chuàng)建一個(gè)新的Maven父工程



(2)添加起步依賴坐標(biāo)

<!--創(chuàng)建SpringBoot的父工程--> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version> </parent><!--SpringBoot的依賴管理坐標(biāo)--> <dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>

二、在父工程下創(chuàng)建子模塊

1.創(chuàng)建privider_service模塊

目錄結(jié)構(gòu)

2.需要的依賴和配置文件application.yml

(1)依賴

<?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_parent</artifactId><groupId>com.william</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>privider_service</artifactId><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!--web的依賴了--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--數(shù)據(jù)庫(kù)的依賴--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--jpa依賴坐標(biāo)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency></dependencies></project>

(2)配置文件application.yml

server:#服務(wù)的端口port: 9091 # DB 配置 spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTCpassword: rootusername: root

3.數(shù)據(jù)庫(kù)的準(zhǔn)備

(1) 創(chuàng)建springcloud數(shù)據(jù)庫(kù)

-- 創(chuàng)建數(shù)據(jù)庫(kù) CREATE database springcloud CHARACTER SET utf8 COLLATE utf8_general_ci;

(2)創(chuàng)建tb_user用戶表

-- 使用springcloud數(shù)據(jù)庫(kù) USE springcloud; -- ---------------------------- -- Table structure for tb_user -- ---------------------------- CREATE TABLE `tb_user` (`id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(100) DEFAULT NULL COMMENT '用戶名',`password` varchar(100) DEFAULT NULL COMMENT '密碼',`name` varchar(100) DEFAULT NULL COMMENT '姓名',`age` int(11) DEFAULT NULL COMMENT '年齡',`sex` int(11) DEFAULT NULL COMMENT '性別,1男,2女',`birthday` date DEFAULT NULL COMMENT '出生日期',`created` date DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',`updated` date DEFAULT NULL COMMENT '更新時(shí)間',`note` varchar(1000) DEFAULT NULL COMMENT '備注',PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用戶信息表'; -- ---------------------------- -- Records of tb_user -- ---------------------------- INSERT INTO `tb_user` VALUES ('1', 'zhangsan', '123456', '張三', '13', '1', '2006-08-01', '2019-05-16', '2019-05-16', '張三'); INSERT INTO `tb_user` VALUES ('2', 'lisi', '123456', '李四', '13', '1', '2006-08-01', '2019-05-16', '2019-05-16', '李四');

4.domain層

package com.itheima.domain;import javax.persistence.*; import java.util.Date;@Entity @Table(name = "tb_user") public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;//主鍵idprivate String username;//用戶名private String password;//密碼private String name;//姓名private Integer age;//年齡private Integer sex;//性別 1男性,2女性private Date birthday; //出生日期private Date created; //創(chuàng)建時(shí)間private Date updated; //更新時(shí)間private String note;//備注@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", name='" + name + '\'' +", age=" + age +", sex=" + sex +", birthday=" + birthday +", created=" + created +", updated=" + updated +", note='" + note + '\'' +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Date getCreated() {return created;}public void setCreated(Date created) {this.created = created;}public Date getUpdated() {return updated;}public void setUpdated(Date updated) {this.updated = updated;}public String getNote() {return note;}public void setNote(String note) {this.note = note;} }

5.controller層

package com.william.Controller;import com.william.domain.User; import com.william.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @author :lijunxuan* @date :Created in 2019/6/29 10:09* @description :* @version: 1.0*/ @RestController @RequestMapping("/user") public class UserController {@AutowiredUserService userService;@RequestMapping("findById")public User findById(Integer id){return userService.findById(id);}}

6.service層

(1)UserService接口

package com.william.service;import com.william.domain.User;public interface UserService {User findById(Integer id);}

(2)UserServiceImpl實(shí)現(xiàn)類

package com.william.service.Impl;import com.william.Dao.UserDao; import com.william.domain.User; import com.william.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.Optional;/*** @author :lijunxuan* @date :Created in 2019/6/29 10:24* @description :* @version: 1.0*/ @Service public class UserServiceImpl implements UserService {@AutowiredUserDao userDao;@Overridepublic User findById(Integer id) {Optional<User> userfindById = userDao.findById(id);return userfindById.get();} }

7.Dao層

package com.william.Dao;import com.william.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;@Repository public interface UserDao extends JpaRepository<User,Integer> { }

8.ProviderServiceApplication

package com.william;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author :lijunxuan* @date :Created in 2019/6/29 10:34* @description :* @version: 1.0*/ @SpringBootApplication public class ProviderServiceApplication {public static void main(String[] args) {SpringApplication.run(ProviderServiceApplication.class,args);} }

9.測(cè)試結(jié)果

2.consumer_service

(1)創(chuàng)建過(guò)程

(2)導(dǎo)入依賴和目錄結(jié)構(gòu)

<?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_parent</artifactId><groupId>com.william</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>consumer_service</artifactId><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--熱部署依賴--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies></project>

(3)application.yml

server:port: 8080

(4)DemoApplication

package com.william;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;/*** @author :lijunxuan* @date :Created in 2019/6/29 9:53* @description :* @version: 1.0*/ @SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class,args);}//RestTemplate注入SPring容器當(dāng)中@Beanpublic RestTemplate restTemplate(){return new RestTemplate();} }

(5)ConsumerController

package com.william.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;/*** @author :lijunxuan* @date :Created in 2019/6/29 9:54* @description :* @version: 1.0*/ @RestController @RequestMapping("/consumer") public class ConsumerController {@AutowiredRestTemplate restTemplate;@RequestMapping("/findUser")public String findUser(Integer id){//請(qǐng)求服務(wù)提供者的查詢用戶詳情地址// String url="http://localhost:9091/user/findById?id="+id;alt+enter 找到formatString url= String.format("http://localhost:9091/user/findById?id=%d", id);return restTemplate.getForObject(url,String.class);}}

(6)測(cè)試結(jié)果

總結(jié)

以上是生活随笔為你收集整理的模拟微服务业务场景的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 人妻在线一区 | 骚虎视频在线观看 | 欧美激情视频二区 | 精品自拍视频 | 午夜精品福利电影 | 日韩精品一区二区免费视频 | 2020亚洲天堂 | 国产亚洲欧美日韩精品一区二区三区 | 91 在线视频 | 欧美日韩精选 | 一区二区三区国产 | 最好看的mv中文字幕国语电影 | 色男人影院 | 制服 丝袜 综合 日韩 欧美 | 97人妻精品一区二区三区视频 | 欧美成人一二三 | 毛片1000部免费看 | 中文字幕日韩精品亚洲一区小树林 | 日韩一级完整毛片 | 日韩久久久久久久久 | 国产性猛交96 | 自拍1区| 成人毛毛片 | 国产精品婷婷午夜在线观看 | 污视频在线观看免费 | 91视频亚洲 | 日韩在线观看av | 日韩一二三区在线观看 | 国内精品第一页 | 精品一区二区不卡 | 一区二区三区在线不卡 | 国产va亚洲va在线va | 亚洲精品字幕在线 | 少妇被躁爽到高潮无码文 | 在线免费看a| 久久久五月| 成人av中文解说水果派 | 天天干影院| 捆绑无遮挡打光屁股 | 亚洲av不卡一区二区 | 日本成人精品 | 国产精品乱码久久久久久久久 | 国产欧美日韩综合精品一区二区三区 | 日本三级精品 | 成人av国产 | 国产精品日韩 | 国产欧美激情视频 | 麻豆精品国产 | 欧美精品乱码久久久久久 | 日韩h在线观看 | 自拍偷拍色 | 亚洲黄片一区二区三区 | 波多野结衣一区二区三区中文字幕 | 91秘密入口 | 按摩害羞主妇中文字幕 | 96亚洲精品久久久蜜桃 | 操网站 | 一区二区三区视频在线免费观看 | 在线激情小视频 | 一本一道久久综合 | 久久久久伊人 | 久久综合桃花网 | 翔田千里在线播放 | 国产卡一卡二卡三无线乱码新区 | 夜夜爽妓女8888视频免费观看 | 日韩免费电影一区 | 伊人久久大香线蕉综合75 | 色播视频在线播放 | 亚洲男人网站 | 日韩操比 | 年代下乡啪啪h文 | 亚洲第一视频在线播放 | 成人精品| 亚洲第一综合 | 豆花在线视频 | 好吊一二三区 | 亚洲特级黄色片 | 九热视频在线观看 | 久久久久久久一区二区三区 | 欧美啪啪一区二区 | 欧美日韩在线中文字幕 | 黄色日b片 | 在线成人亚洲 | 自拍偷拍校园春色 | 亚洲精品视频免费 | 免费在线观看av的网站 | 国产一区二区三区播放 | 欧美又粗又大xxxxbbbb疯狂 | 德国性经典xxxx性hd | 亚洲精品福利在线观看 | 销魂奶水汁系列小说 | 亚洲成人免费视频 | 欧美日韩69 | 久久久精品人妻一区二区三区色秀 | 一级片久久 | 欧美成人毛片 | 久草老司机| 成人精品在线 | 亚洲国产视频网站 |