整合nacos
一、项目结构small-shop为父工程,order(订单服务)、stock(库存服务)
二、父工程pom.xml文件,添加基础依赖,控制版本
<modules>
<module>order</module>
<module>stock</module>
</modules>
<properties>
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.9.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
</dependencies>
</dependencyManagement>
三、stock模块(打算使用order模块调用stock模块)
3.1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>small-shop</artifactId>
<groupId>com.xice.shop</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>stock</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
3.2、application.yml配置文件
server:
port: 8081 #服务端口
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务地址
application:
name: stock-server #服务命称
3.3、服务代码
@RestController
@RequestMapping("stock")
public class StockController {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 无参数
* @return
*/
@RequestMapping("reduce")
public String reduce(){
return "库存减一 "+dateFormat.format(new Date());
}
/**
* 多个参数
* @param orderId
* @return
*/
@RequestMapping("reduce1")
public String reduce1(@RequestParam("orderId")String orderId){
return "orderId="+orderId+",库存减一 "+dateFormat.format(new Date());
}
/**
* 参数为map
* @param map
* @return
*/
@RequestMapping("reduce2")
public String reduce2(@RequestBody Map map){
return "orderId="+map.get("orderId")+",库存减一 "+dateFormat.format(new Date());
}
}
四、order模块
4.1、pom依赖与stock引入的一样
4.2、application.yml配置文件
server:
port: 8080 #服务端口
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务地址
application:
name: order-server #服务命称
4.3、初始化RestTemplate,需要添加 @LoadBalanced 注解,配置负载均衡策略
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
4.4、使用RestTemplate调用stock服务
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("add")
public String add(){
String result = restTemplate.getForObject("http://stock-server/stock/reduce", String.class);
String result1 = restTemplate.getForObject("http://stock-server/stock/reduce1?orderId={1}", String.class,"100");
Map map = new HashMap();
map.put("orderId",100);
String result2 = restTemplate.getForObject("http://stock-server/stock/reduce1?orderId={orderId}",String.class, map);
String result3 = restTemplate.postForObject("http://stock-server/stock/reduce2", map,String.class);
return result+"----"+result1+"----"+result2+"----"+result3;
}
}
五、启动两个服务可以在nacos中看到两个服务注册成功
六、访问http://localhost:8080/order/add