SpringCloud------Eureka注册中心

Eureka 是一个服务注册与发现的组件,用于微服务管理服务的信息,便于服务之间进行通讯

一.搭建Eureka

1.搭建Eureka Server,创建一个SpringBoot项目,引入Eureka Server依赖

2.编写Eureka Server 的 application.yml配置文件

#服务端口号
server:
  port: 8000
spring:
  application:
    name: app-eureka
#eureka 基本信息配置
eureka:
  instance:
    #注册中心ip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      #注册到的Eureka Server地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    #因为自己是为注册中心,不需要自己注册自己
    register-with-eureka: false
    #因为自己是为注册中心,不需要检索服务
    fetch-registry: false

3.在启动类中加入Eureka Server启动注解

package com.xx;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerDemoApplication {

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

}

4.搭建Eureka Client,创建一个SpringBoot项目,引入Web和Eureka Server依赖

5.编写Eureka Client  的 application.yml配置文件

#服务启动端口号
server:
  port: 8100
#服务名称(服务注册到eureka名称)
spring:
  application:
    name: app-member
#将Member服务的ip和端口信息注册到Eureka Server中
eureka:
  client:
    service-url:
      #Eureka Server地址
      defaultZone: http://localhost:8000/eureka
    #将服务注册到Eureka中
    register-with-eureka: true
    #从Eureka获取注册信息
    fetch-registry: true

6.在启动类中加入Eureka Client启动注解

package com.xx;

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

@SpringBootApplication
@EnableEurekaClient
public class MemberApplication {

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

}

二.搭建Eureka集群

再创建一个Eureka Server,并使它们之间相互注册

#服务端口号
server:
  port: 8000
spring:
  application:
    name: app-eureka
#eureka 基本信息配置
eureka:
  instance:
    #注册中心ip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      #将自己注册进端口为9000的Eureka Server
      defaultZone: http://${eureka.instance.hostname}:9000/eureka/
    #搭建集群需要将自己也注册进去
    register-with-eureka: true
    fetch-registry: true
#服务端口号
server:
  port: 9000
spring:
  application:
    name: app-eureka
#eureka 基本信息配置
eureka:
  instance:
    #注册中心ip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      #将自己注册进端口为8000的Eureka Server
      defaultZone: http://${eureka.instance.hostname}:8000/eureka/
    #搭建集群需要将自己也注册进去
    register-with-eureka: true
    fetch-registry: true

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章