使用Idea和maven工具构建SpringCloud项目

本文详细介绍了如何使用IntelliJ IDEA(Idea)和Maven手动构建Spring Cloud项目,强调手动创建能加深对多模块应用设计的理解。首先,创建主工程并引入通用依赖。接着,构建服务注册中心,包括创建新模块、配置依赖及编写启动类。然后,演示如何构建一个服务,并确保其成功注册到Eureka。最后,作者指出手动构建后的理解有助于使用Spring Initializer快速创建项目。
摘要由CSDN通过智能技术生成

构建方式

本文介绍使用maven手动构建Spring Cloud 项目,需要说明的是,使用Idea提供的Spring Initializer建立会更加方便,但是手动创建更有利于加深对多模块化应用设计和结构的理解,以及对工程与模块之间,模块与模块之间关系的理解。在此基础上再利用Spring Initializer创建则会简单的多。

正文

  1. 创建主工程
    创建一个空maven项目SpringCloudDemoByMaven
    创建后得到相应目录结构,因为我们不会在主项目中编程,可以将主项目中的src目录删除,使得目录结构更加清晰,得到如下目录结构。
    在这里插入图片描述
  2. 引入所有模块公用的依赖
    在主项目的pom.xml文件中加入以下依赖
    <!-- spring cloud 配置 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

注意:这一步如果直接从网页上复制粘贴可能会出现问题(具体问题忘了),如果出问题了可以删了手打一遍即可。
主项目的工作到此即可(如后续工作中还有需要引入整个工程所需要的依赖,可以在主项目的pom中添加)。

3.构建服务注册中心
关于服务注册中心的理解,可以参考这里
个人理解:微服务架构的应用是将服务细化,分开,但总是需要一个中心节点来管理这些服务,这就是服务注册中心,一个服务写好运行时需要将自己注册到服务注册中心才能供用户使用。
(1) 构建一个新的模块discovery
file->new>module
同样选择maven项目
需要注意的是以下页面。
在这里插入图片描述
图中圈出的两项一定要设置成主项目,否则主项目中的pom中的配置不会对该子模块生效。
创建好后查看主项目中的pom.xml会发现多了以下内容

	<modules>
        <module>discovery</module>
    </modules>

表明discovery模块是主项目的子模块。
再查看discovery模块的pom.xml,会发现以下内容

	<parent>
        <artifactId>SpringCloudDemoByMaven</artifactId>
        <groupId>Elzat</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

表明该模块的父模块是SpringCloudDemoByMaven
(2) 配置服务注册中心
在discovery模块的pom.xml中引入

    <!-- @EnableEurekaServer -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-server</artifactId>
            <!--<version>1.1.6.RELEASE</version>-->
        </dependency>
    </dependencies>

在discovery.src.main.java(若该目录不是source root需要设置成source root(右键->mark directory as->sources root))下新建类DiscoveryApplication
代码如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class, args);
    }
}

在discovery.src.main.resources下新建application.yml

server:
  port: 8081
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

至此,服务注册中心完成
尝试运行一下发现出错了

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

是因为DiscoveryApplication.java文件不能直接放在main/java文件夹下,必须要建一个包把他放进去
故建一个package discovery,把DiscoveryApplication.java文件放进去
再次运行,成功

  1. 构建一个服务
    新建一个名为service的模块,构建方法同第三步
    同样要正确设置parent模块
    创建后可检查主项目的pom.xml会增加一行
    <modules>
        <module>discovery</module>
        <module>service</module>
    </modules>

在service.src.main.java下新建package service,并在该package下新建类ServiceA

package service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ServiceA {
    @GetMapping("/serviceA")
    public String service(){
        return "serviceA";
    }

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

在service.src.main.java.resources下新建application.yml

spring:
  application:
    name: service.A
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
server:
  port: 8082
  1. 查看运行结果
    先运行discovery模块
    在运行service模块
    在浏览器中输入:http://localhost:8081/
    即可看到服务已经被注册到eureka中
    在这里插入图片描述

后记

学会了手动创建之后,再使用Spring Initializer 来创建会简单很多,对整个项目的结构的认识也会清楚很多。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值