Java 微服务之 SpringCloud快速入门day01 初始SpringCloud
Posted 蓝盒子bluebox
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 微服务之 SpringCloud快速入门day01 初始SpringCloud相关的知识,希望对你有一定的参考价值。
一、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快速入门day01 初始SpringCloud
Java 微服务之 SpringCloud快速入门day01 初始SpringCloud
Java 微服务之 SpringCloud快速入门day01 Eureka注册中心快速入门
Java 微服务之 SpringCloud快速入门day01 Eureka注册中心快速入门