目标:模拟商品下单操作。订单微服务通过注册中心(nacos)获取商品微服务的地址信息,完成商品信息查询,生成订单。
目录
2.2、在主类上添加@EnableDiscoveryClient注解
2.3、在application.yml中添加nacos服务的地址
2.4、启动服务,观察nacos的控制面板中是否有注册上来的商品微服务
1、搭建nacos环境
1.1、安装nacos
下载地址:https://github.com/alibaba/nacos/releases
我的机器是windows环境,下载的zip版本。
1.2、启动nacos
双击startup.cmd启动。或者通过命令行启动。
# 进入nacas的bin目录
cd nacos/bin
# 命令启动
startup.cmd -m standalone
测试发现,通过双击startup.cmd进行启动,会有异常抛出,启动失败。所以通过命令行以单机模式启动。双击启动异常如下:
java.lang.RuntimeException: load jdbc.properties error
推荐做法:修改startup.cmd文件,将启动模式修改为单机模式。然后就可以通过双击startup.cmd文件进行启动了
set MODE="standalone"
1.3、访问nacos
打开浏览器输入http://localhost:8848/nacos,即可访问服务。默认用户名密码是nacos/nacos
2、将商品微服务注册到nacos
接下来开始修改shop-product模块的代码,将其注册到nacos服务上。
2.1、在pom.xml中添加nacos的依赖
<!--nacos客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.2、在主类上添加@EnableDiscoveryClient注解
package cn.jack;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class);
}
}
2.3、在application.yml中添加nacos服务的地址
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
2.4、启动服务,观察nacos的控制面板中是否有注册上来的商品微服务
3、调用商品微服务查询商品信息,生成订单
package cn.jack.controller;
import cn.jack.domain.Order;
import cn.jack.domain.Product;
import cn.jack.service.OrderService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private OrderService orderService;
@Autowired
private DiscoveryClient discoveryClient;
/**
* 下单
* @param pid 商品id
* @return
*/
@RequestMapping("/order/prod/{pid}")
@Deprecated
public Order order(@PathVariable("pid") Long pid) {
log.info("收到下单请求,准备查询商品信息。pid={}", pid);
// 调用商品微服务,查询商品信息
ServiceInstance serviceInstance = discoveryClient.getInstances("service-product").get(0);
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
StringBuffer url = new StringBuffer();
url.append("http://").append(host).append(":").append(port).append("/product/").append(pid);
Product product = this.restTemplate.getForObject(url.toString(), Product.class);
log.info("商品信息查询成功。内容为:{}", JSON.toJSONString(product));
// 生成商品信息保存
Order order = new Order();
order.setNumber(1);
order.setPid(pid);
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setUid(1);
order.setUsername("测试用户");
this.orderService.createOrder(order);
log.info("订单信息保存成功。内容为:{}", JSON.toJSONString(order));
return order;
}
}
至此,案例流程结束。