模拟微服务业务场景
一、創建步驟
模擬開發過程中的服務間關系。抽象出來,開發中的微服務之間的關系是生產者和消費者關系。
總目標:模擬一個最簡單的服務調用場景,場景中保護微服務提供者(Producer)和微服務調用者(Consumer),方便后面使用微服務架構
注意:每個微服務為一個獨立的SpringBoot工程。
(1)創建一個新的Maven父工程
(2)添加起步依賴坐標
<!--創建SpringBoot的父工程--> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version> </parent><!--SpringBoot的依賴管理坐標--> <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>二、在父工程下創建子模塊
1.創建privider_service模塊
目錄結構
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><!--數據庫的依賴--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--jpa依賴坐標--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency></dependencies></project>(2)配置文件application.yml
server:#服務的端口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: root3.數據庫的準備
(1) 創建springcloud數據庫
-- 創建數據庫 CREATE database springcloud CHARACTER SET utf8 COLLATE utf8_general_ci;(2)創建tb_user用戶表
-- 使用springcloud數據庫 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 '創建時間',`updated` date DEFAULT NULL COMMENT '更新時間',`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; //創建時間private Date updated; //更新時間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實現類
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.測試結果
2.consumer_service
(1)創建過程
(2)導入依賴和目錄結構
<?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容器當中@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){//請求服務提供者的查詢用戶詳情地址// 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)測試結果
總結
- 上一篇: 十二生肖真正的老大是谁(十二生肖到底是怎
- 下一篇: 电脑无忧轻松学-五笔字型(五笔字型怎么学