模拟微服务业务场景

一、创建步骤

模拟开发过程中的服务间关系。抽象出来,开发中的微服务之间的关系是生产者和消费者关系。

总目标:模拟一个最简单的服务调用场景,场景中保护微服务提供者(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: root

3.数据库的准备

(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)测试结果

在这里插入图片描述

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

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

相关文章

Linux 命令之 groupmod -- 更改群组识别码或名称

文章目录命令介绍常用选项参考示例命令介绍 groupmod 命令用于更改群组的识别码或名称时。不过大家还是要注意&#xff0c;用户名不要随意修改&#xff0c;组名和 GID 也不要随意修改&#xff0c;因为非常容易导致管理员逻辑混乱。如果非要修改用户名或组名&#xff0c;则建议…

cassandra使用心得_使用Spring Data Cassandra缓存的预备语句

cassandra使用心得今天&#xff0c;我有一篇简短的文章&#xff0c;内容涉及在Spring Data Cassandra中使用Prepared Statements。 Spring为您提供了一些实用程序&#xff0c;使您可以更轻松地使用“预备语句”&#xff0c;而不是依靠自己使用Datastax Java驱动程序手动注册查询…

注册中心 Spring Cloud Eureka

一、搭建eureka-server工程 目录结构 二、导入依赖 <?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…

Linux 用户(user)和用户组(group)管理概述

文章目录一、理解Linux的单用户多任务&#xff0c;多用户多任务概念二、用户(user&#xff09;和用户组&#xff08;group&#xff09;概念三、用户&#xff08;user&#xff09;和用户组&#xff08;group&#xff09;相关的配置文件、命令或目录一、理解Linux的单用户多任务&…

mockito java_Java:使用Mockito模拟ResultSet

mockito java这篇文章展示了如何使用Mockito模拟java.sql.ResultSet 。 它可用于帮助对ResultSet进行操作的单元测试代码&#xff08;例如ResultSetExtractor &#xff09;而无需依赖外部数据源。 您可以通过提供列名列表和2D数据数组来创建MockResultSet 。 例如&#xff1a;…

服务中心Eureka

一、服务消费者-注册服务中心 测试结果 二、消费者通过Eureka访问提供者 测试结果 Consumer ConsumerController package com.william.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance;…

Linux 命令之 newgrp -- 登入另一个群组

文章目录命令介绍常用选项参考示例切换登录用户组命令介绍 newgrp 命令的英文全称为“new group”&#xff0c;使用该命令指定用户组名称&#xff0c;执行命令后&#xff0c;其实是以相同的用户名&#xff0c;但是以另一个群组名称&#xff0c;再次登入系统。所以本质上是切换…

pivotal_Spring Data Pivotal Gemfire教程

pivotal1. Spring Data Pivotal Gemfire –简介 在这篇文章中&#xff0c;我们将介绍有关Spring Data Pivotal Gemfire的全面教程。 Pivotal Gemfire是由Apache Geode支持的内存中数据网格解决方案。 使用Pivotal Gemfire构建的应用程序使您可以在分布式服务器节点之间轻松扩展…

Linux命令之 chsh -- 用来更换登录系统时使用的shell

文章目录命令简介常用选项参考示例查看系统安装了哪些shell的两种方法查看当前正在使用的 shell修改当前登录用户的shell命令简介 chsh 命令用来更换登录系统时使用的shell。若不指定任何参数与用户名称&#xff0c;则 chsh 会以应答的方式进行设置。 chsh 用于更改登录 shell…

layui绑定json_JSON-B非对称属性绑定

layui绑定jsonJSON-B规范定义了诸如JsonbProperty或JsonbTransient类的绑定注释&#xff0c;以声明方式将Java对象映射到JSON&#xff0c;然后又映射回JSON。 这些注释可以“非对称地”用于定义序列化和反序列化的不同处理。 如果在Java属性上或在getter和setter上都注释了JSO…

熔断器

一、引入依赖 1.consumer_service中加入依赖 <!--开启熔断器--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>2.开启熔断的注解 //注…

Linux 命令之 pwck -- 用来验证系统认证文件内容和格式的完整性

文章目录命令介绍常用选项参考示例检验用户配置文件 /etc/passwd和/etc/shadow的内容是否合法、完整命令介绍 pwck 命令用来校验用户配置文件 /etc/passwd 和 /etc/shadow 内容是否合法或完整 常用选项 选项说明-q仅报告错误信息-s以用户id排序文件“/etc/passwd”和“/etc/…

不停机与停机更新_Istio的零停机滚动更新

不停机与停机更新本系列文章的第一部分介绍了如何在Kubernetes集群中实现真正的零停机时间更新。 我们专门解决了将流量从旧实例切换到新实例时出现的请求失败。 本文将展示如何使用Istio群集实现相同的目标。 服务网格技术&#xff08;例如Istio&#xff09;通常与容器编排结…

远程调用 Spring Cloud Feign

一、 Feign简介 Feign [feɪn] 译文 伪装。Feign是一个声明式WebService客户端.使用Feign能让编写WebService客户端更加简单,它的使用方法是定义一个接口&#xff0c;然后在上面添加注解。不再需要拼接URL&#xff0c;参数等操作。项目主页&#xff1a;https://github.com/Ope…

Linux 命令之 pwconv -- 开启用户的投影密码

命令介绍 该命令根据文件 /etc/passwd 创建影子文件 /etc/shadow。 用来开启用户的投影密码。Linux系统里的用户和群组密码&#xff0c;本来分别存放在名称为 passwd 和 group 的文件中&#xff0c; 这两个文件位于 /etc 目录下。因系统运作所需&#xff0c;任何人都得以读取…

plsql如何执行单个语句_在单个try-with-resources语句中仔细指定多个资源

plsql如何执行单个语句Java 7最有用的新功能之一是引入了try-with-resources语句 [AKA 自动资源管理 &#xff08; ARM &#xff09;]。 try-with-resources语句的吸引力在于它承诺 “确保在语句末尾关闭每个资源”。 在这种情况下&#xff0c;“资源”是实现AutoCloseable及其…

Spring Cloud Feign 负载均衡

一、Feign负载均衡介绍 Feign本身集成了Ribbon依赖和自动配置&#xff0c;因此不需要额外引入依赖&#xff0c;也不需要再注册RestTemplate对象 Feign内置的ribbon默认设置了请求超时时长&#xff0c;默认是1000&#xff0c;可以修改 ribbon内部有重试机制&#xff0c;一旦超…

Linux 命令之 pwunconv -- 关闭投影密码

文章目录命令介绍命令介绍 pwunconv 命令与 pwconv 功能相反&#xff0c;用来关闭用户的投影密码。它会将密码从 /etc/shadow 文件内&#xff0c;重新回存到 /etc/passwd 文件中。换句话说&#xff0c;pwunconv 命令将文件 /etc/shadow 中的数据同步到文件 /etc/passwd 中&…

apache ignite_Apache Ignite,Hazelcast,Cassandra和Tarantool之间的主要区别

apache igniteApache Ignite在世界范围内得到广泛使用&#xff0c;并且一直在增长。 诸如Barclays&#xff0c;Misys&#xff0c;Sberbank&#xff08;欧洲第三大银行&#xff09;&#xff0c;ING&#xff0c;JacTravel之类的公司都使用Ignite来增强其架构的功能&#xff0c;这…

Spring Cloud Feign 熔断器支持

一、实现步骤 在配置文件application.yml中开启feign熔断器支持编写FallBack处理类&#xff0c;实现FeignClient客户端在FeignClient注解中&#xff0c;指定FallBack处理类。测试 1. 在配置文件application.yml中开启feign熔断器支持&#xff1a;默认关闭 # 开启Feign的熔断功…