搭建nacos环境
1 安装nacos
下载地址: https://github.com/alibaba/nacos/releases
下载zip格式的安装包,然后进行解压缩操作
2 启动nacos
#切换目录
cd nacos/bin
#命令启动
startup.cmd -m standalone
3 访问nacos
打开浏览器输入http://localhost:8848/nacos,即可访问服务, 默认密码是nacos/nacos
将商品服务注册到nacos
接下来开始修改 shop-product 模块的代码, 将其注册到nacos服务上
1 在pom.xml中添加nacos的依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
2.在主启动类上添加nacos的开启注解
package com.cyy.product; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient @MapperScan(basePackages = "com.cyy.product.mapper") public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class,args); } }
3.在application.propreties添加nacos的配置
#配置nacos注册中心的地址 spring.cloud.nacos.server-addr=localhost:8848
4.启动服务,观察nacos的控制面板中是否有注册上来的商品微服务
将订单微服务注册到nacos上
接下来开始修改 order 模块的代码, 将其注册到nacos服务上
1.在pom.xml中添加nacos的依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
2.在主启动类上添加nacos的开启注解
package com.cyy.order; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient @MapperScan(basePackages = "com.cyy.order.mapper") public class OrderApplication { public static void main(String[] args) { SpringApplication.run(com.cyy.order.OrderApplication.class,args); } @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
3.在application.propreties添加nacos服务的地址
spring.application.name=order
spring.cloud.nacos.server-addr=localhost:8848
4.启动服务, 观察nacos的控制面板中是否有注册上来的订单微服务
5.修改OrderController中的代码
package com.cyy.order.controller; import com.cyy.entry.Order; import com.cyy.entry.Product; import com.cyy.order.service.OrderService; 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.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; //@RestController //@RequestMapping("order") public class OrderController_write { @Autowired private OrderService orderService; @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("saveOrder") public String saveOrder(Integer pid,Integer num){//商品id 购买数量 收货地址 Order order=new Order(); order.setNumber(num);//设置订单的数量 order.setUid(1); order.setUsername("xxx"); order.setPid(pid); //根据商品id查询商品对应的信息--->调用远程商品服务 //基于TCP协议--rpc远程方法调用 速度快 但是非常消耗资源 因为TCP属于常链接 //基于HTTP协议---->速度慢 但是资源消耗少 属于短链接 List<ServiceInstance> instances = discoveryClient.getInstances("product"); int index=(int)(Math.random()*instances.size()); ServiceInstance instance = instances.get(index); Product product = restTemplate.getForObject(instance.getUri()+"/product/selectById/" + pid, Product.class); order.setPname(product.getPname()); order.setPprice(product.getPprice()); orderService.saveOrder(order); return "下单成功"; } }
6.启动服务, 观察nacos的控制面板中是否有注册上来的订单微服务,然后通过访问消费者服务验证调用是否成功
转自:https://blog.csdn.net/Hedeghog_cyy/article/details/118555486