构建方式
本文介绍使用maven手动构建Spring Cloud 项目,需要说明的是,使用Idea提供的Spring Initializer建立会更加方便,但是手动创建更有利于加深对多模块化应用设计和结构的理解,以及对工程与模块之间,模块与模块之间关系的理解。在此基础上再利用Spring Initializer创建则会简单的多。
正文
- 创建主工程
创建一个空maven项目SpringCloudDemoByMaven
创建后得到相应目录结构,因为我们不会在主项目中编程,可以将主项目中的src
目录删除,使得目录结构更加清晰,得到如下目录结构。
- 引入所有模块公用的依赖
在主项目的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
文件放进去
再次运行,成功
- 构建一个服务
新建一个名为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
- 查看运行结果
先运行discovery模块
在运行service模块
在浏览器中输入:http://localhost:8081/
即可看到服务已经被注册到eureka中
后记
学会了手动创建之后,再使用Spring Initializer 来创建会简单很多,对整个项目的结构的认识也会清楚很多。