微服务调用方式ribbon
订单服务(调用方) 调用商品服务 获取商品信息
1、创建order_service项目
2、使用ribbon. (类似httpClient,URLConnection)
启动类增加注解
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
package net.xdclass.order_service; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); } @Bean @LoadBalanced//指定负载均衡的策略 public RestTemplate restTemplate() { return new RestTemplate(); } }
3、开发下单接口
controller:
package net.xdclass.order_service.controller; import net.xdclass.order_service.service.ProductOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("api/v1/order") public class OrderController { @Autowired private ProductOrderService productOrderService; /** * * @param userId * @param productId * @return */ @RequestMapping("save") public Object save( @RequestParam("user_id")int userId, @RequestParam("product_id") int productId){ return productOrderService.save(userId, productId); } }
service:主要是service业务层,如何调用 商品服务 的接口呢?
先写个接口
package net.xdclass.order_service.service; import net.xdclass.order_service.domain.ProductOrder; /** * 订单业务类 */ public interface ProductOrderService { /** * 下单接口 * @param userId * @param productId * @return */ ProductOrder save(int userId, int productId); }
调用方式一:service
使用RestTemplate类的方法,指定服务名称product-service的接口即可
Spring RestTemplate中几种常见的请求方式:https://blog.csdn.net/u012702547/article/details/77917939/
package net.xdclass.order_service.service.impl; import net.xdclass.order_service.domain.ProductOrder; import net.xdclass.order_service.service.ProductOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import sun.net.www.URLConnection; import sun.net.www.http.HttpClient; import java.util.Date; import java.util.Map; import java.util.UUID; @Service public class ProductOrderServiceImpl implements ProductOrderService { @Autowired private RestTemplate restTemplate; @Override public ProductOrder save(int userId, int productId) { /** * 调用product-service的接口 */ Map<String,Object> productMap = restTemplate.getForObject("http://product-service/api/v1/product/find?id="+productId, Map.class); ProductOrder productOrder = new ProductOrder(); productOrder.setCreateTime(new Date()); productOrder.setUserId(userId); productOrder.setTradeNo(UUID.randomUUID().toString()); productOrder.setProductName(productMap.get("name").toString()); productOrder.setPrice(Integer.parseInt(productMap.get("price").toString())); return productOrder; } }
调用方式二:sevice
1.注入LoadBalancerClient对象,可以从这个对象中获取服务名称,端口号等等信息
2.然后用这个对象构造一个url
3.再使用RestTemplate类的方法发送请求即可
注意:%s表示后的参数的,第一个表示第一个参数,第二个表示第二个参数...
package net.xdclass.order_service.service.impl; import net.xdclass.order_service.domain.ProductOrder; import net.xdclass.order_service.service.ProductOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import sun.net.www.URLConnection; import sun.net.www.http.HttpClient; import java.util.Date; import java.util.Map; import java.util.UUID; @Service public class ProductOrderServiceImpl implements ProductOrderService { @Autowired private RestTemplate restTemplate; // // @Autowired private LoadBalancerClient loadBalancer; @Override public ProductOrder save(int userId, int productId) { //调用方式二 ServiceInstance instance = loadBalancer.choose("product-service"); String url = String.format("http://%s:%s/api/v1/product/find?id="+productId, instance.getHost(),instance.getPort()); RestTemplate restTemplate = new RestTemplate(); Map<String,Object> productMap = restTemplate.getForObject(url, Map.class); ProductOrder productOrder = new ProductOrder(); productOrder.setCreateTime(new Date()); productOrder.setUserId(userId); productOrder.setTradeNo(UUID.randomUUID().toString()); productOrder.setProductName(productMap.get("name").toString()); productOrder.setPrice(Integer.parseInt(productMap.get("price").toString())); return productOrder; } }