我尝试将微服务注册为Eureka客户端以发现其他微服务,但是,我按照教程但没有显示在Eureka Server中。以下是代码段:
我的demo
应用程序:在localhost:9001
上运行的Spring启动应用程序,我希望它成为Eureka客户端,即将自身注册为实例,同时具有发现其他实例的能力(我使用了泛型@EnableDiscoveryClient
注释,Spring Netflix Eureka在类路径上):
@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
@RequestMapping("/")
String home() {
return "This is a Demo project";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
application.yml:
server:
port: 9001
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
Eureka Server应该没问题,因为我在服务器上成功注册了localhost:8080
上运行的另一个Miscroservice。以防这里是Eureka Server的application.yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
有人在这看到任何问题吗?
答案 0 :(得分:6)
重要的是选择正确的导入。我花了几个小时才意识到:
这是裸依赖项:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
在大多数情况下,这是入门包依赖项:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
如果导入裸依赖,则不会显示任何错误或日志,但该服务不会注册到Eureka服务器。
最重要的是,请确保仔细检查依赖性:也许您需要Spring Boot组件的“入门包”依赖性。
答案 1 :(得分:3)
问题解决了,我没有包含依赖&#39; com.netflix.eureka:eureka-client:1.1.147&#39;在我的build.gradle中。
答案 2 :(得分:1)
service.application.properties
spring.application.name=rating-data-service
server.port=8083
eureka.client.eureka-server-port=8761 // on which port server is running
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
server.application.properties
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
答案 3 :(得分:0)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
在pom.xml中添加以上行,如果使用gradle,请使用以下代码:
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: '1.4.6.RELEASE'
答案 4 :(得分:0)
在内部添加pom.xml spring-cloud-netflix-eureka-客户端
答案 5 :(得分:0)
在pom.xml中执行以下操作
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
在Spring引导配置中,请执行以下操作:
@EnableDiscoveryClient @SpringBootApplication
公共类ServiceApplication
答案 6 :(得分:0)
客户端build.gradle必须有点像下面的
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
ext {
set('springCloudVersion', "Hoxton.SR1")
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
客户端的application.properties文件应包含以下内容-
spring.application.name=movie-catalog-service // Your service name
server.port=8081 // on which port you are running
eureka.client.eureka-server-port=8761 // on which port server is running
Eureka Server build.gradle必须有点像下面的
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "Hoxton.SR3")
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
Eureka Server文件的application.properties应该有点像
server.port=8761 // port on which server is running
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
参考: https://spring.io/guides/gs/service-registration-and-discovery/
答案 7 :(得分:0)