首先,我们需要模拟一个服务调用的场景。方便后面学习微服务架构
5.1.创建父工程
编写项目信息
编写项目保存的位置
pom.xml文件
- 注意父工程的pom文件的打包方式是 pom
<?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>cn.clocktt.demo</groupId>
<artifactId>SpringCloudDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>userservice</module>
<module>user-service</module>
<module>user-consumer</module>
<module>eurekaserver</module>
</modules>
<!--定义该工程的打包方式为pom-->
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</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.RC1</spring-cloud.version>
<mapper.starter.version>2.0.2</mapper.starter.version>
<mysql.version>5.1.32</mysql.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--这是一个pom文件,来管理springcloud中的各个组件的版本-->
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 通用Mapper启动器 -->
<dependency>
<groupId>tk.mybatis</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>
<!--
dependencyManagement和dependencies的区别
dependencyManagement:子工程需要手动去引入对应的dependency才会有依赖
dependencies:所有子工程都有该依赖
-->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
dependencyManagement和dependencies的区别
dependencyManagement:子工程需要手动去引入对应的dependency才会有依赖
dependencies:所有子工程都有该依赖
创建子工程
5.2.服务提供者
我们新建一个项目,对外提供查询用户的服务。
5.2.1.创建module
右键父工程,选择module
选择maven工程
填写子工程名
编辑子工程保存的路径,子工程的路径一定要在父工程目录的下级
5.2.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>SpringCloudDemo</artifactId>
<groupId>cn.clocktt.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-service</artifactId>
<dependencies>
<!--springweb依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
5.1.2.编写代码
Controller
添加一个对外查询的接口:
package cn.clocktt.controller;
import cn.clocktt.pojo.User;
import cn.clocktt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/{id}")
public User queryUserById(@PathVariable Long id){
User user = userService.queryUserById(id);
return user;
}
}
Service
package cn.clocktt.service;
import cn.clocktt.mapper.UserMapper;
import cn.clocktt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryUserById(Long id){
User user = userMapper.selectByPrimaryKey(id);
return user;
}
}
mapper
package cn.clocktt.mapper;
import cn.clocktt.pojo.User;
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User> {
}
pojo
package cn.clocktt.pojo;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
@Table(name = "tb_user")
@Data
//如果要使用@Data,那么需要导入包和安装lombok插件
public class User implements Serializable {
@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; //备注
}
属性文件 application.yml
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/leyou
username: root
password: root
mybatis:
type-aliases-package: cn.clocktt.pojo
启动类
package cn.clocktt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("cn.clocktt.mapper")
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class,args);
}
}
user-service 项目结构
5.1.3.启动并测试:
启动项目,访问接口:http://localhost:8081/user/1
5.2.服务调用者
5.2.1.创建工程
与上面类似,这里不再赘述,需要注意的是,我们调用user-service的功能,因此不需要mybatis相关依赖了。
pom
<?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>SpringCloudDemo</artifactId>
<groupId>cn.clocktt.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
5.2.2.编写代码
首先在启动类中注册RestTemplate
:
package cn.clocktt;
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 UserConsumerApplication {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(UserConsumerApplication.class,args);
}
}
Controller
package cn.clocktt.controller;
import cn.clocktt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
@Controller
public class WebController {
@Autowired
private RestTemplate restTemplate;
@ResponseBody
@RequestMapping("/consumer/user")
public User queryUserById(long id){
User user = restTemplate.getForObject("http://localhost:8081/user/" + id, User.class);
return user;
}
}
pojo
package cn.clocktt.pojo;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
//如果要使用@Data,那么需要导入包和安装lombok插件
public class User implements Serializable {
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; //备注
}
application.yml
server:
port: 8082
5.2.3.启动测试:
我们访问:http://localhost:8082/consumer/user?id=1
一个简单的远程服务调用案例就实现了。
项目整体结构
5.3.有没有问题?
简单回顾一下,刚才我们写了什么:
- use-service:一个提供根据id查询用户的微服务
- consumer:一个服务调用者,通过RestTemplate远程调用user-service
流程如下:
存在什么问题?
- 在consumer中,我们把url地址硬编码到了代码中,不方便后期维护
- consumer需要记忆user-service的地址,如果出现变更,可能得不到通知,地址将失效
- consumer不清楚user-service的状态,服务宕机也不知道
- user-service只有1台服务,不具备高可用性
- 即便user-service形成集群,consumer还需自己实现负载均衡
其实上面说的问题,概括一下就是分布式服务必然要面临的问题:
- 服务管理
- 如何自动注册和发现
- 如何实现状态监管
- 如何实现动态路由
- 服务如何实现负载均衡
- 服务如何解决容灾问题
- 服务如何实现统一配置
以上的问题,我们都将在SpringCloud下一章中得到答案。