mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.2.0
This commit is contained in:
86
doc/docs/files/10113_扩展其它注册中心.md
Normal file
86
doc/docs/files/10113_扩展其它注册中心.md
Normal file
@@ -0,0 +1,86 @@
|
||||
# 扩展其它注册中心
|
||||
|
||||
SOP默认使用的注册中心是[nacos](https://nacos.io/),可以扩展实现其它注册中心,其中`eureka`分支是已经扩展好的,使用eureka注册中心。
|
||||
|
||||
现在以扩展[consul](https://www.consul.io/)为例,说下具体扩展步骤:
|
||||
|
||||
- 扩展注册中心监听
|
||||
|
||||
在`sop-gateway-common`工程下,找到com.gitee.sop.gatewaycommon.route包,可以看到有两个类
|
||||
|
||||
`EurekaRegistryListener`和`NacosRegistryListener`
|
||||
|
||||
这两个类的作用是监听注册中心服务注册,从而触发事件,然后获取新注册的服务。
|
||||
|
||||
新建一个类:`ConsulRegistryListener`,继承`BaseRegistryListener`
|
||||
|
||||
实现onEvent方法,具体内容可参考`EurekaRegistryListener`类
|
||||
|
||||
```java
|
||||
public class ConsulRegistryListener extends BaseRegistryListener {
|
||||
/**
|
||||
* 注册中心触发事件,可以从中获取服务<br>
|
||||
*
|
||||
* 这个方法做的事情有2个:<br>
|
||||
*
|
||||
* 1. 找出新注册的服务,调用pullRoutes方法<br>
|
||||
* 2. 找出删除的服务,调用removeRoutes方法<br>
|
||||
*
|
||||
* @param applicationEvent 事件体
|
||||
*/
|
||||
@Override
|
||||
public void onEvent(ApplicationEvent applicationEvent) {
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
配置类中新增:
|
||||
|
||||
```java
|
||||
@Bean
|
||||
@ConditionalOnProperty("spring.cloud.consul.host")
|
||||
RegistryListener registryListenerConsul() {
|
||||
return new ConsulRegistryListener();
|
||||
}
|
||||
```
|
||||
|
||||
其中`@ConditionalOnProperty("spring.cloud.consul.host")`的意思是只有配置了`spring.cloud.consul.host`属性,这个Bean才会被Spring注入
|
||||
|
||||
`sop-gateway`工程添加`Spring Cloud Consul`相关依赖,配置文件新增consul配置
|
||||
|
||||
- 扩展admin实现
|
||||
|
||||
找到`sop-admin-server`工程下com.gitee.sop.adminserver.service包,可以看到有两个类,`RegistryServiceEurekaImpl`和`RegistryServiceNacosImpl`
|
||||
它们实现了`com.gitee.sop.adminserver.service.RegistryService`接口,因此我们要新建一个consul对应的类
|
||||
|
||||
新建`RegistryServiceConsulImpl`,然后实现RegistryService接口中的方法,具体可参考RegistryServiceEurekaImpl
|
||||
|
||||
```java
|
||||
public class RegistryServiceConsulImpl implements RegistryService {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
打开`com.gitee.sop.adminserver.config.WebConfig`类
|
||||
|
||||
新增一条配置
|
||||
|
||||
```java
|
||||
/**
|
||||
* 当配置了registry.name=eureka生效。
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "registry.name", havingValue = "consul")
|
||||
RegistryService registryServiceEureka() {
|
||||
return new RegistryServiceConsulImpl();
|
||||
}
|
||||
```
|
||||
|
||||
application配置文件新增一条配置:
|
||||
|
||||
```properties
|
||||
registry.name=consul
|
||||
```
|
Reference in New Issue
Block a user