Spring Cloud Discovery First 根本不起作用 [英] Spring cloud discovery first does not work at all

查看:151
本文介绍了Spring Cloud Discovery First 根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 eureka 服务注册中心、配置服务器和客户端微服务来实现一个 spring cloud netflix 项目.我使用的是 spring cloud finchley 版本.

I try to implement a spring cloud netflix project with a eureka service registry, a configuration server and a client microservice. I use spring cloud finchley release.

我有两个问题:1. Eureka 和配置服务器都在运行,但是互不识别.我已经阅读了官方 docs 和多个教程,但它不起作用.这是我的代码:

I have two problems: 1. Eureka and configuration server are both running, but do not recognize each other. I have read the official docs and multiple tutorials, but it does not work. Here is my code:

配置服务器应用程序:

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

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

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

配置服务器的bootstrap.yml:

bootstrap.yml of config server:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        native:
          search-locations: classpath:shared
  profiles:
    active: native

server:
  port: 8888

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

配置服务器的build.gradle:

build.gradle of config server:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.0.3.RELEASE'
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

// let a BOM (Bill Of Materials) handle dependency versions that work together
dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.RELEASE'
    }
}

dependencies {
    // for SpringApplication
    compile group: 'org.springframework.boot', name: 'spring-boot'
    // for @SpringBootApplication
    compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure'
    // @EnableConfigServer
    compile group: 'org.springframework.cloud', name: 'spring-cloud-config-server'
}

服务注册应用程序代码:

The service registry application code:

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceRegistryApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceRegistryApplication.class, args);
    }
}

来自尤里卡的 bootstrap.yml:

bootstrap.yml from eureka:

spring:
  application:
    name: service-registry

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

来自尤里卡的build.gradle:

build.gradle from eureka:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.0.3.RELEASE'
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

// let a BOM (Bill Of Materials) handle dependency versions that work together
dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.RELEASE'
    }
}

// define the dependencies here
dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-netflix-eureka-server'
    // for SpringApplication
    compile group: 'org.springframework.boot', name: 'spring-boot'
    // for @SpringBootApplication
    compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure'
    // @EnableDiscoveryServer and @EnableDiscoveryClient
    compile group: 'org.springframework.cloud', name: 'spring-cloud-config-server'
}

两个应用程序都运行良好,但配置服务器没有自行注册,有什么建议吗?

Both apps run fine, but the config server doesn't registrate itself, any advice?

第二个问题:在启动另一个应该首先在 eureka 注册然后从那里获取配置服务器地址的微服务时,它给出了以下错误:

The second problem: On starting the other microservice which should registrate first at eureka and then getting from there the address of the config server, it gives the following error:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-04 14:50:34.435 ERROR 28364 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Parameter 0 of method configServerInstanceProvider in org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration required a bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' in your configuration.

2018-07-04 14:50:34.440 ERROR 28364 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'discoveryClientConfigServiceBootstrapConfiguration': Unsatisfied dependency expressed through field 'instanceProvider'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerInstanceProvider' defined in org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration: Unsatisfied dependency expressed through method 'configServerInstanceProvider' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:580) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:137) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:197) ~[spring-cloud-context-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:104) ~[spring-cloud-context-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:70) ~[spring-cloud-context-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at de.tudresden.inf.rn.amcs.microservices.courseservice.CourseServiceApplication.main(CourseServiceApplication.java:13) [main/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerInstanceProvider' defined in org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration: Unsatisfied dependency expressed through method 'configServerInstanceProvider' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:732) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:474) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1256) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1105) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 30 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:818) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:724) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 43 common frames omitted

这个服务的build.gradle:

The build.gradle of this service:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.0.3.RELEASE'
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

// let a BOM (Bill Of Materials) handle dependency versions that work together
dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.RELEASE'
    }
}

dependencies {
    // for SpringApplication
    compile group: 'org.springframework.boot', name: 'spring-boot'
    // for @SpringBootApplication
    compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure'
    // @EnableDiscoveryServer and @EnableDiscoveryClient
    compile group: 'org.springframework.cloud', name: 'spring-cloud-config-server'
    // @EnableFeignClients
    compile group: 'org.springframework.cloud', name: 'spring-cloud-netflix-core'
    // CrudRepository
    compile group: 'org.springframework.data', name: 'spring-data-jpa'
    // @EnableFeignClients
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '2.0.0.RELEASE'
}

该服务的bootstrap.yml:

bootstrap.yml of this service:

spring:
  application:
    name: course-service
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: config-server

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

应用类:

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class MicroServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(CourseServiceApplication.class, args);
    }
}

如果有人至少可以提供一些提示或建议该怎么做,那就太好了.谢谢大家!

It would be really nice, if someone could at least provide some hints or advices what to do. Thank you all!

推荐答案

我认为您的 Eureka Server 配置不正确.

I think your Eureka Server is not configured correctly.

尝试替换:

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

这样:

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这篇关于Spring Cloud Discovery First 根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆