开篇:

我们将前面的springboot整合H2内存数据库,实现单元测试与数据库无关性提供的Restful服务注册到spring cloud的Eureka Server上。

一、引入Eureka的Client

</dependencies>
 <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka</artifactId>
 </dependency>
</dependencies>
<dependencyManagement>
 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-dependencies</artifactId>
   <version>Camden.SR3</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
 </dependencies>
</dependencyManagement> 

二、添加配置文件

# spring boot服务注册到Eureka Server上的应用名称
spring.application.name=springboot-h2
eureka.instance.prefer-ip-address=true
# Eureka Server注册服务的地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka 

三、开启Eureka Client支持

package com.chhliu.springboot.h2;

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

@SpringBootApplication
@EnableEurekaClient
public class SpringbootH2Application {

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

四、启动spring boot服务并验证

通过上面几个步骤,说明我们已经将spring boot的服务注册到了Eureka Server上了!

五、可能出现的问题

如果在服务注册的过程中,发现Connection refused异常,如下:

有可能是hosts文件中没有加入如下映射关系

127.0.0.1  loaclhost

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

02-06 19:46