本篇将完成一个可以注册到注册中心的最简单的服务。无论是服务提供者还是服务消费者,都可以在此项目基础上完善出来。
基础服务
最基础的服务,只完成注册到注册中心。
1、pom.xml 文件
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 6 <groupId>com.wei</groupId> 7 <artifactId>CloudUserServer</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>CloudUserServer</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.1.RELEASE</version> 18 <relativePath /> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.M9</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-test</artifactId> 42 <scope>test</scope> 43 </dependency> 44 </dependencies> 45 46 <dependencyManagement> 47 <dependencies> 48 <dependency> 49 <groupId>org.springframework.cloud</groupId> 50 <artifactId>spring-cloud-dependencies</artifactId> 51 <version>${spring-cloud.version}</version> 52 <type>pom</type> 53 <scope>import</scope> 54 </dependency> 55 </dependencies> 56 </dependencyManagement> 57 58 <build> 59 <plugins> 60 <plugin> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-maven-plugin</artifactId> 63 </plugin> 64 </plugins> 65 </build> 66 67 <repositories> 68 <repository> 69 <id>spring-milestones</id> 70 <name>Spring Milestones</name> 71 <url>https://repo.spring.io/milestone</url> 72 <snapshots> 73 <enabled>false</enabled> 74 </snapshots> 75 </repository> 76 </repositories> 77 78 79 </project>
2、配置文件
1 spring.application.name=cloud-user-server 2 3 server.port=9001 4 5 eureka.client.service-url.defaultZone=http://localhost:9000/eureka/ 6 7 # 发送心跳的时间间隔(默认为30秒) 8 eureka.instance.lease-renewal-interval-in-seconds=2 9 # 自最后一次接收到心跳后等待时间(默认为90秒) 10 eureka.instance.lease-expiration-duration-in-seconds=5
3、启动类
1 package com.wei; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 7 @SpringBootApplication 8 @EnableEurekaClient 9 public class CloudUserServerApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(CloudUserServerApplication.class, args); 13 } 14 }
@EnableEurekaClient 注解,使项目成为可注册到eureka的服务,启动后就可以在eureka页面上看到服务cloud-user-server
至此,最简单,最纯粹的一个服务就注册到eureka了。