从零到一搭建一个SpringCloud2.0项目

 首先创建一个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

在这里插入图片描述
访问接口:
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值