创建springcloud父子工程遇到的问题
初学spring cloud,按照视频上的步骤创建eureka会出现maven异常和各种前所未见的错误,但是经过反复检查,除了导入的springboot依赖版本不一样之外,无别的不同,基于此,通过查找资料发现,springboot和springcloud版本存在着一些冲突,而目前比较新的springboot 2.2.1.RELEASE和springcloud的F版本应该有些冲突(本人猜想),而使用springcloud的Hoxton.RC2版本和springboot的2.2.x版本没有任何报错,但是RC2属于私服,他针对的是Java11,所以目前的jar包不全,经过我反复试验,发现eureka中的client相关的包没有,所以目前为止最好还是使用springboot 2.0.6.RELEASE和Finchley.RELEASE相结合的版本是最稳定的
基于此自己总结出一种野路子创建springcloud父子工程的方法,用于学习springcloud
首先创建父工程
new project
这里父工程使用spring Initializr,点击next
输入包名,父项目名,注意的是type点的是POM(这样父工程不会出现src,比较简洁,不容易出错),点击next
此时,什么都不需要选,继续点击next
到此步,检查文件所在位置是否正确,确认后点击finish
此时项目是这样的(看起来很简洁),然后修改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 https://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.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zz</groupId>
<artifactId>microservice_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice_parent</name>
<description>Demo project for Spring Boot</description>
<!--packageing需要指定为pom-->
<packaging>pom</packaging>
<!--子工程目录,每添加一个model,需要将子工程的model名注册到此-->
<modules>
<module>provider-user</module>
<module>consumer-order</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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!--让整个工程都是spring cloud-->
<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>
此时父工程创建完成,创建一个子model(提供者)
这里选择的也是spring Initializr,点击next,然后输入包名,工程名,注意的是这里的type是maven project,点击next
然后还是什么都不需要选,直接点击next,检查文件路径,确认无误后,点击finish
配置子model的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--将springboot的parent改成依赖父工程-->
<parent>
<groupId>com.zz</groupId>
<artifactId>microservice_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.zz</groupId>
<artifactId>provider-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-user</name>
<description>Demo project for Spring Boot</description>
<!--packageing指定为jar,子model都需指定成jar-->
<packaging>jar</packaging>
<dependencies>
<!--因为此项目需要访问前端,所以需要导入springboot的web组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
因为父子工程的依赖关系所以需要在父工程中的中添加相应的model
这里开始写业务代码,学到springcloud相信都是有一定的基础的,只列出基础逻辑,和代码,具体的不细说
创建了两个类,一个是pojo包中的User,一个是controller包中的UserController,然后需要配置application.yml
package com.zz.springcloud.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
public class User {
private Long id;
private Date date;
public User(Long id) {
this.id = id;
this.date=new Date();
}
}
package com.zz.springcloud.controller;
import com.zz.springcloud.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/user/{id}")
public User getUser(@PathVariable Long id){
return new User(id);
}
}
server:
port: 7900 #程序启动后的端口,也就是tomcat的端口
spring:
application:
name: provider-user #应用别名
点击ProviderUserApplication的类中的运行按钮,在网页中输入 http://localhost:7900/user/1
会显示出
此时,子工程的provider-user已经完成
继续创建consumer-order的model
和创建provider-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zz</groupId>
<artifactId>microservice_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.zz</groupId>
<artifactId>consumer-order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer-order</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
看结构图
其中User类和provider中的类保持一致,然后创建OrderController,最后配置application.yml
package com.zz.springcloud.controller;
import com.zz.springcloud.pojo.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;
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Value("${user.url}")
private String url;
@GetMapping("/order/{id}")
public User getOrder(@PathVariable Long id){
User user=restTemplate.getForObject(url+id,User.class);
return user;
}
}
server:
port: 8900
spring:
application:
name: consumer-order
user:
url: http://localhost:7900/user/
此时会发现restTemplate类没有找到,所以还需要把RestTemplate类注入到spring容器中,在ConsumerOrderApplication添加一个方法:
package com.zz.springcloud;
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 ConsumerOrderApplication {
@Bean
public RestTemplate getRest(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerOrderApplication.class, args);
}
}
此时测试consumer的时候需要先启动provider,也就是需要启动两个服务,输入http://localhost:8900/order/1
此时rest风格的分布式已经完成