免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2469 | 回复: 1
打印 上一主题 下一主题

如何创建“服务注册中心” [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-06-14 11:25 |只看该作者 |倒序浏览
创建一个基础的Spring Boot工程,命名为eureka-server,并在pom.xml中引入需要的依赖内容:
  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.4.RELEASE</version>
  5. <relativePath/>
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.cloud</groupId>
  10. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  11. </dependency>
  12. </dependencies>
  13. <dependencyManagement>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.cloud</groupId>
  17. <artifactId>spring-cloud-dependencies</artifactId>
  18. <version>Dalston.SR1</version>
  19. <type>pom</type>
  20. <scope>import</scope>
  21. </dependency>
  22. </dependencies>
  23. </dependencyManagement>
复制代码
通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的Spring Boot应用中添加这个注解就能开启此功能,比如下面的例子:

  1. @EnableEurekaServer  
  2. @SpringBootApplication  
  3. public class Application {  
  4. public static void main(String[] args) {  
  5. new SpringApplicationBuilder(Application.class)  
  6. .web(true).run(args);  
  7. }  
  8. }  
复制代码
在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:

  1. spring.application.name=eureka-server  
  2. server.port=1001  
  3. eureka.instance.hostname=localhost  
  4. eureka.client.register-with-eureka=false  
  5. eureka.client.fetch-registry=false  
复制代码

为了与后续要进行注册的服务区分,这里将服务注册中心的端口通过server.port属性设置为1001。

从现在开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,希望可以帮助更多的好学者。大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。


论坛徽章:
0
2 [报告]
发表于 2018-06-14 11:25 |只看该作者
欢迎大家一起交流~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP