首先创建一个Maven项目:
配置pom.xml:
<dependencyManagement>
<dependencies>
<!-- Spring cloud 依赖管理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>runtime</scope>
</dependency>
<!-- Spring boot 依赖管理 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
添加上面的代码,dependencyManagement用于依赖管理,其中定义了许多常用包的版本号,这些包可以在引入dependency时不填写版本号。使用默认配置的好处在于不用花时间解决版本冲突的问题。
那么在哪里能看到定义的默认版本号呢?
首先找到spring-boot-dependencies在你本地maven仓库的位置:
spring-boot-dependencies-2.1.5.RELEASE.pom中定义了版本号等一些配置:
在项目的pom.xml中引入spring-boot-starter-web:
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
在项目的pom.xml中引入Feign、Hystrix、Eureka:
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
这里可以不用定义版本号,使用缺省值。
完整的pom.xml配置:
<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.yl</groupId>
<artifactId>demo-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- Spring cloud 依赖管理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring boot 依赖管理 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
SpringBoot项目启动类:
package com.yl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CloudApplication {
public static void main(String[] args) {
SpringApplication.run(CloudApplication.class, args);
}
}
SpringCloud项目可以使用@SpringCloudApplication,但是需要注意,使用@SpringCloudApplication时需要配置Feign、Hystrix和Eureka,不然会报错。
至于Feign、Hystrix和Eureka,他们都是SpringCloud生态中的一部分,分别运用在微服务之间接口调用、服务降级、服务发现与注册中,在后续文章中会一一进行讲解。
新建一个controller,注意包位置,SpringBoot项目默认会扫描启动类包路径的子路径中的Bean。
这里放出项目结构图:
在TestController中开发一个最基本的接口:
package com.yl.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/hello")
public String doTest() {
return "hello word!";
}
}
在application.properties中设置项目端口:
server.port=8080
访问接口: