我這里用的是IDEA構(gòu)建,你如果沒有IDEA送膳,也可以使用 Spring 官網(wǎng)構(gòu)建或者用 Eclipse 工具構(gòu)建
下一步再輸入你的項目名员魏,和項目路徑,點擊 Finish 就構(gòu)建成功了
我們打開項目的第一時間叠聋,首先要做的是撕阎,打開pom文件,查看 SpringBoot 和 SpringCloud 的版本碌补,我這里采用的是 SpringBoot 2.0.0.M3 和 SpringCloud Finchley.M2 版本虏束。
在 Spring 官網(wǎng) https://spring.io/projects/spring-cloud 可以查看到 SpringBoot 和SpringCloud 對應(yīng)合適的版本
上面 Finchley.SR2 是 SpringCloud 的版本
下面 2.0.2.RELEASE 是Springboot 的版本
Spring Cloud Eureka 實際上是基于 Netflix Eureka 做了二次封裝 名斟,所以在版本圖里面,顯示的是 spring-cloud-netflix
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- SpringBoot 版本-->
<version>2.0.0.M3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.show</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>
<!-- SpringCloud 版本-->
<spring-cloud.version>Finchley.M2</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>
</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>
<!--下面是對 maven 源做定義魄眉,不然下載不了2.0.0.M3版本-->
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
下載好相應(yīng)的jar以后砰盐,就可以啟動項目了
2019-01-25 08:17:21.599 INFO 19668 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2019-01-25 08:17:21.781 INFO 19668 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http)
2019-01-25 08:17:21.785 INFO 19668 --- [ main] com.show.eureka.EurekaApplication : Started EurekaApplication in 8.239 seconds (JVM running for 9.229)
這個時候,你打開 localhost:8080 會發(fā)現(xiàn)404
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jan 25 08:17:55 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
我們需要給啟動類加一個注解
@SpringBootApplication
@EnableEurekaServer /** 加上這個注解坑律,才有注冊中心功能 */
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
再重新啟動 打開頁面
這樣說明岩梳,你的注冊中心初始化完成了
但是你會看到日志每隔幾秒就會報異常
2019-01-25 08:22:31.339 WARN 31500 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/WINDOWS-861L20K-show - registration failed Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:807) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:104) [eureka-client-1.7.0.jar:1.7.0]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_151]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_151]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_151]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
2019-01-25 08:22:31.340 WARN 31500 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator : There was a problem with the instance info replicator
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:807) ~[eureka-client-1.7.0.jar:1.7.0]
at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:104) ~[eureka-client-1.7.0.jar:1.7.0]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_151]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_151]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_151]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
這是因為,你這個應(yīng)用晃择,雖然是一個 Server端冀值,同時也是一個 Client端,他也需要找一個注冊中心把自己注冊上去
你點擊 @EnableEurekaServer 注解進去宫屠,就看到里面還有一個 @EnableDiscoveryClient 注解
@EnableDiscoveryClient
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({EurekaServerMarkerConfiguration.class})
public @interface EnableEurekaServer {
}
所以它即是 Server端列疗,也是 Client端
我們可以通過配置 application.yml 讓它自己給自己注冊
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka/
spring:
application:
name: eureka
開始還是會有報錯信息,當(dāng)時它會間隔時間心跳浪蹂,然后注冊上去
2019-01-25 21:37:39.505 INFO 16808 --- [ Thread-46] c.n.e.registry.AbstractInstanceRegistry : Registered instance UNKNOWN/WINDOWS-861L20K-show with status UP (replication=true)
2019-01-25 21:37:39.505 INFO 16808 --- [ Thread-46] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2019-01-25 21:37:39.505 INFO 16808 --- [ Thread-46] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2019-01-25 21:37:39.506 INFO 16808 --- [ Thread-46] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2019-01-25 21:37:39.508 INFO 16808 --- [ Thread-46] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
這樣就注冊成功了抵栈,就不會報錯了。(開始還是會報錯坤次,因為啟動的時候它還是沒有注冊成功的古劲,因為注冊中心還是初始化完成,等待幾秒后缰猴,就注冊成功了)
我們還可以配置 register-with-eureka 為 false 产艾,不注冊自己
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka/
register-with-eureka: false #不注冊自身
順便修改下項目端口設(shè)置成官方eureka默認端口8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false #不顯示在注冊中心頁面
spring:
application:
name: eureka
server:
port: 8761
這樣eureka的初始化,就完成了滑绒,eureka只是一個注冊中心闷堡,功能相對簡單。
該項目GitHub地址 https://github.com/MrXuan3168/springCloud.git