我们在上一章的基础上进行学习。https://blog.csdn.net/qinxun2008081/article/details/131330451
什么是注册中心?它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。
注册中心的作用就是服务的注册和服务的发现。
一、引入Nacos作为注册中心
我们查看SpringBoot和SpringCloudAlibab的版本匹配
版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub
在父项目pom.xml中添加Nacos所需的依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>myshop</artifactId>
<version>1.0-SNAPSHOT</version>
<!--设置为pom,管理依赖-->
<packaging>pom</packaging>
<modules>
<module>myshop-common</module>
<module>myshop-user</module>
<module>myshop-stock</module>
<module>myshop-order</module>
<module>myshop-goods</module>
<module>myshop-pay</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--统一管理项目依赖版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
在user子项目引入Nacos
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>myshop</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>myshop-user</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!--JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--hibernate-validate-->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!--cloud相关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<!--添加common模块的依赖-->
<dependency>
<groupId>org.example</groupId>
<artifactId>myshop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
二、服务注册
在user模块启动类中添加注解@EnableDiscoveryClient开启服务注册发现功能
package com.example.myshop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author qx
* @date 2023/06/21
* @desc 启动类
*/
@SpringBootApplication
// 开启服务注册发现
@EnableDiscoveryClient
public class MyShopUserApplication {
public static void main(String[] args) {
SpringApplication.run(MyShopUserApplication.class, args);
}
}
- 在配置文件application.yml中添加服务名称和Nacos Server地址
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/myshop?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
#服务名称
name: user-service
cloud:
nacos:
discovery:
# Nacos Server地址
server-addr: 127.0.0.1:8848
- 启动项目,通过Nacos控制台查看,发现用户服务已经注册到了Nacos Server。
最后我们完善其它几个业务子模块,将其它几个业务模块服务也注册到Nacos注册中心 。