微服务-实现Nacos+Eureka双注册中心

版本对应关系: file

file

Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version
2021.1.1.0*Spring Cloud 2021.1.12.6.3
2021.0.5.0*Spring Cloud 2021.0.52.6.13
2021.0.4.0Spring Cloud 2021.0.42.6.11
2021.0.1.0Spring Cloud 2021.0.12.6.3
2021.1Spring Cloud 2020.0.12.4.2
2022.0.0.0*Spring Cloud 2022.0.03.0.2
2022.0.0.0-RC2Spring Cloud 2022.0.03.0.2
2022.0.0.0-RC1Spring Cloud 2022.0.03.0.0

Nacos+Eureka双注册中心

1、 pom文件

                <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
                <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.0.1.0</version>
        </dependency>

2、配置yaml

eureka:
  instance:
    # 设置该服务注册中心的hostname
    hostname: 127.0.0.1
  client:
    # 我们创建的是服务注册中心,而不是普通的应用,这个应用会向注册中心注册它自己
    #,设置为false就是禁止自己向自己注册的这个种行为
    register-with-eureka: false
    # 不去检索其他的服务,因为注册中心本身的职责就是维护服务实例
    fetch-registry: false
    # 制定服务注册中心的位置
    service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


spring:
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      username: nacos
      passwrod: nacos
      discovery:
        namespace: dev # 命名空间。这里使用 dev 开发环境
        metadata:
          version: 1.0.0 # 服务实例的版本号,可用于灰度发布

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient   // 开启 Spring Cloud 注册发现客户端
public class Application {

    public static void main(String[] args) {
        // 启动 Spring Boot 应用
        SpringApplication.run(Application.class, args);
        System.out.println("---------启动成功!------");
    }

}

启动报错,只能有一个注册实例

file

修改配置

spring:
  application:
    name: doubleRegister
  autoconfigure:
    exclude: org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration,org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration

Nacos+Eureka注册注册成功 file

检查nacos file

检查eureka file

本文由博客一文多发平台 OpenWrite 发布!

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值