mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
nacos注册中心
This commit is contained in:
113
doc/docs/files/10106_nacos注册中心.md
Normal file
113
doc/docs/files/10106_nacos注册中心.md
Normal file
@@ -0,0 +1,113 @@
|
||||
# nacos注册中心
|
||||
|
||||
完整代码在`registry-nacos`分支
|
||||
|
||||
SOP默认使用eureka注册中心,如果要换成nacos注册中心,步骤如下:
|
||||
|
||||
- 实现`com.gitee.sop.registryapi.service.RegistryService`接口
|
||||
|
||||
1.找到`SOP/sop-common/sop-registry-api`工程,在service.impl包下新建一个类,实现RegistryService接口
|
||||
|
||||
```java
|
||||
public class RegistryServiceNacos implements RegistryService {
|
||||
|
||||
@Override
|
||||
public List<ServiceInfo> listAllService(int pageNo, int pageSize) throws Exception {
|
||||
// TODO: 返回服务实例
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onlineInstance(ServiceInstance serviceInstance) throws Exception {
|
||||
// TODO: 实例上线
|
||||
}
|
||||
|
||||
@Override
|
||||
public void offlineInstance(ServiceInstance serviceInstance) throws Exception {
|
||||
// TODO: 实例下线
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2.在`com.gitee.sop.registryapi.config.BaseRegistryConfig`中新增
|
||||
|
||||
```java
|
||||
/**
|
||||
* 当配置了registry.name=nacos生效
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "registry", name = "name", havingValue = "nacos")
|
||||
RegistryService registryServiceNacos() {
|
||||
return new RegistryServiceNacos();
|
||||
}
|
||||
```
|
||||
|
||||
其中`@ConditionalOnProperty(prefix = "registry", name = "name", havingValue = "nacos")`
|
||||
表示`application.properties`配置了`registry.name=nacos`参数才能生效,registry.name=nacos下文会讲到。
|
||||
|
||||
|
||||
- 微服务端修改
|
||||
|
||||
1.修改微服务应用pom.xml,注释eureka服务发现依赖,添加nacos服务发现依赖
|
||||
|
||||
```xml
|
||||
<!-- 注册中心【只能用一个,不用的注释掉】 -->
|
||||
<!-- 使用eureka注册中心
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
-->
|
||||
|
||||
<!-- 使用nacos注册中心
|
||||
版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
|
||||
https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-alibaba-nacos-discovery
|
||||
-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
<version>0.2.2.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- 注册中心end -->
|
||||
```
|
||||
|
||||
2.yml文件新增nacos配置,并注释掉eureka相关配置
|
||||
|
||||
```yaml
|
||||
spring:
|
||||
cloud:
|
||||
# nacos注册中心,和eureka只能用一个
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
```
|
||||
|
||||
- 网关修改
|
||||
|
||||
找到`sop-gateway`工程,步骤同上
|
||||
|
||||
- SOP-admin修改
|
||||
|
||||
|
||||
修改yml文件,新增nacos服务器地址,`registry.name`填nacos
|
||||
|
||||
```yaml
|
||||
# 注册中心地址,根据实际情况改,这里只是参数,并不会去注册
|
||||
registry:
|
||||
# 使用哪个配置中心,使用eureka,填eureka;使用nacos填nacos
|
||||
name: nacos
|
||||
eureka-server-addr: http://localhost:1111/eureka/
|
||||
nacos-server-addr: 127.0.0.1:8848
|
||||
```
|
||||
|
||||
- website-server修改
|
||||
|
||||
步骤同`SOP-admin修改`
|
||||
|
||||
如果要改成consul注册中心,可参照以上步骤。
|
||||
|
||||
- 参考资料
|
||||
|
||||
1.[nacos介绍及安装](https://nacos.io/zh-cn/docs/quick-start.html)
|
||||
2.[nacos spring cloud注册发现](https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html)
|
Reference in New Issue
Block a user