温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring cloud如何将spring boot服务注册到Eureka Server上

发布时间:2021-08-04 14:13:23 来源:亿速云 阅读:109 作者:小新 栏目:编程语言

这篇文章主要介绍spring cloud如何将spring boot服务注册到Eureka Server上,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

开篇:

我们将前面的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-h3 
eureka.instance.prefer-ip-address=true 
# Eureka Server注册服务的地址 
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

三、开启Eureka Client支持

package com.chhliu.springboot.h3; 
 
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 cloud如何将spring boot服务注册到Eureka Server上

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

五、可能出现的问题

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

spring cloud如何将spring boot服务注册到Eureka Server上

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

127.0.0.1  loaclhost

以上是“spring cloud如何将spring boot服务注册到Eureka Server上”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI