Spring Cloud系列之初识(一)
初识
由于是开篇,先撇开springcloud的各种核心组件,来个demo 简单的认知下。话不多说直接开撸。
场景
简单场景如下图
编写服务提供者
microservice-simple-provider-user
- 引入pom依赖文件如下:数据库访问采用内嵌数据库H2来做 demo 的持久化结合JPA来搞。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kirago.sc</groupId>
<artifactId>microservice-simple-provider-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-simple-provider-user</name>
<description>microservice-simple-provider-user project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>mysql</groupId>-->
<!--<artifactId>mysql-connector-java</artifactId>-->
<!--<scope>runtime</scope>-->
<!--</dependency>-->
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project> - 服务提供者的yaml文件配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29server:
port: 8000
spring:
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: update
datasource:
platform: h2
schema: classpath:sql/schema.sql #数据scchema sql
data: classpath:sql/data.sql #数据初始化sql
url: jdbc:h2:mem:dbtest
username: root
password: root
driver-class-name: org.h2.Driver
h2:
console:
settings:
web-allow-others: true #开启web页面端
path: /h2 #uri路径
enabled: true #开启远端访问
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE - schema sql 如下:
1
2
3
4
5
6
7
8drop table user if exists;
create table user (
id bigint generated by default as identity,
username varchar(48),
name varchar(20),
age int(3),
balance decimal(10,2),
primary key (id)); - data.sql如下:
1
2
3insert into user (id, username, name, age, balance) values (1, 'account1', '张三', 20, 100.00);
insert into user (id, username, name, age, balance) values (2, 'account2', '李四', 21, 101.00);
insert into user (id, username, name, age, balance) values (3, 'account3', '王五', 22, 111.00); - 登录H2内存服务器结果显示如下:
- controller层如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package com.kirago.sc.microservicesimpleprovideruser.controller;
import com.kirago.sc.microservicesimpleprovideruser.entity.User;
import com.kirago.sc.microservicesimpleprovideruser.repository.UserRepository;
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.RestController;
import java.util.Optional;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/{id}")
public User findById(@PathVariable Long id){
Optional<User> user = userRepository.findById(id);
return user.get();
}
} - 实体类如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package com.kirago.sc.microservicesimpleprovideruser.entity;
import lombok.Data;
import javax.persistence.*;
import java.math.BigDecimal;
@Entity
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Integer age;
@Column
private BigDecimal balance;
} - Dao层:
1
2
3
4
5
6
7
8
9package com.kirago.sc.microservicesimpleprovideruser.repository;
import com.kirago.sc.microservicesimpleprovideruser.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
} - 通过postman 发送请求测试验证如下:
服务消费者
microservice-simple-consumer-movie
由于此部分内容比较简单就是构建好消费者需要获取的用户,直接构建了一个POJO,然后通过RestTemplate去请求服务提供者。
重点注意的就是 controller 层中的用法而已,如下:
1 | package com.kirago.sc.microservicesimpleconsumermovie.controller; |
此 demo 为了避免 userServiceUrl 的硬编码,通过 @Value 注解获取 yaml 配置文件的环境变量实现。
- 消费者 yaml 配置如下:
1
2
3
4
5user:
userServiceUrl: http://localhost:8000/
server:
port: 8001 - 通过 postman 发送请求验证如下:
示例代码链接
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 kirago杂谈!