SpringCloud从入门到入土02-Eureka服务注册中心快速搭建

1.Eureka搭建

1.1.什么是Eureka

注册中心,用来管理微服务的,实现服务的注册与发现。功能类似于Dubbo的注册中心,比如Zookeeper。
EurekaServer就是服务方,提供注册和发现的服务。
EurekaClient就是客户端,我们的微服务都注册到对应的注册中心当中去。

2 搭建EurekaServer

2.1 新建子工程EurekaServer

SpringCloud从入门到入土02-Eureka服务注册中心快速搭建_第1张图片

2.2 pom文件


  <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
    dependencies>

这块我们在子工程即可,后面其他客户端会导入另外的坐标。

 <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-eureka-serverartifactId>
        <version>1.4.6.RELEASEversion>
    dependency>

当然也可以在父工程直接导入,一劳永逸。两者任选一。

1.5版本使用spring-cloud-starter-eureka-server还是没问题的。2.0以上建议使用 spring-cloud-starter-netflix-eureka-server。

2.3.application.yml

server:
 port: 8000 #声明端口号
eureka:
 server:
    enable-self-preservation: false
 client:
    register-with-eureka: false #默认是true,将自己注册到eureka上
    fetch-registry: false #是否从eureka上获取信息
    service-url:
        defaultZone: http://localhost:8000/eureka #设置与eureka交互的地址,查询服务和注册服务都需要依赖这个地址,默认是http://localhost:8761/eureka

或者想写成配置

server:
  port: 8000 #声明端口号
# Eureka配置
eureka:
  instance:
    # Eureka服务端的实例名字
    hostname: 127.0.0.1
  client:
    # 表示是否向 Eureka 注册中心注册自己(这个模块本身是服务器,所以不需要)
    register-with-eureka: false
    # fetch-registry如果为false,则表示自己为注册中心,客户端的化为 ture
    fetch-registry: false
    # Eureka监控页面~
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这里的配置基本上固定不变的,端口号确定一般不会修改

2.4 设置启动类,访问8000端口测试

启动类加上@EnableEurekaServer注解即可

package com.liu;

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

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

启动测试http://localhost:8000/
SpringCloud从入门到入土02-Eureka服务注册中心快速搭建_第2张图片
这块启动成功,但是没有一个服务注册上去了。

2.5 启动报错问题

注意这块会启动报错,当然你也可能正常启动。
这块我报错的原因不是Eureka的错误,因为检查了版本和依赖配置都没问题,是SpringBoot和SpringCloud版本不一致问题。
下图是重点
SpringCloud从入门到入土02-Eureka服务注册中心快速搭建_第3张图片
我们选择的是2.6.7版本,最新的可以去官方查询
版本查询
复制下来序列化

{
	"git": {
		"branch": "d8a01da77ea93ed6441d0c7002d920058eef3e7b",
		"commit": {
			"id": "d8a01da",
			"time": "2022-05-19T09:16:16Z"
		}
	},
	"build": {
		"version": "0.0.1-SNAPSHOT",
		"artifact": "start-site",
		"versions": {
			"spring-boot": "2.6.7",
			"initializr": "0.13.0-SNAPSHOT"
		},
		"name": "start.spring.io website",
		"time": "2022-05-19T09:19:17.958Z",
		"group": "io.spring.start"
	},
	"bom-ranges": {
		"codecentric-spring-boot-admin": {
			"2.4.3": "Spring Boot >=2.3.0.M1 and <2.5.0-M1",
			"2.5.6": "Spring Boot >=2.5.0.M1 and <2.6.0-M1",
			"2.6.5": "Spring Boot >=2.6.0.M1 and <2.7.0-M1"
		},
		"solace-spring-boot": {
			"1.1.0": "Spring Boot >=2.3.0.M1 and <2.6.0-M1",
			"1.2.1": "Spring Boot >=2.6.0.M1 and <2.7.0-M1"
		},
		"solace-spring-cloud": {
			"1.1.1": "Spring Boot >=2.3.0.M1 and <2.4.0-M1",
			"2.1.0": "Spring Boot >=2.4.0.M1 and <2.6.0-M1",
			"2.3.0": "Spring Boot >=2.6.0.M1 and <2.7.0-M1"
		},
		"spring-cloud": {
			"Hoxton.SR12": "Spring Boot >=2.2.0.RELEASE and <2.4.0.M1",
			"2020.0.5": "Spring Boot >=2.4.0.M1 and <2.6.0-M1",
			"2021.0.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-M3",
			"2021.0.0-M3": "Spring Boot >=2.6.0-M3 and <2.6.0-RC1",
			"2021.0.0-RC1": "Spring Boot >=2.6.0-RC1 and <2.6.1",
			"2021.0.2": "Spring Boot >=2.6.1 and <2.6.9-SNAPSHOT",
			"2021.0.3-SNAPSHOT": "Spring Boot >=2.6.9-SNAPSHOT and <3.0.0-M1",
			"2022.0.0-M1": "Spring Boot >=3.0.0-M1 and <3.0.0-M2",
			"2022.0.0-M2": "Spring Boot >=3.0.0-M2 and <3.1.0-M1"
		},
		"spring-cloud-azure": {
			"4.1.0": "Spring Boot >=2.5.0.M1 and <2.7.0-M1"
		},
		"spring-cloud-gcp": {
			"2.0.10": "Spring Boot >=2.4.0-M1 and <2.6.0-M1",
			"3.2.1": "Spring Boot >=2.6.0-M1 and <2.7.0-M1"
		},
		"spring-cloud-services": {
			"2.3.0.RELEASE": "Spring Boot >=2.3.0.RELEASE and <2.4.0-M1",
			"2.4.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1",
			"3.3.0": "Spring Boot >=2.5.0-M1 and <2.6.0-M1",
			"3.4.0": "Spring Boot >=2.6.0-M1 and <2.7.0-M1"
		},
		"spring-geode": {
			"1.3.12.RELEASE": "Spring Boot >=2.3.0.M1 and <2.4.0-M1",
			"1.4.13": "Spring Boot >=2.4.0-M1 and <2.5.0-M1",
			"1.5.13": "Spring Boot >=2.5.0-M1 and <2.6.0-M1",
			"1.6.7": "Spring Boot >=2.6.0-M1 and <2.7.0-M1",
			"1.7.0-RC1": "Spring Boot >=2.7.0-M1 and <3.0.0-M1",
			"2.0.0-M2": "Spring Boot >=3.0.0-M1 and <3.1.0-M1"
		},
		"vaadin": {
			"14.8.11": "Spring Boot >=2.1.0.RELEASE and <2.6.0-M1",
			"23.0.9": "Spring Boot >=2.6.0-M1 and <2.8.0-M1"
		},
		"wavefront": {
			"2.0.2": "Spring Boot >=2.1.0.RELEASE and <2.4.0-M1",
			"2.1.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1",
			"2.2.2": "Spring Boot >=2.5.0-M1 and <2.7.0-M1"
		}
	},
	"dependency-ranges": {
		"native": {
			"0.9.0": "Spring Boot >=2.4.3 and <2.4.4",
			"0.9.1": "Spring Boot >=2.4.4 and <2.4.5",
			"0.9.2": "Spring Boot >=2.4.5 and <2.5.0-M1",
			"0.10.0": "Spring Boot >=2.5.0-M1 and <2.5.2",
			"0.10.1": "Spring Boot >=2.5.2 and <2.5.3",
			"0.10.2": "Spring Boot >=2.5.3 and <2.5.4",
			"0.10.3": "Spring Boot >=2.5.4 and <2.5.5",
			"0.10.4": "Spring Boot >=2.5.5 and <2.5.6",
			"0.10.5": "Spring Boot >=2.5.6 and <2.5.9",
			"0.10.6": "Spring Boot >=2.5.9 and <2.6.0-M1",
			"0.11.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-RC1",
			"0.11.0-M2": "Spring Boot >=2.6.0-RC1 and <2.6.0",
			"0.11.0-RC1": "Spring Boot >=2.6.0 and <2.6.1",
			"0.11.0": "Spring Boot >=2.6.1 and <2.6.2",
			"0.11.1": "Spring Boot >=2.6.2 and <2.6.3",
			"0.11.2": "Spring Boot >=2.6.3 and <2.6.4",
			"0.11.3": "Spring Boot >=2.6.4 and <2.6.6",
			"0.11.5": "Spring Boot >=2.6.6 and <2.7.0-M1",
			"0.12.0-SNAPSHOT": "Spring Boot >=2.7.0-M1 and <3.0.0-M1"
		},
		"okta": {
			"1.4.0": "Spring Boot >=2.2.0.RELEASE and <2.4.0-M1",
			"1.5.1": "Spring Boot >=2.4.0-M1 and <2.4.1",
			"2.0.1": "Spring Boot >=2.4.1 and <2.5.0-M1",
			"2.1.5": "Spring Boot >=2.5.0-M1 and <2.7.0-M1"
		},
		"mybatis": {
			"2.1.4": "Spring Boot >=2.1.0.RELEASE and <2.5.0-M1",
			"2.2.2": "Spring Boot >=2.5.0-M1"
		},
		"camel": {
			"3.5.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1",
			"3.10.0": "Spring Boot >=2.4.0.M1 and <2.5.0-M1",
			"3.13.0": "Spring Boot >=2.5.0.M1 and <2.6.0-M1",
			"3.16.0": "Spring Boot >=2.6.0.M1 and <2.7.0-M1"
		},
		"picocli": {
			"4.6.3": "Spring Boot >=2.4.0.RELEASE and <3.0.0-M1"
		},
		"open-service-broker": {
			"3.2.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1",
			"3.3.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1",
			"3.4.0-M2": "Spring Boot >=2.5.0-M1 and <2.6.0-M1"
		}
	}
}

这边我们对应的SpringCloud的版本为2021.0.2,修改对应的版本号再重新启动即可。

3 搭建EurekaClient

服务端引入依赖,加上注解即可制作,客户端就是之前我们的demo1,demo2,我们把这些微服务注册到服务端即可。

3.1 pom文件

 <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
    dependencies>

3.2.application.yml

新增配置,我们可以先给微服务取个名字,这样在注册中心更容易看到

spring:
  application:
    name: demo1
# Eureka配置:配置服务注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka

3.3启动类测试

在原来的启动类上加注解测试

@EnableEurekaClient

SpringCloud从入门到入土02-Eureka服务注册中心快速搭建_第4张图片

你可能感兴趣的:(分布式,eureka,spring,cloud,java)