SpringCloudAlibaba随笔目录
一、SpringCloudAlibaba项目之父工程搭建
二、SpringCloudAlibaba项目之Nacos搭建及服务注册
三、SpringCloudAlibaba项目之生产者与消费者
四、SpringCloudAlibaba项目之Ribbon负载均衡
五、SpringCloudAlibaba项目之OpenFeign远程呼叫
六、SpringCloudAlibaba项目之Nacos-config配置中心
七、SpringCloudAlibaba项目之Sentinel流量控制
八、SpringCloudAlibaba项目之Seata分布式事务
九、SpringCloudAlibaba项目之GateWay网关
十、SpringCloudAlibaba项目之SkyWalking链路跟踪
SpringCloudAlibaba项目之生产者与消费者
1、生产者与消费者
本篇文章,我们搭建俩个服务,生产者服务与消费者服务,
我们以电商系统为例:服务生产者service-order,
服务消费者service-stock
说明:service-order 作为消费者提供新增订单信息的功能,service-stock作为生产者提供库存的扣减服务功能;这里呼叫者为消费者,被掉用者为生产者,
项目目录
2、生产者服务搭建
使用Spring Initializr创建
修改pom.xml档案:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 继承父工程版本依赖 --> <parent> <artifactId>spring-cloud-alibaba</artifactId> <groupId>com.qt</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.qt</groupId> <artifactId>service-stock</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-stock</name> <description>service-stock project for Spring Boot</description> <dependencies> <!-- springweb 启动依赖 --> <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> <!-- springboot 测验类 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.qt.servicestock.ServiceStockApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
父工程的pom.xml档案添加:
<modules> <module>service-stock</module> </modules>
application.properties修改:
# 应用名称
spring.application.name=service-stock
# 应用服务 WEB 访问埠
server.port=8030
# Nacos帮助档案: https://nacos.io/zh-cn/docs/concepts.html
# Nacos认证信息
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和埠
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# 注册到 nacos 的指定 namespace,默认为 public
spring.cloud.nacos.discovery.namespace=public
新增StockController:
/** * 库存服务 */ @RestController @RequestMapping("/stock") public class StockController { @Value("${server.port}") private String port; /** * 库存新增 * @return */ @RequestMapping("/addStock") public String addStock(){ System.out.println("库存新增成功"); return "库存服务-库存新增成功"; } /** * 库存扣减 * @return */ @RequestMapping("/subStock") public String subStock(){ System.out.println("库存扣减成功"); return "库存服务-库存扣减成功 - 埠:" + port; } }
启动类:
@SpringBootApplication @EnableDiscoveryClient //可加可不加,依版本而定,从Spring Cloud Edgware开始,@EnableDiscoveryClient可省略,只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上, public class ServiceStockApplication { public static void main(String[] args) { SpringApplication.run(ServiceStockApplication.class, args); } }
3、消费者服务搭建(和生产者创建一样)
使用Spring Initializr创建
修改pom.xml档案:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 继承父工程版本依赖 --> <parent> <artifactId>spring-cloud-alibaba</artifactId> <groupId>com.qt</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.qt</groupId> <artifactId>service-order</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-order</name> <description>service-order project for Spring Boot</description> <dependencies> <!-- springweb 启动依赖 --> <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
0 评论