spring cloud 服务注册中心

ゝ一纸荒年。 2024-02-19 16:56 121阅读 0赞

原文链接: https://blog.csdn.net/forwujinwei/article/details/72794231
创建“服务注册中心”
spring cloud的学习都是建立在spring boot基础上的,所以对spring boot不了解的童鞋先要了解下spring boot项目的搭建以及运行机制。

1.创建springBoot项目

è¿éåå¾çæè¿°

2.并在pom.xml中引入需要的依赖

  1. <dependencies>
  2. <!--eureka 服务依赖,将来其他服务注册就是注册到这个项目下,类似于dubbo使用zookeeper作为注册中心-->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-test</artifactId>
  10. <scope>test</scope>
  11. </dependency>
  12. </dependencies>
  13. <!--用来管理spring cloud,在 <dependency>标签中引用spring cloud的组件就不需要指定版本信息 -->
  14. <dependencyManagement>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-dependencies</artifactId>
  19. <version>Brixton.SR5</version><!-- 最好使用一些新的版本 -->
  20. <type>pom</type>
  21. <scope>import</scope>
  22. </dependency>
  23. </dependencies>
  24. </dependencyManagement>

3.在application.properties中添加配置

  1. server.port=8888
  2. eureka.client.register-with-eureka=false
  3. eureka.client.fetch-registry=false
  4. eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

eureka端口,和其他项目区分,保持独立
server.port=8761
配置主机名
eureka.instance.hostname=eureka-server-01
配置服务注册中心是否以自己为客户端进行注册(配置false)
eureka.client.registerWithEureka=fasle
是否取得注册信息(配置false)
eureka.client.fetchRegistry=false
配置eureka客户端的缺省域(该配置可能没有提示,请复制或者手动输入,切勿使用有提示的service-url会引起内置tomcat报错)
eureka.client.serviceUrl.defaultZone=http://localhost:$\{server.port\}/eureka4.在入口文件中开启服务注册中心

  1. package com.example;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer
  7. public class SpringCloudServerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringCloudServerApplication.class, args);
  10. }
  11. }

5.启动项目
访问:http://localhost:8761/

è¿éåå¾çæè¿°
这样注册中心就好了,下面就可以将服务注册到注册中心,供消费者调用,大家也许会想一个事,如果注册中心宕机了那不是就惨了吗?所有调用将死去,是的,所以搭建高可用的注册中心是非常重要的。
一般来说,搭建高可用的注册中心架构人员会做,无需普通开发人员关心,其实在spring cloud环境下工作很多事情变得比较容易,下一篇博文我们来搭建高可用的注册中心。
-——————————
作者:Awna
来源:CSDN
原文:https://blog.csdn.net/forwujinwei/article/details/72794231
版权声明:本文为博主原创文章,转载请附上博文链接!

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读