SpringCloud 入门2-创建eureka项目

2019-01-25  本文已影响0人  主打情歌

我这里用的是IDEA构建,你如果没有IDEA,也可以使用 Spring 官网构建或者用 Eclipse 工具构建





下一步再输入你的项目名,和项目路径,点击 Finish 就构建成功了

我们打开项目的第一时间,首先要做的是,打开pom文件,查看 SpringBoot 和 SpringCloud 的版本,我这里采用的是 SpringBoot 2.0.0.M3 和 SpringCloud Finchley.M2 版本。
在 Spring 官网 https://spring.io/projects/spring-cloud 可以查看到 SpringBoot 和SpringCloud 对应合适的版本


上面 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>

下载好相应的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 会发现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]

这是因为,你这个应用,虽然是一个 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

开始还是会有报错信息,当时它会间隔时间心跳,然后注册上去

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 #不注册自身

顺便修改下项目端口设置成官方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

上一篇下一篇

猜你喜欢

热点阅读