下面来看看,分布式服务怎么整合Nacos,实现电商微服务拆分与服务注册和高可用部署
实现一个订单下单的流程,创建订单同时减库存的场景
Windows10
JDK1.8
IDEA2021
Maven2.6
IDEA中新建一个Maven项目,起好名字,选好项目存放路径,作为父模块
删除用不到的src目录
在父模块的POM里添加如下内容
<!--add-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
点击项目右键创建Order子Module
填写必要信息
子模块结构
父模块POM文件中可以看到子模块
不用修改子模块的POM文件,依赖继承自父模块
创建这样的文件结构
OrderController
package com.order.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class OrderController {
@Resource
private RestTemplate restTemplate;
@GetMapping("/order")
public String order(String userId, String productId) {
String stockResult = restTemplate.getForObject(
"http://stock-serv/stock/" + productId, String.class);
return stockResult;
}
}
YML
server:
port: 8002
spring:
cloud:
nacos:
discovery:
service: order-serv
server-addr: localhost:8848
OrderApplication
package com.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class);
}
@Bean
@LoadBalanced
public RestTemplate create() {
return new RestTemplate();
}
}
启动查看是否注册到Nacos
如上,创建Stock子Maven模块
创建如下结构
YML
server:
port: 11000
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
service: stock-serv
StockController
package com.stock.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StockController {
@GetMapping("/stock/{productId}")
public String stock(@PathVariable String productId) {
System.out.println("减库存一个成功");
return "减库存一个成功";
}
}
StockApplication
package com.stock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class StockApplication {
public static void main(String[] args) {
SpringApplication.run(StockApplication.class);
}
}
启动服务查看是否注册到Nacos
浏览器输入:localhost:8848/nacos,登录
前面我们已经将测试用的代码搭建起来了,现在我们来测试多个Stock服务的Nacos注册与发现
首先在IDEA中配置Stock服务允许多开
修改YML文件服务端口,启动
Apifox测试,请求是否轮训到每个Stock服务上
发送两个请求
11000处理了一个请求
11001处理了一个请求
Nacos中的Stock服务集群
此时如果下线11000服务,那么所有请求就会发送给11001服务处理,自测