前言:此篇文章系本人学习过程中记录下来的笔记,里面难免会有不少欠缺的地方,诚心期待大家多多给予指教。
往期目录回顾:
- SpringCloud微服务(一)
- SpringCloud微服务(二)
- SpringCloud微服务(三)
- SpringCloud微服务(四)
- SpringCloud微服务(五)
- SpringCloud微服务(六)
- SpringCloud微服务(七)
- SpringCloud微服务(八)
接上期内容:上期我们顺利完成了服务网关组件的学习,至此,微服务项目里所应用的 Spring Cloud 原生组件基本都学习完了。接下来,马上要开启阿里系组件的学习旅程了,话不多说,直接发车!
一、开篇前言
鉴于阿里系组件与官方原生组件之间存在显著差异,为便于后续复习梳理,特创建一个全新项目,该项目将专门用于深入研习阿里系组件知识体系,以便更系统、高效地掌握其特性与应用场景。
新建步骤省略。总体项目结构和依赖如下:
<groupId>com.springcloud</groupId>
<artifactId>AaLiBaBaSpringCloudStudy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hutool.version>5.8.22</hutool.version>
<lombok.version>1.18.26</lombok.version>
<druid.version>1.1.20</druid.version>
<mybatis.springboot.version>3.0.2</mybatis.springboot.version>
<mysql.version>8.0.11</mysql.version>
<swagger3.version>2.2.0</swagger3.version>
<mapper.version>4.2.3</mapper.version>
<fastjson2.version>2.0.40</fastjson2.version>
<persistence-api.version>1.0.2</persistence-api.version>
<spring.boot.test.version>3.1.5</spring.boot.test.version>
<spring.boot.version>3.2.0</spring.boot.version>
<spring.cloud.version>2023.0.0</spring.cloud.version>
<spring.cloud.alibaba.version>2023.0.0.0-RC1</spring.cloud.alibaba.version>
<!--sentinel 版本和alibaba版本不兼容-->
<!-- <spring.cloud.alibaba.version>2022.0.0.0-RC2</spring.cloud.alibaba.version>-->
<micrometer-tracing.version>1.2.0</micrometer-tracing.version>
<micrometer-observation.version>1.12.0</micrometer-observation.version>
<feign-micrometer.version>12.5</feign-micrometer.version>
<zipkin-reporter-brave.version>2.17.0</zipkin-reporter-brave.version>
</properties>
<dependencyManagement>
<dependencies>
<!--springboot 3.2.0-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springCloud 2023.0.0-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springCloud alibaba 2023.0.0.0-RC1-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringBoot集成mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.springboot.version}</version>
</dependency>
<!--Mysql数据库驱动8 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--SpringBoot集成druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!--通用Mapper4之tk.mybatis-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
<!--persistence-->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>${persistence-api.version}</version>
</dependency>
<!-- fastjson2 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<!-- swagger3 调用方式 http://你的主机IP地址:5555/swagger-ui/index.html -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${swagger3.version}</version>
</dependency>
<!--huTool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<!-- spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.test.version}</version>
<scope>test</scope>
</dependency>
<!--micrometer-tracing-bom导入链路追踪版本中心 1-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bom</artifactId>
<version>${micrometer-tracing.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--micrometer-tracing指标追踪 2-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
<version>${micrometer-tracing.version}</version>
</dependency>
<!--micrometer-tracing-bridge-brave适配zipkin的桥接包 3-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
<version>${micrometer-tracing.version}</version>
</dependency>
<!--micrometer-observation 4-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
<version>${micrometer-observation.version}</version>
</dependency>
<!--feign-micrometer 5-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-micrometer</artifactId>
<version>${feign-micrometer.version}</version>
</dependency>
<!--zipkin-reporter-brave 6-->
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
<version>${zipkin-reporter-brave.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
二、Nacos总体介绍
1、Nacos是什么
总结来说就是一句话:一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台,官方地址:https://nacos.io/。
2、Nacos能干嘛
- 替代Eureka/consul做服务注册中心。
- 替代(Config+Bus)/Consul 做服务配置中心和满足动态刷新广播通知。
3、Nacos去哪儿下
4、Nacos的安装
下载完成后会得到一个压缩包。
启动命令:startup.cmd -m standalone
访问:http://localhost:8848/nacos,
出现这个页面代表Nacos启动成功。
三、Nacos注册中心实操
1、新建cloud-api-commos模块
拷贝以前的包到新项目中。目录结构如下:
2、新建cloud-consume-order9001模块
1、修改pom.xml文件,(记得刷新maven)
<dependencies>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>com.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringBoot通用依赖模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、新增application.yml文件,新增Nacos配置
server:
port: 9001
spring:
application:
name: alibaba-nacos-order-consume
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置naCos地址
3、修改主启动类
4、新增RestTemplateConfig,OrderNaCosController
@Configuration
@LoadBalancerClient(value = "alibaba-nacos-payment-provider",configuration = RestTemplateConfig.class)
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@RestController
@RequestMapping("/alibaba/consumer")
public class OrderNaCosController {
private static final String serverURL = "http://alibaba-nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@GetMapping("/order/nacos/{id}")
public ResultResponse<Object> paymentInfo(@PathVariable("id") Integer id) {
return ResultResponse.success(restTemplate.getForObject(serverURL + "/pay/nacos/" + id, String.class));
}
}
2、新建cloud-provider-payment9002模块
1、修改pom.xml文件,(记得刷新maven)
<dependencies>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>com.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringBoot通用依赖模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、新增application.yml文件,新增Nacos配置
server:
port: 9002
spring:
application:
name: alibaba-nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置naCos地址
3、修改主启动类
4、新增PayAlibabaController
@RestController
@RequestMapping("/pay")
public class PayAlibabaController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/nacos/{id}")
public ResultResponse<String> getPayInfo(@PathVariable("id") Integer id) {
return ResultResponse.success("nacos 支付服务模块, serverPort: " + serverPort + " 请求id:" + id);
}
}
3、测试功能
启动9001、9002服务,观察Nacos控制台
访问http://localhost:9001/alibaba/consumer/order/nacos/1
四、Nacos服务配置实操
1、新建cloud-nacos-config-client3377模块
步骤省略。
2、修改pom.xml文件,(记得刷新maven)
导入相关依赖。
<dependencies>
<!--bootstrap-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!--nacos-config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--引入自己通用的api-->
<dependency>
<groupId>com.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--web + actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3、新增application.yml和bootstrap.yml文件
server:
port: 3377
spring:
profiles:
active: dev # 表示开发环境
# active: prod # 表示生产环境
# active: test # 表示测试环境
# nacos配置
spring:
application:
name: alibaba-nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
config:
server-addr: localhost:8848
file-extension: yaml
4、修改主启动类
5、新增NaCosConfigClientController文件
@RestController
@RequestMapping("/alibaba/client")
public class NaCosConfigClientController {
/**
* 从nacos 配置中心取值
*/
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public ResultResponse<String> getConfigInfo() {
return ResultResponse.success(configInfo);
}
}
6、在Nacos中添加配置信息
Nacos端配置文件DataId的命名规则:
#{spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
7、测试功能
访问http://localhost:3377/alibaba/client/config/info,能正常从nacos获取配置。
8、服务配置动态刷新
使用Spring Cloud 原生注解@RefreshScope实现。
五、总结
通过学习 Nacos,不仅掌握了一款实用的服务发现和配置管理工具,更是对微服务架构下的服务治理有了更深层次的理解。在未来的项目开发中,可以更自信地运用 Nacos 去构建高效、灵活、易于维护的微服务体系,更好地服务于项目实践。
ps:努力到底,让持续学习成为贯穿一生的坚守。学习笔记持续更新中。。。。
标签:服务,SpringCloud,boot,nacos,alibaba,spring,org,cloud From: https://blog.csdn.net/m0_48860687/article/details/144668598