通过上一章的操作,我们已经可以实现微服务之间的调用。但是我们把服务提供者的网络地址
下载地址 : https://github.com/alibaba/nacos/releases下载 zip 格式的安装包,然后进行解压缩操作
# 切换目录cd nacos/bin# 命令启动startup.cmd -m standalone
双击startup.cmd 即可启动nacos
第3步: 访问nacos
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
@SpringBootApplication@EnableDiscoveryClientpublic class ProductApplication
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
@SpringBootApplication@EnableDiscoveryClientpublic class OrderApplication
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
package com.itheima.controller;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.itheima.entity.Order;
import com.itheima.entity.Product;
import com.itheima.service.OrderService;
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private OrderService orderService;
//准备买1件商品
@GetMapping("/order/prod/{pid}")
public Order order(@PathVariable("pid") Integer pid) {
//从nacos中获取服务地址
ServiceInstance serviceInstance = discoveryClient.getInstances("service-product").get(0);
String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();
//通过restTemplate调用商品微服务
Product product = restTemplate.getForObject(
"http://" + url +"/product/" + pid, Product.class);
log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
Order order = new Order();
order.setUid(1);
order.setUsername("测试用户");
order.setPid(product.getPid());
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setNumber(1);
orderService.save(order);
return order;
}
}
DiscoveryClient 是专门负责服务注册和发现的,我们可以通过它获取到注册到注册中心的所有服 务
6 然后通过访问消费者服务验证,调用成功
我们在微服务调用关系中一般会选择 客户端负载均衡,也就是在服务调用的一方来决定服务由哪个提供者执行。
2 通过nacos查看微服务的启动情况
还可以查看服务详情
3 修改 shop-order 的代码,实现负载均衡(我们自己用随机数实现负载均衡)
package com.itheima.controller;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.itheima.entity.Order;
import com.itheima.entity.Product;
import com.itheima.service.OrderService;
import java.util.List;
import java.util.Random;
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private OrderService orderService;
//准备买1件商品
@GetMapping("/order/prod/{pid}")
public Order order(@PathVariable("pid") Integer pid) {
// 从nacos中获取服务地址
// 自定义规则实现随机挑选服务
List instances = discoveryClient.getInstances("service-product");
int index = new Random().nextInt(instances.size());
ServiceInstance serviceInstance = instances.get(index);
String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();
log.info(">>从nacos中获取到的微服务地址为:" + url);
//通过restTemplate调用商品微服务
Product product = restTemplate.getForObject(
"http://" + url +"/product/" + pid, Product.class);
log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
Order order = new Order();
order.setUid(1);
order.setUsername("测试用户");
order.setPid(product.getPid());
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setNumber(1);
orderService.save(order);
return order;
}
}
@Bean@LoadBalancedpublic RestTemplate restTemplate () {return new RestTemplate ();}
package com.itheima.controller;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.itheima.entity.Order;
import com.itheima.entity.Product;
import com.itheima.service.OrderService;
import java.util.List;
import java.util.Random;
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private OrderService orderService;
@GetMapping("/order/prod/{pid}")
public Order order(@PathVariable("pid") Integer pid) {
//直接使用微服务名字,从nacos中获取服务地址
String url = "service-product";
//通过restTemplate调用商品微服务
Product product = restTemplate.getForObject(
"http://" + url +"/product/" + pid, Product.class);
log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
Order order = new Order();
order.setUid(1);
order.setUsername("测试用户");
order.setPid(product.getPid());
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setNumber(1);
orderService.save(order);
return order;
}
}
我们可以通过修改配置来调整Ribbon的负载均衡策略,具体代码如下
service-product: # 调用的提供者的名称
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
org.springframework.cloud spring-cloud-starter-openfeign
2 在主类上添加Fegin的注解
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClients // 开启 Feginpublic class OrderApplication {}
package com.itheima.service;
import com.itheima.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("service-product") //声明调用的提供者的name
public interface ProductService {
// 指定调用提供者的哪个方法
// @FeignClient+@GetMapping 就是一个完整的请求路径 http://service- product/product/{pid}
@GetMapping(value = "/product/{pid}")
Product findByPid(@PathVariable("pid") Integer pid);
}
package com.itheima.controller;
import com.itheima.service.ProductService;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.itheima.entity.Order;
import com.itheima.entity.Product;
import com.itheima.service.OrderService;
import java.util.List;
import java.util.Random;
@RestController
@Slf4j
public class OrderController {
@Autowired
private OrderService orderService;
@Autowired
private ProductService productService;
@GetMapping("/order/prod/{pid}")
public Order order(@PathVariable("pid") Integer pid) {
//通过fegin调用商品微服务
Product product = productService.findByPid(pid);
log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
Order order = new Order();
order.setUid(1);
order.setUsername("测试用户");
order.setPid(product.getPid());
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setNumber(1);
orderService.save(order);
return order;
}
}
5 重启order微服务,查看效果