一、使用场景/原因
-
过渡期迁移:
- 当系统从一个服务注册中心迁移到另一个时,例如从 eureka 迁移到 nacos,可以在过渡期内同时使用两个注册中心,确保服务平稳迁移,逐步过渡,避免一次性切换带来的风险。
-
兼容性考虑:
- 不同的微服务可能使用不同的注册中心,为了兼容这些微服务,可以同时支持 nacos 和 eureka。这样既可以支持新开发的服务使用 nacos,也可以兼容旧有的使用 eureka 的服务。
二、增加注册中心
2.1、pom文件分别配置nacos、eureka
<dependency>
<groupid>com.alibaba.cloud</groupid>
<artifactid>spring-cloud-starter-alibaba-nacos-discovery</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
</dependency>
2.2、启动类注解
@springbootapplication
//@enableeurekaclient
@enablediscoveryclient
public class application {
public static void main(string[] args) {
springapplication.run(application.class, args);
}
}
2.3、项目配置文件application.yml
eureka:
client:
serviceurl:
defaultzone: http://localhost:9002/eureka/
instance:
prefer-ip-address: true
server:
port: 11500
spring:
application:
name: ko
cloud:
service-registry:
auto-registration:
enabled: false
nacos:
discovery:
server-addr: http://127.0.0.1:9999
namespace: 47dea8bf-3dce-4c86-82bc-82f1497c57d2
三、启动项目
3.1、自动注册时候报错
图和文字是一样的 为了方便看,可方便复制
disconnected from the target vm, address: 'localhost:63051', transport: 'socket'
***************************
application failed to start
***************************
description:
field autoserviceregistration in org.springframework.cloud.client.serviceregistry.autoserviceregistrationautoconfiguration required a single bean, but 2 were found:
- nacosautoserviceregistration: defined by method 'nacosautoserviceregistration' in class path resource [com/alibaba/cloud/nacos/registry/nacosserviceregistryautoconfiguration.class]
- eurekaautoserviceregistration: defined by method 'eurekaautoserviceregistration' in class path resource [org/springframework/cloud/netflix/eureka/eurekaclientautoconfiguration.class]
action:
consider marking one of the beans as @primary, updating the consumer to accept multiple beans, or using @qualifier to identify the bean that should be consumed
3.2、注入失败问题解决
3.2.1 application.yml 配置方式 autoconfigure.exclude
spring:
application:
name: ko
cloud:
nacos:
discovery:
server-addr: http://127.0.0.1:9999
namespace: 47dea8bf-3dce-4c86-82bc-82f1497c57d2
config:
enabled: false
server-addr: http://127.0.0.1:9999
import-check: false
namespace: 47dea8bf-3dce-4c86-82bc-82f1497c57d2
autoconfigure:
exclude: org.springframework.cloud.client.serviceregistry.autoserviceregistrationautoconfiguration
3.2.2 application 启动类配置方式
// exclude 同样可以起到忽略的作用 ,至于这里为什么要忽略两个,继续往下看,会说到
@springbootapplication(exclude = {autoserviceregistrationautoconfiguration.class,serviceregistryautoconfiguration.class})
@enablediscoveryclient
public class application {
public static void main(string[] args) {
springapplication.run(application.class, args);
}
}
***************************
application failed to start
***************************
description:
field registration in org.springframework.cloud.client.serviceregistry.serviceregistryautoconfiguration$serviceregistryendpointconfiguration required a single bean, but 2 were found:
- nacosregistration: defined by method 'nacosregistration' in class path resource [com/alibaba/cloud/nacos/registry/nacosserviceregistryautoconfiguration.class]
- eurekaregistration: defined in beandefinition defined in class path resource [org/springframework/cloud/netflix/eureka/eurekaclientautoconfiguration$refreshableeurekaclientconfiguration.class]
action:
consider marking one of the beans as @primary, updating the consumer to accept multiple beans, or using @qualifier to identify the bean that should be consumed
********
disconnected from the target vm, address: 'localhost:63833', transport: 'socket'
/**
* @author spencer gibb
*/
@configuration(proxybeanmethods = false)
public class serviceregistryautoconfiguration {
@conditionalonbean(serviceregistry.class)
@conditionalonclass(endpoint.class)
protected class serviceregistryendpointconfiguration {
@autowired(required = false)
private registration registration;
@bean
@conditionalonenabledendpoint
public serviceregistryendpoint serviceregistryendpoint(
serviceregistry serviceregistry) {
serviceregistryendpoint endpoint = new serviceregistryendpoint(
serviceregistry);
endpoint.setregistration(this.registration);
return endpoint;
}
}
}
发表评论