springcloud服务注册中心Eureka

启动一个服务注册中心,只需要一个注解@EnableEurekaServer

package com.dalingjia.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
 * 启动一个服务注册中心,只需要一个注解@EnableEurekaServer
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

配置文件如下,表明自己是一个服务注册中心:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Eureka客服端,通过注解@EnableEurekaClient表明自己是一个eurekaClient:

/**
 * 通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
 */
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class ServiceHiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceHiApplication.class, args);
    }
}

客户端配置:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: service-hi

启动服务端和客户端,访问localhost:8761,可以看到注册到eureka的当前实例:
在这里插入图片描述

@EnableDiscoveryClient与@EnableEurekaClient区别

简而言之 spring cloud 中服务发现有多种实现(eureka、consul、zookeeper等等),@EnableDiscoveryClient 基于 spring-cloud-commons, @EnableEurekaClient 基于 spring-cloud-netflix。

一句话:如果选用eureka做注册中心,那么推荐@EnableEurekaClient;如果不是eureka,那么推荐使用@EnableDiscoveryClient。

@EnableEurekaClient源码如下:

package org.springframework.cloud.netflix.eureka;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface EnableEurekaClient {
}

@EnableDiscoveryClient源码如下:

package org.springframework.cloud.client.discovery;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({EnableDiscoveryClientImportSelector.class})
public @interface EnableDiscoveryClient {
    boolean autoRegister() default true;
}
目标:本示例说明Spring Cloud的高可以用服务中心的搭建。也就是解决当一个服务注册中心当机后,我们可以提供多个服务注册副本来解决服务查询不到的问题,从达到在微服开发中实现高可用注册中心的功能。 开发环境:IDEA集成工具,JDK 1.8 使用步骤:下载资源后解压项目,使用IDEA导入项目文件。 1. 在找C:\Windows\System32\drivers\etc\hosts文件,在最配置文件添加 127.0.0.1 peer-one 127.0.0.1 peer-two 2. 在IDEA工具的右上角选择“MySpringThreeApplication”项目,然后点击播放按钮启动注册中心。 3. 在IDEA工具的右上角选择“MySpringOneApplication”项目,点击播放按钮启动该微服务项目。 4. 在浏览器中打开一个窗口的地址栏,输入:http://localhost:9000/后回车启动第一个服务注册。 5. 在浏览器中打开一个窗口的地址栏,输入:http://localhost:9001/后回车启动第二个服务注册。 6. 在浏览器中打开一个窗口的地址栏,输入:http://localhost:8080/hello后回车请求该服务。 如果一切正常,那么我们在服务注册中心面板另外一个服务注册中心,以及注册的微服务。并且在控制台中可以看到DiscoveryClient接口使用日志对象打印两个注册中心的信息,然后我们在IDEA中关掉第一个服务注册中心,并且在浏览器的地址栏输入:http://localhost:8080/hello后回车请求该服务。 你会发现,在控制台中还会显示一个服务注册中心的信息。 使用对象:想学习使用Spring 5.x版本实现的Spring Cloud编程和微服开发的小白。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值