学完springboot单体项目 是很无聊的 一直再想到底干不干微服务 觉得很难又觉得工作深入的学习越来越难 鉴于自己的学生生涯较长 就干吧

在此想起饺子的一句话 出来混最重要的是什么---“出来”!!!
image


言归正传 三步走

注册中心/配置管理nacos


一.简介安装和快速开始

省去一大堆废话 官网言简意赅
image

首先安装:https://nacos.io/download/nacos-server/?spm=5238cd80.2ef5001f.0.0.3f613b7cl5w5gK

解压后进入bin目录

startup.cmd -m standalone //单机启动

访问 http://电脑主机号:8848/nacos/index.html

出现可视化页面到此成功
image


二.服务注册和服务发现

首先依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibabdependencies</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

我不展开一一罗列每个功能 模拟一个场景来展示

放在代码中为 product商品模块 和 order订单模块
image

原理:
image

代码块实现:

order订单模块:
package com.gao.Main;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RefreshScope //自动刷新配置中心文件
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/or/info")
public String getOrderInfo() {
String productInfo = restTemplate.getForObject("http://localhost:8800/pt/info", String.class);
//以下为实现负载均衡后动态输入调用服务模块的名字nacos-product
// String productInfo = restTemplate.getForObject("http://nacos-product/pt/info", String.class);
return "Order Info: Order ID - 123, " + productInfo;
}
}
配置类RestTemplate
简介:RestTemplate 是 Spring Framework 提供的一个用于简化 HTTP 通信的同步客户端工具类,主要面向服务端与服务端之间的 HTTP 接口调用
package com.gao.Main.Configuration;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class config {
@Bean
@LoadBalanced //负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
product商品模块:
package Main;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@GetMapping("/pt/info")
public String getProductInfo() {
return "Product Info: Product Name - Example Product, Price - $100";
}
}
要在启动类上加上
@EnableDiscoveryClient //开启服务发现功能
两个服务模块都需要配置properties:
spring.application.name=该服务模块名字
server.port=端口号
spring.cloud.nacos.server-addr=主机名:8848

启动两个服务后:输入http://localhost:8000/or/info 得到商品信息

服务注册成功
image

成功远程调用
image

上面的负载均衡@LoadBalanced
搭配负载均衡 通过客户端负载均衡,能够显著提升微服务架构的弹性和性能,是构建高可用系统的关键组件
使用 @LoadBalanced 注解修饰 RestTemplate
http://商品模块名字/di/info 动态实现远程调用

一道问题:
问题:注册中心宕机(nacos服务关掉) 远程调用是否可以成功?
1.若第一次调用 宕机后 不能成功
2.若已经调用过 宕机后 仍能成功 因为此时用的是缓存中的信息


三.配置中心

嘿嘿偷个懒 这个有点复杂 不过多阐述
参考:https://www.yuque.com/leifengyang/sutong/oz4gbyh5maa0rmxu#xtDS8

image

参考:尚硅谷视频:
https://www.bilibili.com/video/BV1UJc2ezEFU/?spm_id_from=333.1007.top_right_bar_window_default_collection.content.click&vd_source=eb2341710c995d8261ecc99fdd066ba7

参考:DeepSeek-R1问答

还有还有人总得证明自己活着!

posted on   不爱美女爱辣条  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?



点击右上角即可分享
微信分享提示