整合nacos
一、项目结构small-shop为父工程,order(订单服务)、stock(库存服务)
二、父工程pom.xml文件,添加基础依赖,控制版本
- <modules>
- <module>ordermodule>
- <module>stockmodule>
- modules>
-
- <properties>
- <spring-boot.version>2.3.12.RELEASEspring-boot.version>
- <spring-cloud.version>Hoxton.SR12spring-cloud.version>
- <spring-cloud-alibaba.version>2.2.9.RELEASEspring-cloud-alibaba.version>
- properties>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-dependenciesartifactId>
- <version>${spring-boot.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-dependenciesartifactId>
- <version>${spring-cloud.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-alibaba-dependenciesartifactId>
- <version>${spring-cloud-alibaba.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
三、stock模块(打算使用order模块调用stock模块)
3.1、pom.xml
- "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-shopartifactId>
- <groupId>com.xice.shopgroupId>
- <version>0.0.1-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>stockartifactId>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
- 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中看到两个服务注册成功