spring cloud 使用Eureka作为服务注册中心

Posted shar-wang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud 使用Eureka作为服务注册中心相关的知识,希望对你有一定的参考价值。

什么是Eureka?  Eureka是在AWS上定位服务的REST服务。

Eureka简单示例,仅作为学习参考

   在pom文件引入相关的starter(起步依赖)

/*定义使用的spring cloud版本*/
<properties> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties>
/**在dependency中引入eureka-server*/ <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
/**spring cloud dependencies*/ <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>$spring-cloud.version</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

    声明@EnableEurekaServer

在springboot 启动类上添加@EnableEurekaServer注解

  注意事项

#配置Eureka默认端口
server.port=8761
#Eureka自己不要注册到Eureka上
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

打开http://localhost:8761可以看到Eureka的注册页面

将服务注册到Eureka Server

  引入注入的Starter

/**在服务上POM引入*/
    <properties>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>
/**dependency 引入eureka-client*/
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
/**dependencyManagement*/ <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>$spring-cloud.version</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies>

声明@EnableDiscoveryClient 或者 @EnableEurekaClient

  EnableDiscoveryClient : spring cloud 抽象,在启动类加上@EnableDiscoveryClient 

配置bootstarp.properties文件

#设置服务名
spring.application.name=my-service

启动服务

技术图片

 

以上是关于spring cloud 使用Eureka作为服务注册中心的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Eureka 使用 IP 地址进行服务注册

Spring Cloud Eureka 使用 IP 地址进行服务注册

Spring Cloud Eureka 使用 IP 地址进行服务注册

如何使用基本身份验证保护 Spring Cloud Eureka 服务?

Spring Cloud 升级之路 - 2020.0.x - 4. 使用 Eureka 作为注册中心

Spring Cloud 升级之路 - 2020.0.x - 4. 使用 Eureka 作为注册中心