1-服务调用
eureka内部继承了ribbon
- 在创建RestTemplate的时候,声明@LoadBalanced
在服务消费者order_service中,找到启动类OrderApplication
package cn.itcast.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EntityScan("cn.itcast.order.entity") //用来扫描和发现指定包及其子包中的Entity定义
public class OrderApplication {
/**
* 使用spring提供的RestTemplate发送http请求到商品服务
* 1-创建RestTemplate对象交给容器管理
* 2-在使用的时候,调用其方法完成操作(getXX,postXXX)
*/
@LoadBalanced //是ribbon提供的负载均衡的注解
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
- 使用RestTemplate调用远程服务
不需要拼接微服务的URL,以待请求的服务名替换ip地址
package cn.itcast.order.controller;
import cn.itcast.order.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/order")
public class OrderController {
//注入restTemplate
@Autowired
private RestTemplate restTemplate;
@Autowired
//import org.springframework.cloud.client.discovery.DiscoveryClient;
/**
* 注入DiscoveryClient:springcloud提供的获取元数据的工具类,调用方法获取服务的元数据信息
*/
private DiscoveryClient discoveryClient;
/**
*
* @param id 商品id
* @return
* 通过订单系统,调用商品服务根据id查询商品信息
* 1-需要配置商品服务
* 2-需要调用商品服务
*/
@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id){
Product product = null;
//根据元数据的主机地址和端口号拼接请求微服务的URL
product = restTemplate.getForObject("http://service-product/product/1",Product.class);
return product;
}
}
启动类:
2-负载均衡
服务端负载均衡:nginx F5
客户端负载均衡:ribbon
Ribbon是一个典型的客户端负载均衡器,Ribbon会获取服务的所有地址,根据内部的负载均衡算法,获取本次请求的有效地址.
- 准备两个商品微服务(9001,9011)
修改ProductController
package cn.itcast.product.controller;
import cn.itcast.product.entity.Product;
import cn.itcast.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@Value("${server.port}")
private String port;
@Value("${spring.cloud.client.ip-address}")
private String ip;
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id){
Product product = productService.findById(id);
product.setProductName("访问的服务地址:"+ip+":"+port);
return product;
}
@RequestMapping(value = "",method = RequestMethod.POST)
public String save(@RequestBody Product product){
productService.save(product);
return "保存成功";
}
}
- 在订单系统中远程以负载均衡的形式调用商品服务
由于使用了@LoadBalanced
f5
轮询的负载均衡
标签:负载,spingcloud,RestTemplate,product,springframework,org,import,annotation,ribbon From: https://blog.51cto.com/u_12528551/5900122