搭建springcloud-eureka服务

本文开头头先列一下所用环境:idea2017,springboot2.0.5,springcloud Finchley.SR2

踩了三四天的坑,现在仅用本篇文章记录一下犯错历程。

读了很多springcloud eureka集群搭建的博客,书籍,开始着手搭建eureka服务。

1.创建行家父工程

先是创建一个maven工程,算是整个springcloud的父工程,文件 - >新建 - >项目 - >选择maven - > next - >输入groupId,artifactId 

- > next - > finish。(对于第一次创建eureka的新手来说,不需要对这个maven工程进行操作的,当然强迫症患者可以将它的src目录删了),这个maven项目相当于将下面所有的尤里卡子工程的POM依赖进行统一管理,使用多次的聚甲醛可以写在此工程内,后面直接调用父工程的POM,我这里没有对此工程进行任何操作

这里仅列出该Maven的项目的POM文件,父工程的的groupId,artifactId的版本,在后续创建子工程时需要用到。



    4.0.0

    com.aust
    springcloud-demo
    1.0-SNAPSHOT


2.创建一个尤里卡服务器工程

以下所有工程都是父maven项目的子模块,子模块就是一个单独的项目可以单独运行,只是在选依赖时有所不同,由两个依赖时必须勾选的,web和eureka服务器。

右击父maven项目 - >新 - >模块 - > springInitializr - > next - >输入groupId,artifactId(这里我们需要注意的时groupId需要输入父工程的groupId和artifactId,如下图),artifactId自己取。

搭建springcloud-eureka服务_第1张图片

勾选依赖如下图,创建eureka注册中心时勾选web和Eureka Server,创建服务提供者时勾选web和Eureka Discovery。

搭建springcloud-eureka服务_第2张图片

第一个尤里卡注册中心就创建完成了。

下面划重点,第一次创建eruka服务时大部分人都会遇到的问题:springboot和springcloud版本兼容性问题,这也是我踩坑花了很长时间的点。有些小伙伴回发现创建的eureka服务中心没毛病运行为什么会办错,其实就是版本不兼容问题,由于我用的springboot 2.0.5网上的博客很少由用2.0以上的springboot,如果按照网上的攻略你就走不通。这里大家可以去官网看一下springboot和springcloud的版本对应,这里我给出部分截图,具体全部的版本对应查看以下网址,拉到最下面就可以看到了。https://spring.io/projects/spring-cloud

搭建springcloud-eureka服务_第3张图片

3.配置eureka-server的application.yml

话不多说,直接上文件

server:
  port: 1000
#  #服务中心页面地址
spring:
  application:
#    应用名
    name: server1

eureka:
  instance:
    hostname: localhost
  server:
#    关闭自我保护模式(当一定时间内没有收到某个微服务实例的心跳时,不会注销这个微服务)
    enable-self-preservation: false
  client:
#    eureka服务器访问地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#    是否需要检索服务(检索服务是client端的事)
    fetch-registry: false
#    是否向注册中心注册自己(因为它就是注册中心,负载均衡时需要用到)
    register-with-eureka: false

如果大家是手撸配置的话,需要注意一个地方,eureka.client.service-url时写defaultZone系统没有任何提示,会让你以为没有这个配置或者是不是自己写错了,不用担心就是这样,没错,而且defaultZone是必须的不然运行时会报以下错误。告诉你service-url对不上号,叫你修改配置。

2018-12-02 16:46:10.866  WARN 9436 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2018-12-02 16:46:10.866  INFO 9436 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-12-02 16:46:10.878  INFO 9436 --- [ost-startStop-1] c.netflix.config.DynamicPropertyFactory  : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@765599c2
2018-12-02 16:46:11.201 ERROR 9436 --- [ost-startStop-1] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration': Unsatisfied dependency expressed through field 'eurekaServerConfig'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eurekaServerConfig' defined in class path resource [org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration$EurekaServerConfigBeanConfiguration.class]: Unsatisfied dependency expressed through method 'eurekaServerConfig' parameter 0; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'eurekaClientConfigBean': Could not bind properties to 'EurekaClientConfigBean' : prefix=eureka.client, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'eureka.client.service-url' to java.util.Map
2018-12-02 16:46:11.234  INFO 9436 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-12-02 16:46:11.242  WARN 9436 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2018-12-02 16:46:11.258  INFO 9436 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-12-02 16:46:11.262 ERROR 9436 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'eureka.client.service-url' to java.util.Map:

    Reason: No converter found capable of converting from type [java.lang.String] to type [java.util.Map]

Action:

Update your application's configuration


Process finished with exit code 1

配置搞定在启动类上添加一个@EnableEurekaServer注解,表明你是注册中心,跑起来。

package com.aust.springclouddemo.eurakeserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurakeServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurakeServerApplication.class, args);
	}
}

浏览器输入地址:端口号看看注册中心长啥样,喏,就这样了。搭建springcloud-eureka服务_第4张图片

可以看到Application一栏没货,下面我们创建一个eureka-client进货了。

4.创建一个eureka-client工程

按照步骤2的流程走一遍,依赖勾选web和Eureka Discovery,其他一样。

创建完成启动类加一个@EnableDiscoveryClient表明你是服务提供者,让注册中心可以发现你的心跳。(总觉得heartbeat在这里翻译成心跳怪怪的,但是都这么翻译,算了不纠结这玩意了)

package com.aust.springclouddemo.eurekaclient1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClient1Application {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClient1Application.class, args);
	}
}
配置的话,拿去。
server:
  port: 2001
spring:
  application:
    name: eureka-client1
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://localhost:1000/eureka/

跑起来,注意之前的那个尤里卡服务器不要关闭了,再访问一下注册中心,你会发现应用程序栏多了一个尤里卡 - 客户端1如下图:搭建springcloud-eureka服务_第5张图片

至此,尤里卡的初步创建使用已经基本完成了。后续负载均衡啥的,应就比较简单,理解了思想就好办了。

报告老板,今天的关键绩效指标已经完成,请求准点下班,过来。

你可能感兴趣的:(springcloud)