项目用的是springcloud,但是自己一直没有搭建过,都是公司大佬们搭建好的,所以有时间就想好好总结一下,自己从头搭建一遍
供以后查看。
首先就是项目目录
创建一个maven,然后再创建modules
其中最外层的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xhs</groupId>
<artifactId>iot</artifactId>
<version>1.0.0.Standard.RELEASE</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<spring-boot.version>1.5.9.RELEASE</spring-boot.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties>
<!--公共依赖-->
<dependencies>
<!--注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>iot-eureka</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
</project>
然后是iot-eureka的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xhs.iot</groupId>
<artifactId>iot-eureka</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>iot-eureka</name>
<description>iot-eureka</description>
<parent>
<groupId>com.xhs</groupId>
<artifactId>iot</artifactId>
<version>1.0.0.Standard.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
在iot-eureka的resource创建配置文件application.yml
server:
port: 1025 #端口
spring:
application:
name: iot-eureka-server #服务名称
security:
basic:
enabled: false # 关闭身份认证 为true表示开启身份验证
user:
name: root # 定义用户名
password: root # 定义密码
eureka:
client:
fetch-registry: false #为false意味着无需注册自身。
register-with-eureka: false #为false意味着自身仅作为服务器,不作为客户端
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
instance:
hostname: localhost
server: #配置属性,但由于 Eureka 自我保护模式以及心跳周期长的原因,经常会遇到 Eureka Server 不剔除已关停的节点的问题
enable-self-preservation: false
eviction-interval-timer-in-ms: 5000
logging:
level:
root: info
然后再启动类加上注解@EnableEurekaServer(springboot用的1.5.x,不是2.0.X)
package com.xhs.iot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class IotEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(IotEurekaApplication.class, args);
}
}
然后启动项目,访问刚刚设置的1025端口,如果开启了身份验证,则要输入用户名和密码,上面设置的用户和密码
注册服务就启动了