说明

由于springclound基于Springboot,因此第一篇熟悉一下Springboot。
这里为了后面的演示,创建一个project,名为microservice-spring-cloud
创建2个子模块:

  • microservice-springcloud-user
    该模块提供用户查询功能
  • microservice-springcloud-movie
    该模块是电影模块,从microservice-springcloud-user模块查询用户信息。

microservice-spring-cloud

pom.xml

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
<?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.tommy.springcloud</groupId>
<artifactId>microserver-spring-cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>microservice-springcloud-user</module>
<module>microservice-springcloud-movie</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

microservice-springcloud-user模块

该模块使用Springboot,数据库使用h2内存数据库,数据库框架使用jpa。

application.yml:

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
server:
port: 7902

spring:
application:
name: microservice-springcloud-user
jpa:
generate-ddl: false
# 显示SQL
show-sql: true
format_sql: true
hibernate:
ddl-auto: none
datasource:
platform: h2
# 指定DDL脚本
schema: classpath:schema.sql
# 指定数据初始化脚本
data: classpath:data.sql


loggin:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
com.tommy: DEBUG

schema.sql:

1
2
DROP TABLE USER IF EXISTS ;
create table USER(id bigint generated BY DEFAULT AS IDENTITY ,username VARCHAR(32),NAME VARCHAR (20),balance DECIMAL(10,2),PRIMARY KEY (id));

data.sql:

1
2
3
4
INSERT INTO USER(ID,username,NAME ,balance) VALUES (1,'user1','张三',100.00);
INSERT INTO USER(ID,username,NAME ,balance) VALUES (2,'user2','李四',100.00);
INSERT INTO USER(ID,username,NAME ,balance) VALUES (3,'user3','王五',100.00);
INSERT INTO USER(ID,username,NAME ,balance) VALUES (4,'user4','马六',100.00);

pom.xml

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
<?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.tommy.cloud</groupId>
<artifactId>microservice-sample-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>microservice-sample-provider</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
</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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>

用户实体类:

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
package com.tommy.springcloud.entity;

import javax.persistence.*;
import java.math.BigDecimal;

/**
* @author j.tommy
* @version 1.0
* @date 2018/1/24
*/
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private BigDecimal balance;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}

数据库操作类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.tommy.springcloud.repository;

import com.tommy.springcloud.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
* @author j.tommy
* @version 1.0
* @date 2018/1/24
*/
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}

对外提供服务的Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.tommy.springcloud.controller;
import com.tommy.springcloud.entity.User;
import com.tommy.springcloud.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;
/**
* @author j.tommy
* @version 1.0
* @date 2018/1/24
*/
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/sample/{id}")
public User findById(@PathVariable Long id) {
return this.userRepository.findOne(id);
}
}

应用启动类:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.tommy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author Administrator
*/
@SpringBootApplication
public class MicroserviceUserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceUserProviderApplication.class, args);
}
}

启动后,通过http://localhost:7902/sample/1,即可查询用户id为1的用户信息。

microservice-springcloud-movie模块

电影模块,从microservice-springcloud-user模块查询用户信息。

pom.xml

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
<?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.tommy.cloud</groupId>
<artifactId>microservice-springcloud-movie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>microservice-springcloud-movie</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>

application.yml:

1
2
server:
port: 7901

用户实体类:

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
package com.tommy.cloud.microservicemovie.entity;
import java.math.BigDecimal;
/**
* @author j.tommy
* @version 1.0
* @date 2018/1/24
*/
public class User {
private Long id;
private String username;
private String name;
private BigDecimal balance;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}

MovieController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.tommy.cloud.microservicemovie.controller;
import com.tommy.cloud.microservicemovie.entity.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.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author j.tommy
* @version 1.0
* @date 2018/1/24
*/
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/movie/{id}")
public User findUserById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://localhost:7902/sample/" + id, User.class);
}
}

应用启动类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.tommy.cloud.microservicemovie;
import org.springframework.beans.factory.annotation.Autowired;
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 MicroserviceSampleConsumerApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MicroserviceSampleConsumerApplication.class, args);
}
}

启动后,通过http://localhost:7901/movie/1,即可查询用户id为1的用户信息。
至此,使用Springboot搭建的一个服务提供者和服务消费者模块就完成了。