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

时间:2021-09-24 03:19:54

1.创建一个spring cloud项目

1.1.使用工具创建--idea

点击creat new project,选择spring initializr

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

点击next,选择下一步

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

填入自己的GroupId和ArtifactId,

选择路径后,点击完成,

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

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

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

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

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

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

父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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zwjk.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-spring-cloud</name>
<description>Demo project for Spring Boot</description> <modules>
<module>microservice-provider-user</module>
<module>microservice-doscovey-eureka</module>
<module>microservice-consumer-movie</module>
</modules> <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>Greenwich.SR1</spring.cloud.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

user模块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>
<parent>
<groupId>com.zwjk.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>microservice-provider-user</artifactId>
<packaging>jar</packaging> <name>microservice-provider-user</name>
<description>Demo 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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies> </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:

 package com.zwjk.cloud.entity;

 import javax.persistence.*;
import java.math.BigDecimal; /**
* @author : Jixiaohu
* @Date : 2019-04-11.
* @Time : 9:07.
* @Description :
*/
@Entity
public class User { public User(Long id, String username) {
super();
this.id = id;
this.username = username;
} public User() {
super();
} @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id; @Column
private String username; @Column
private String name; @Column
private Short age; @Column
private BigDecimal balance; public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return this.username;
} public void setUsername(String username) {
this.username = username;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Short getAge() {
return this.age;
} public void setAge(Short age) {
this.age = age;
} public BigDecimal getBalance() {
return this.balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
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文件:

 <?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>com.zwjk.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>microservice-consumer-movie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-consumer-movie</name>
<description>Demo project for Spring Boot</description> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> </project>

yml配置文件:

 spring:
application:
name: microservice-consumer-movie
server:
port: 7901
user:
userServicePath: http://localhost:7900/simple/

User实体类:

 package com.zwjk.cloud.entity;

 import java.math.BigDecimal;

 /**
* @author : Jixiaohu
* @Date : 2019-04-11.
* @Time : 9:39.
* @Description :
*/
public class User {
private Long id; private String username; private String name; private Short age; 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 Short getAge() {
return age;
} public void setAge(Short age) {
this.age = age;
} public BigDecimal getBalance() {
return balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}

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:

 package com.zwjk.cloud;

 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 MicroserviceConsumerMovieApplication { @Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieApplication.class, args);
} }

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

http://localhost:7901/movie/2

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

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

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