专业的网站制作设计,深圳电商网站制作,站点建立网站的方法,除了凡科建站还有什么网站吗一、创建步骤
模拟开发过程中的服务间关系。抽象出来#xff0c;开发中的微服务之间的关系是生产者和消费者关系。
总目标#xff1a;模拟一个最简单的服务调用场景#xff0c;场景中保护微服务提供者(Producer)和微服务调用者(Consumer)#xff0c;方便后面使用微服务架…一、创建步骤
模拟开发过程中的服务间关系。抽象出来开发中的微服务之间的关系是生产者和消费者关系。
总目标模拟一个最简单的服务调用场景场景中保护微服务提供者(Producer)和微服务调用者(Consumer)方便后面使用微服务架构
注意每个微服务为一个独立的SpringBoot工程。
1创建一个新的Maven父工程 2添加起步依赖坐标
!--创建SpringBoot的父工程--
parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.1.6.RELEASE/version
/parent!--SpringBoot的依赖管理坐标--
dependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionGreenwich.SR1/versiontypepom/typescopeimport/scope/dependency/dependencies
/dependencyManagement二、在父工程下创建子模块
1.创建privider_service模块 目录结构 2.需要的依赖和配置文件application.yml
1依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringCloud_parent/artifactIdgroupIdcom.william/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdprivider_service/artifactIdpropertiesmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependencies!--web的依赖了--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--数据库的依赖--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency!--jpa依赖坐标--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependency/dependencies/project2配置文件application.yml
server:#服务的端口port: 9091
# DB 配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/springcloud?useUnicodetruecharacterEncodingUTF-8serverTimezoneUTCpassword: 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)
) ENGINEInnoDB AUTO_INCREMENT2 DEFAULT CHARSETutf8 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 {IdGeneratedValue(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层
1UserService接口
package com.william.service;import com.william.domain.User;public interface UserService {User findById(Integer id);}
2UserServiceImpl实现类
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) {OptionalUser 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 JpaRepositoryUser,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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringCloud_parent/artifactIdgroupIdcom.william/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdconsumer_service/artifactIdpropertiesmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--热部署依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactId/dependency/dependencies/project3application.yml server:port: 80804DemoApplication
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();}
}
5ConsumerController 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 urlhttp://localhost:9091/user/findById?idid;altenter 找到formatString url String.format(http://localhost:9091/user/findById?id%d, id);return restTemplate.getForObject(url,String.class);}}
6测试结果