SpringCloud服务注册到Eureka Server

一、为什么使用Eureka?

   在一个完整的系统中,任何单点的服务都不能保证不会中断,因此我们需要服务发现机制,在某个节点中断后,其它的节点能够继续提供服务,从而保证整个系统的是可用的。
    Eureka Server会提供服务注册表,各个服务节点启动后,会在Eureka Server中进行注册,这样Eureka Server中就有了所有服务节点的信息,并且Eureka有监控页面,可以在页面中直观的看到所有注册服务的情况。

二、如何将SpringCloud服务注册到Eureka Server上?

1)开启Eureka Server
     step1.新建一个springBoot项目后除了springBoot的依赖外还需要在pom.xml中引入以下依赖:

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

    <!--放在dependencies外面-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    step2.在启动类中加入@EnableEurekaServer注解

@SpringBootApplication
@Component
@EnableEurekaServer
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

    Step3.在application.yml中添加如下配置

eureka:
  client:
    register-with-eureka: false #禁止自身注册到Eureka Server中
    fetch-registry: false #禁止从Eureka服务器获取注册信息
    service-url:
      defaultIcons: http://localhost:8080/eureka/ # 指定Eureka服务端的地址,
  server:
    enable-self-preservation: false #关闭自我保护
    eviction-interval-timer-in-ms: 10000 #清理间隔

step4.验证

当服务启动成功后在浏览器的地址栏中输入”localhost:8080”,如果出现以下情况就证明成功了。

这里写图片描述

2)将微服务注册到Eureka Server上
     step1.新建一个springBoot项目后除了springBoot的依赖外还需要在pom.xml中引入以下依赖:

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

    <!--放在dependencies外面-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    step2.在启动类中加入@EnableDiscoveryClient注解

@SpringBootApplication
@Component
@EnableDiscoveryClient
public class DemoApplication {

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

    Step3.在application.yml中添加如下配置

#默认使用配置
spring:
  profiles:
      active: dev
#公共配置与profiles选择无关
mybatis:
  typeAliasesPackage: com.aim.graduation.dao
  mapperLocations: classpath:mapper/*.xml
server:
  port: 8081  #注意端口号不要重复

---

#开发配置
spring:
  session:
    store-type: none
  application:
    name: schoolSaralyServiceProvider #设置服务名称

eureka:
 client:
  serviceUrl:
    defaultZone: http://localhost:8080/eureka/
 instance:
   prefer-ip-address: true #使用ip地址注册
   instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} #设置实例的id
   lease-renewal-interval-in-seconds: 1 #设置eureka client发送心跳给server端的频率
   lease-expiration-duration-in-seconds: 2 #设置示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间

将Eureka Server和SpringCloud服务同时打开,在浏览器的地址栏中输入localhost:8080就会出现下图的这种情况。

这里写图片描述

此时说明springCloud已经注册到Eureka Server上了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值