IDEA 社区版 创建spring cloud项目图文

系统管理员 2021-10-09 04:38 1282阅读 0赞

之前一直使用Eclipse开发,后来开始使用Spring Tool Suite,最近才开始使用IDEA,不过工作中还是用的Eclipse比较多,私下才会用IDEA,还不是很熟悉,这里记录一下使用IDEA创建spring Cloud。

项目信息:
JDK 1.8
spring boot 2.0.3
spring cloud Finchley.SR3
maven 3.2.2
idea 社区版

create new project ->Spring Assistant,然后选择JDK,以及默认的Spring Initializr server(https://start.spring.io 快速创建spring boot)
在这里插入图片描述

填写自己项目的信息,我选择使用的是maven
在这里插入图片描述
选择Spring Boot 的版本,这里现在这里可供选择的是2.1.6以上的版本,以及1.5.21的版本,先选择一个版本后面进行修改就好了。
在这里插入图片描述
选择完版本之后,可以选择一下自己需要的组件,所依赖的包会自动添加到pom文件中,不需要自己手动添加了。比如选择Eureka Server,就会自动在pom文件中加入 spring-cloud-starter-netflix-eureka-server 的依赖。

在这里插入图片描述

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4. </dependency>

然后,确定项目名称以及工作空间,Finish项目就完成了。
在这里插入图片描述

这样就成功创建了一个Eureka Server项目,修改pom.xm里面的spring boot版本

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.0.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>clarezhou.example</groupId>
  12. <artifactId>eureka-server-test</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>eureka-server-test</name>
  15. <description>Eureka Server</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Finchley.SR3</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-devtools</artifactId>
  28. <scope>runtime</scope>
  29. <optional>true</optional>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.projectlombok</groupId>
  33. <artifactId>lombok</artifactId>
  34. <optional>true</optional>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. <dependencyManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-dependencies</artifactId>
  47. <version>${spring-cloud.version}</version>
  48. <type>pom</type>
  49. <scope>import</scope>
  50. </dependency>
  51. </dependencies>
  52. </dependencyManagement>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>

修改完之后,有时候不会自动下载依赖包,可以 Maven->Reimport 一下
在这里插入图片描述
修改启动类,添加Eureka Server的annotation

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  4. @SpringBootApplication
  5. @EnableEurekaServer
  6. public class EurekaServerTestApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(EurekaServerTestApplication.class, args);
  9. }
  10. }

修改配置文件

  1. #端口号
  2. server.port=8081
  3. eureka.instance.hostname=localhost
  4. #向注册中心注册服务
  5. eureka.client.registerWithEureka=false
  6. # 检索服务
  7. eureka.client.fetchRegistry=false
  8. eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
  9. #在Eureka服务器获取不到集群里对等服务器上的实例时,需要等待的时间
  10. server.waitTimeInMsWhenSyncEmpty=0
  11. #自我保护模式
  12. server.enableSelfPreservation=false

启动Eureka Server
在这里插入图片描述

这样最简单的Eureka Server就创建成功了,再创建Eureka Client作为服务提供者,以及消费者,这里就不多介绍了。

发表评论

表情:
评论列表 (有 0 条评论,1282人围观)

还没有评论,来说两句吧...

相关阅读