从零开始学spring cloud(四) -------- 基础项目搭建

 

1.创建一个spring cloud项目

1.1.使用工具创建--idea

点击creat new project,选择spring initializr

点击next,选择下一步

填入自己的GroupId和ArtifactId,

选择路径后,点击完成,

由于需要开发的模块比较多,这边将建立多模块的项目。

 搭建完成后的项目结构如下图所示:

其中,一个用户模块,一个电影模块,电影模式是做为消费者的角色存在,用户模块提供用户信息

现在,直接使用硬编码,让movie模块,调用user模块接口,

先看一下user服务提供者代码:

 父pom文件(所有子模块的父模块的pom):

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.3.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.zwjk.cloud</groupId>
12     <artifactId>microservice-spring-cloud</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>microservice-spring-cloud</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <modules>
18         <module>microservice-provider-user</module>
19         <module>microservice-doscovey-eureka</module>
20         <module>microservice-consumer-movie</module>
21     </modules>
22 
23 
24     <properties>
25         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
27         <java.version>1.8</java.version>
28         <spring.cloud.version>Greenwich.SR1</spring.cloud.version>
29     </properties>
30 
31     <dependencyManagement>
32         <dependencies>
33             <dependency>
34                 <groupId>org.springframework.cloud</groupId>
35                 <artifactId>spring-cloud-dependencies</artifactId>
36                 <version>${spring.cloud.version}</version>
37                 <type>pom</type>
38                 <scope>import</scope>
39             </dependency>
40         </dependencies>
41     </dependencyManagement>
42 
43     <build>
44         <plugins>
45             <plugin>
46                 <groupId>org.springframework.boot</groupId>
47                 <artifactId>spring-boot-maven-plugin</artifactId>
48             </plugin>
49         </plugins>
50     </build>
51 
52 </project>

 

 

user模块pom文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.zwjk.cloud</groupId>
 7         <artifactId>microservice-spring-cloud</artifactId>
 8         <version>0.0.1-SNAPSHOT</version>
 9     </parent>
10 
11     <artifactId>microservice-provider-user</artifactId>
12     <packaging>jar</packaging>
13 
14     <name>microservice-provider-user</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-data-jpa</artifactId>
25         </dependency>
26         <dependency>
27             <groupId>org.springframework.boot</groupId>
28             <artifactId>spring-boot-starter-web</artifactId>
29         </dependency>
30         <dependency>
31             <groupId>com.h2database</groupId>
32             <artifactId>h2</artifactId>
33             <scope>runtime</scope>
34         </dependency>
35     </dependencies>
36 
37 </project>

 

user模块配置文件yml:

server:
  port: 7900
spring:
  jpa:
    generate-ddl: false
    show-sql: false
    hibernate:
      ddl-auto: none
  datasource:
    platform: h2
    schema: classpath:schema.sql
    data: classpath:data.sql
  application:
    name: microservice-provider-user
logging:
  level:
    root: info
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.zwjk: DEBUG

 

data.sql(放在yml同一级):

insert into user(id,username, name, age, balance) values(1,'user1', '张三', 20, 100.00);
insert into user(id,username, name, age, balance) values(2,'user2', '李四', 20, 100.00);
insert into user(id,username, name, age, balance) values(3,'user3', '王五', 20, 100.00);
insert into user(id,username, name, age, balance) values(4,'user4', '马六', 20, 100.00);

 

schema.sql(放在yml同一级):

drop table user if exists;
create table user(
    id bigint generated by default as identity,
    username varchar(40),
    name varchar(20),
    age int(3),
    balance decimal(10,2), 
    primary key(id)
);

 

User:

 1 package com.zwjk.cloud.entity;
 2 
 3 import javax.persistence.*;
 4 import java.math.BigDecimal;
 5 
 6 /**
 7  * @author : Jixiaohu
 8  * @Date : 2019-04-11.
 9  * @Time : 9:07.
10  * @Description :
11  */
12 @Entity
13 public class User {
14 
15     public User(Long id, String username) {
16         super();
17         this.id = id;
18         this.username = username;
19     }
20 
21     public User() {
22         super();
23     }
24 
25     @Id
26     @GeneratedValue(strategy = GenerationType.AUTO)
27     private Long id;
28 
29     @Column
30     private String username;
31 
32     @Column
33     private String name;
34 
35     @Column
36     private Short age;
37 
38     @Column
39     private BigDecimal balance;
40 
41     public Long getId() {
42         return this.id;
43     }
44 
45     public void setId(Long id) {
46         this.id = id;
47     }
48 
49     public String getUsername() {
50         return this.username;
51     }
52 
53     public void setUsername(String username) {
54         this.username = username;
55     }
56 
57     public String getName() {
58         return this.name;
59     }
60 
61     public void setName(String name) {
62         this.name = name;
63     }
64 
65     public Short getAge() {
66         return this.age;
67     }
68 
69     public void setAge(Short age) {
70         this.age = age;
71     }
72 
73     public BigDecimal getBalance() {
74         return this.balance;
75     }
76 
77     public void setBalance(BigDecimal balance) {
78         this.balance = balance;
79     }
80 }

 

UserRepository:
package com.zwjk.cloud.repository;

import com.zwjk.cloud.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    User getUserById(Long id);
}

Controller:

package com.zwjk.cloud.controller;

import com.zwjk.cloud.entity.User;
import com.zwjk.cloud.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 : Jixiaohu
 * @Date : 2019-04-11.
 * @Time : 9:08.
 * @Description :
 */
@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/simple/{id}")
    public User findById(@PathVariable Long id) {
        return this.userRepository.getUserById(id);
    }
}

 

启动类:

package com.zwjk.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MicroserviceProviderUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceProviderUserApplication.class, args);
    }
}

 

启动项目后,通过浏览器访问http://localhost:7900/simple/1可以得到相应的user

{"id":1,"username":"user1","name":"张三","age":20,"balance":100.00}

 

 到这里,一个用户消息提供者已经开发完成。

下面搭建电影消费者模块:

movie模块pom文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <parent>
 7         <groupId>com.zwjk.cloud</groupId>
 8         <artifactId>microservice-spring-cloud</artifactId>
 9         <version>0.0.1-SNAPSHOT</version>
10     </parent>
11 
12 
13     <artifactId>microservice-consumer-movie</artifactId>
14     <version>0.0.1-SNAPSHOT</version>
15     <name>microservice-consumer-movie</name>
16     <description>Demo project for Spring Boot</description>
17 
18 
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-web</artifactId>
23         </dependency>
24     </dependencies>
25 
26 
27 </project>

 

yml配置文件:

1 spring:
2   application:
3     name: microservice-consumer-movie
4 server:
5   port: 7901
6 user:
7   userServicePath: http://localhost:7900/simple/

 

User实体类:

 1 package com.zwjk.cloud.entity;
 2 
 3 import java.math.BigDecimal;
 4 
 5 /**
 6  * @author : Jixiaohu
 7  * @Date : 2019-04-11.
 8  * @Time : 9:39.
 9  * @Description :
10  */
11 public class User {
12     private Long id;
13 
14     private String username;
15 
16     private String name;
17 
18     private Short age;
19 
20     private BigDecimal balance;
21 
22     public Long getId() {
23         return id;
24     }
25 
26     public void setId(Long id) {
27         this.id = id;
28     }
29 
30     public String getUsername() {
31         return username;
32     }
33 
34     public void setUsername(String username) {
35         this.username = username;
36     }
37 
38     public String getName() {
39         return name;
40     }
41 
42     public void setName(String name) {
43         this.name = name;
44     }
45 
46     public Short getAge() {
47         return age;
48     }
49 
50     public void setAge(Short age) {
51         this.age = age;
52     }
53 
54     public BigDecimal getBalance() {
55         return balance;
56     }
57 
58     public void setBalance(BigDecimal balance) {
59         this.balance = balance;
60     }
61 }

 

 MovieController:

package com.zwjk.cloud.controller;

import com.zwjk.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 : Jixiaohu
 * @Date : 2019-04-11.
 * @Time : 9:38.
 * @Description :
 */
@RestController
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${user.userServicePath}")
    private String userServicePath;

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
        return this.restTemplate.getForObject(this.userServicePath + id, User.class);
    }
}

 

 MicroserviceConsumerMovieApplication:

 1 package com.zwjk.cloud;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.web.client.RestTemplate;
 7 
 8 @SpringBootApplication
 9 public class MicroserviceConsumerMovieApplication {
10 
11     @Bean
12     public RestTemplate restTemplate() {
13         return new RestTemplate();
14     }
15 
16     public static void main(String[] args) {
17         SpringApplication.run(MicroserviceConsumerMovieApplication.class, args);
18     }
19 
20 }

 

启动项目后,可以通过访问浏览器的

http://localhost:7901/movie/2

可以获得user消息提供用户信息

{"id":2,"username":"user2","name":"李四","age":20,"balance":100.00} 

这里,实现了服务之间的调用

 

posted on 2019-04-11 09:59  小~虎  阅读(555)  评论(2编辑  收藏

导航