我的Spring Cloud(二):Eureka Server注册中心

一、Eureka是什么

    Eureka是Netflix开源的基于REST的服务治理方案,Spring Cloud集成了Eureka,提供服务治理和服务发现功能,可以和基于Spring Boot搭建的微服务应用轻松完成整合。

二、Spring Cloud Eureka的组成

    1. Eureka Server,注册中心

    2. Eureka Client,所有要进行注册的微服务通过Eureka Client连接到Eureka Server,完成注册

三、实战!快速搭建注册中心

    1.创建一个父工程,pom.xml配置如下

<parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>2.0.7.RELEASE</version>
  </parent>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  </dependencies>
  
  <dependencyManagement>
  	<dependencies>
  		<dependency>
  			<groupId>org.springframework.cloud</groupId>
  			<artifactId>spring-cloud-dependencies</artifactId>
  			<version>Finchley.SR2</version>
  			<type>pom</type>
  			<scope>import</scope>
  		</dependency>
  	</dependencies>
  </dependencyManagement>

2.在父工程下创建一个子工程Module,pom.xml配置如下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>

3.在子工程下创建application.yml,添加Eureka相关的配置如下

server:
  # 当前Eureka Server服务端口
  port: 8761
eureka:
  client:
    # 是否将当前的Eureka Server服务作为客户端进行注册
    register-with-eureka: false
    # 是否获取其他Eureka Server服务的数据
    fetch-registry: false
    
    service-url:
      # 注册中心的访问地址
      defaultZone: http://localhost:8761/eureka/

属性说明

    * server.port:当前Eureka Server服务端口

    * eureka.client.register-with-eureka:是否将当前的Eureka Server服务作为客户端进行注册

    * eureka.client.fetch-registry:是否获取其他Eureka Server服务的数据

    * eureka.client.service-url.defaultZone:注册中心的访问地址

4.创建启动类

package com.frr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

}

注解说明

    * @SpringBootApplication:声明该类是Spring Boot服务的入口

    *@EnableEurekaServer:声明该类是一个Eureka Server微服务,提供服务注册和服务发现的功能,即注册中心

5.启动成功后的界面

四、总结

    1.新建父工程,在pom文件中加入公用的依赖

    2.在父工程中创建一个子工程Module,在Module的pom文件中添加自己需要的组件依赖

    3.在Spring Boot里面添加它的端口和Eureka相关的配置

    4.最后创建启动类,在启动类添加注解,让当前这个工程成为一个Eureka Server

    从业务角度,我们将注册到服务中心的服务可分为服务提供者与服务消费者,那么服务提供者如何向其他服务提供服务呢,让我们期待下一篇《我的Spring Cloud(三):Eureka Client 服务提供者》。

    更多精彩内容,敬请扫描下方二维码关注我的微信公众号【Java觉浅】,获取第一时间更新哦!

猜你喜欢

转载自blog.csdn.net/qq_34942272/article/details/106349217