关键词:
一、SpringCloud的简介
微服务是一种架构方式,最终肯定需要技术架构去实施。
微服务的实现方式很多,但是最火的莫过于Spring Cloud了。为什么?
- 后台硬:作为Spring家族的一员,有整个Spring全家桶靠山,背景十分强大。
- 技术强: Spring作为Java领域的前辈,可以说是功力深厚。有强力的技术团队支撑,一般人还真比不了
- 群众基础好:可以说大多数程序员的成长都伴随着Spring框架,试问:现在有几家公司开发不用Spring?SpringCloud与Spring的各个框架无缝整合,对大家来说一切都是熟悉的配方,熟悉的味道。
- 使用方便:相信大家都体会到了SpringBoot给我们开发带来的便利,而SpringCloud完全支持SpringBoot的开发,用很少的配置就能完成微服务框架的搭建
SpringCloud快速入门:
SpringCloud的官网:
https://spring.io/projects/spring-cloud
SpringCloud的版本
SpringCloud是Spring旗下的项目之一,
最擅长的就是集成,把世界上最好的框架拿过来,集成到自己的项目中。
SpringCloud也是一样,它将现在非常流行的一些技术整合到一起,实现了诸如:配置管理,服务发现,智能路由,负载均衡,熔断器,控制总线,集群状态等等功能。
其主要涉及的组件包括:
Netflix
- Eureka:注册中心
- zuul:服务网关
- Ribbon:负载均衡
- Feign:服务调用
- Hystix:熔断器
以上只是其中一部分,架构图:
二、微服务场景模拟
首先,我们需要模拟一个服务调用的场景。方便后面学习微服务框架。
1、创建父工程
微服务中需要同时创建多个项目,为了方便课堂演示,我们先创建一个父工程,然后后续的工程都以这个工程为父,实现maven的聚合。这样可以在一个窗口看到所有工程,方便我们讲解。
在实际开发中,应该是每个微服务独立一个工程。
(1)创建cloud-demo
(2)修改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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itzheng.demo</groupId>
<artifactId>cloud-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<mapper.starter.version>2.0.3</mapper.starter.version>
<mysql.version>5.1.32</mysql.version>
<pageHelper.starter.version>1.2.5</pageHelper.starter.version>
</properties>
<dependencyManagement>
<dependencies>
<!--springCloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>$spring-cloud.version</version>
<type>pom</type>
</dependency>
<!--通用Mapper启动器-->
<dependency>
<groupId>tk.mapper</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>$mapper.starter.version</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>$mysql.version</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、创建服务提供者
我们新建一个项目,对外提供查询用户的服务
(1)创建module
选中父工程:cloud-demo
(2)完善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>cloud-demo</artifactId>
<groupId>com.itzheng.demo</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
</project>
3、创建服务提供者:编写代码
(1)属性文件:这里我们采用了yaml语法,而不是properties
server:
port: 8082
spring:
datasource:
url: jdbc:mysql://localhost:3306/itzheng
username: root
password: root
mybatis:
type-aliases-package: com.itzheng.user.pojo
(2)创建对应的包结构
(3)创建启动类
package com.itzheng;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.itzheng.user.mapper")
public class UserApplication
(4)创建com.itzheng.user.mapper包
(5)完善UserApplication类
package com.itzheng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.itzheng.user.mapper")
public class UserApplication
public static void main(String[] args)
SpringApplication.run(UserApplication.class);
(6)创建User实体类
package com.itzheng.user.pojo;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Table(name="tb_user")
@Data
public class User
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
private 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; //备注
(7)引入插件
(8)创建通用Mapper即UserMapper继承Mapper
package com.itzheng.user.mapper;
import com.itzheng.user.pojo.User;
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User>
(9)创建Service。创建UserService
package com.itzheng.user.Service;
import com.itzheng.user.mapper.UserMapper;
import com.itzheng.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService
@Autowired
private UserMapper userMapper;
public User queryById(Long id)
return userMapper.selectByPrimaryKey(id);
(10)增加一个对外查询的接口:
package com.itzheng.user.web;
import com.itzheng.user.Service.UserService;
import com.itzheng.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserController
@Autowired
private UserService userService;
@GetMapping("/id")
public User queryById(@PathVariable("id") Long id)
return userService.queryById(id);
(11)运行测试项目
访问项目:http://localhost:8082/user/8
"id":8,
"userName":"zhangsan",
"password":"123",
"name":"zhangsan123",
"age":12,
"sex":1,
"birthday":"2021-06-13T16:00:00.000+0000",
"created":"2021-06-29T10:21:50.000+0000",
"updated":"2021-06-30T10:21:53.000+0000",
"note":"1"
4、服务的调用放:代码编写
(1)创建项目工程
(2)在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>cloud-demo</artifactId>
<groupId>com.itzheng.demo</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer-demo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
(3)编写启动类
package com.itzheng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ConsumerApplication
@Bean
public RestTemplate restTemplate()
return new RestTemplate();
public static void main(String[] args)
SpringApplication.run(ConsumerApplication.class,args);
(4)编写实体类
package com.itzheng.consumer.web;
import com.itzheng.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("consumer")
public class ConsumerController
@Autowired
private RestTemplate restTemplate;
@GetMapping("/id")
public User queryById(@PathVariable("id") Long id )
String url = "http://localhost:8082/user/"+id;
User user = restTemplate.getForObject(url,User.class);
return user;
(5)打开Run Dashboard
查看详情
java微服务之springcloud快速入门day01eureka注册中心快速入门(代码片段)
一、Eureka注册中心简介1、认识Eureka首先我们来解决第一问题,服务的管理。问题分析在刚才的案例中,user-service对外提供服务,需要对外暴露自己的地址。而consumer(调用者)需要记录服务提供者的地址。将来地址出现... 查看详情
java微服务之springcloud快速入门day02zuul网关,面向服务的路由,zuul过滤器(代码片段)
Java微服务之SpringCloud快速入门day02(三)Zuul网关一、简介1、相关概念官网:https://github.com/Netflix/zuulZuul:维基百科:电影《捉鬼敢死队》中的怪兽,Zuul,在纽约引发了巨大强乱。事实上,在微服务架构中... 查看详情
java微服务之springcloud快速入门day02zuul网关,面向服务的路由,zuul过滤器(代码片段)
Java微服务之SpringCloud快速入门day02(三)Zuul网关一、简介1、相关概念官网:https://github.com/Netflix/zuulZuul:维基百科:电影《捉鬼敢死队》中的怪兽,Zuul,在纽约引发了巨大强乱。事实上,在微服务架构中... 查看详情
java微服务之springcloud快速入门day02hystrix线程隔离,服务降级(代码片段)
Hystrix1、简介Hystrix,英文意思是豪猪,全身都是刺,看起来就不好惹,是一种保护机制。Hystrix也是Netflix公司的一款组件。主页:https://github.com/Netflix/Hystrix那么Hystrix的作用是什么呢?具体要保护什么呢ÿ... 查看详情
java微服务之springcloud快速入门day02hystrix线程隔离,服务降级(代码片段)
Hystrix1、简介Hystrix,英文意思是豪猪,全身都是刺,看起来就不好惹,是一种保护机制。Hystrix也是Netflix公司的一款组件。主页:https://github.com/Netflix/Hystrix那么Hystrix的作用是什么呢?具体要保护什么呢ÿ... 查看详情
java微服务之springcloud快速入门day01系统架构的演变,服务调用方式(代码片段)
一、系统架构的演变1、集中式架构当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时,用于简化增删改查工作量的数据访问框架(ORM)是影响项目开发的关键。存在的问题&... 查看详情
java微服务之springcloud快速入门day01eureka注册中心高级部分(集群)(高可用)(代码片段)
接下来我们详细讲解Eureka的原理及配置。1、基础框架Eureka架构中的三个核心角色:服务注册中心Eureka的服务端应用,提供服务注册和发现功能,就是刚刚我们建立的eureka-server服务提供者提供服务的应用,可以是SpringBoo... 查看详情
java微服务之springcloud快速入门day01eureka注册中心高级部分(集群)(高可用)(代码片段)
接下来我们详细讲解Eureka的原理及配置。1、基础框架Eureka架构中的三个核心角色:服务注册中心Eureka的服务端应用,提供服务注册和发现功能,就是刚刚我们建立的eureka-server服务提供者提供服务的应用,可以是SpringBoo... 查看详情
springcloud微服务开发快速入门(代码片段)
...变6.微服务开发框架7.微服务需要学习什么?小结二、SpringCloud1.SpringCloud介绍2.SpringBoot与SpringCloud的区别3.服务拆分和远程调用实现远程调用案例案例需求注册RestTemplate实现远程 查看详情
springcloud微服务开发快速入门(代码片段)
...变6.微服务开发框架7.微服务需要学习什么?小结二、SpringCloud1.SpringCloud介绍2.SpringBoot与SpringCloud的区别3.服务拆分和远程调用实现远程调用案例案例需求注册RestTemplate实现远程 查看详情
java之springcloud微服务搭建(第一个阶段)springboot项目实现商品服务器端是调用(代码片段)
Java之SpringCloud微服务入门到精通Spring微服务快速入门(Eureka)一、Springcloud微服务概述1、微服务中的相关概念(1)服务注册与发现(2)负载均衡(3)熔断(4)链路追踪(5)API网关2... 查看详情
springcloud系列教程汇总整理手册
...系列之集成Dubbo的方式 >>sourcedownload2、微服务之SpringCloud2.1服务治理实现SpringCloud系列使用NetflixEureka进行服务治理2.2声明式服务调用SpringCloud系列之声明式服务调用NetflixFeign2.3客户端负载均衡SpringCloud系列之客户端负载均... 查看详情
springcloud微服务快速教程之gateway服务网关
...,是异步非阻塞网关,ZUUL2也是异步非阻塞的,但未纳入springcloud整合计划 基于WebFlux ,与spring-boot-starter-web冲突,要排除该依赖;ZUUL1是阻塞io的APIGateway; 性能上,自然是异步非阻塞的gateway胜出,不过实际项目中,... 查看详情
springcloud之zuul网关入门
SpringCloud实现微服务的架构基本成型: 使用SpringCloudNetflix中的Eureka实现了服务注册中心以及服务注册与发现;而服务间通过Ribbon或Feign实现服务的消费以及均衡负载。 为了使得服务集群更为健壮,使用Hystrix的融断机制来... 查看详情
springcloud实战微服务之——微服务简介以及入门使用(代码片段)
微服务概述微服务是什么?微服务解决了什么问题?微服务有什么特点?单体架构是什么?一个归档包包含了应用所有功能的应用程序,我们通常称之为单体应用。架构单体应用的架构风格,我们称之为单... 查看详情
springcloud从入门到进阶——使用springboot搭建微服务
内容 SpringBoot整合SpringCloud的Eureka、Zuul等组件,快速实现简单易懂且具有服务熔断、负载均衡的分布式架构1.0,体验微服务的魅力。版本 IDE:IDEA2017.2.2x64 JDK:1.8.0_171 manve:3.3.3 SpringBoot:1.5.9.RELEASE SpringCloud... 查看详情
springcloud微服务之跨服务调用后端接口
SpringCloud微服务系列博客:SpringCloud微服务之快速搭建EurekaServer:https://blog.csdn.net/egg1996911/article/details/78787540SpringCloud微服务之注册服务至EurekaServer:https://blog.csdn.net/egg1996911/article/details/78859200Sp 查看详情