SpringCloud简单的项目搭建及运行
Posted 掀起你的头盖骨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud简单的项目搭建及运行相关的知识,希望对你有一定的参考价值。
一,搭建框架之Eureka-server
1.打开idea,新建一个空项目,点击下一步
2.选择通过spring Initialize 构建项目module,点击Next
3. 填写好项目名称和包名 直接点击下一步
4. 选择Cloud Discovery 中的Eureka Server 选项,目的自动在pom 文件中 自动引入eureka server 的依赖,然后直接点击Next,再点击Finish 就可以完成项目的创建
5.添加maven依赖,基本的pom.xml配置信息如下
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.配置yml文件(生成的是application.properties文件,可以shift+F6改
server:
port: 8761 #修改端口号
eureka:
instance:
hostname: eureka-server #eureka服务端起个名字
client:
#由于自己就是服务器,不需要注册到自己
registerWithEureka: false
#由于自己就是服务器,不需要注册到自己
fetchRegistry: false
serviceUrl:
#注册中心的地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
7.启动类加注解@EnableEurekaServer
8.启动项目 访问http: //localhost:8071
二,搭建框架之Eureka-Client
1.和eureka的第一步相同,(我是分成两个建立的)
2.选择通过spring Initialize 构建项目module,点击Next(前面和eureka步骤一样)
3.pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.配置application.yml文件
#设置端口号
server:
port:8070
#设置服务的名称
spring:
application:
name: client001
#往eureka_service端注册的地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
5.在启动类加注解@EnableEurekaClient
//注册中心为eureka的话 推荐用EnableEurekaClient 其他的注册中心用EnabeDiscoveryClient
5.先启动eureka-server,再启动client,访问http://localhost:8071
完成
以上是关于SpringCloud简单的项目搭建及运行的主要内容,如果未能解决你的问题,请参考以下文章