基于搭建好的Eureka Server+Eureka Client:https://www.cnblogs.com/xuyiqing/p/10861541.html

有了服务,那么现在学习如何调用服务

上文搭建的是商品服务,以下搭建订单服务,订单服务调用商品服务

对Eureka Client进行改造,方便以后得到数据来源

在商品服务的Controller层注入端口号,并进行回显:

package org.dreamtech.product.controller;

import org.dreamtech.product.domain.Product;
import org.dreamtech.product.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/api/product")
public class ProductController { @Value("${server.port}")
private String port; private final ProductService productService; @Autowired
public ProductController(ProductService productService) {
this.productService = productService;
} @RequestMapping("/list")
public Object list() {
return productService.getProductList();
} @RequestMapping("/find")
public Object findById(@RequestParam("id") int id) {
Product product = productService.findById(id);
Product result = new Product();
BeanUtils.copyProperties(product,result);
result.setName(result.getName()+" data from port="+port);
return result;
} }

启动Eureka Server,8761端口

启动三个商品服务,一个项目8771端口,一个8772端口,一个8773端口:

多实例启动方法如下图

新建一个SpringBoot项目order-service:

Web模块必须的,由于订单服务本身也是服务需要Eureka,最后负载均衡调用Ribbon

订单服务开发(模拟实现):

实体类

package org.dreamtech.orderservice.domain;

import java.io.Serializable;
import java.util.Date; public class ProductOrder implements Serializable {
//ID
private int id;
//商品名称
private String productName;
//订单号
private String tradeNo;
//价格
private int price;
//创建时间
private Date createTime;
//用户ID
private int userId;
//用户名
private String userName; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public int getUserId() {
return userId;
} public void setUserId(int userId) {
this.userId = userId;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
} public String getTradeNo() {
return tradeNo;
} public void setTradeNo(String tradeNo) {
this.tradeNo = tradeNo;
} public int getPrice() {
return price;
} public void setPrice(int price) {
this.price = price;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}

为了方便,和商品服务一样,不调用数据库,只做简单的模拟:

在SpringBoot启动类中加入Bean

package org.dreamtech.orderservice;

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();
}
}

配置文件对端口和服务名称进行配置:

server:
port: 8781
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: order-service

Controller:

package org.dreamtech.orderservice.controller;

import org.dreamtech.orderservice.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/order")
public class OrderController {
private final ProductOrderService productOrderService; @Autowired
public OrderController(ProductOrderService productOrderService) {
this.productOrderService = productOrderService;
} @RequestMapping("/save")
public Object save(@RequestParam("user_id") int userId, @RequestParam("product_id") int productId) {
return productOrderService.save(userId, productId);
}
}

Service:

package org.dreamtech.orderservice.service;

import org.dreamtech.orderservice.domain.ProductOrder;

public interface ProductOrderService {
/**
* 下单接口
*
* @param userId 用户ID
* @param productId 商品ID
* @return ProductOrder
*/
ProductOrder save(int userId, int productId);
}
package org.dreamtech.orderservice.service.impl;

import org.dreamtech.orderservice.domain.ProductOrder;
import org.dreamtech.orderservice.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import java.util.Date;
import java.util.Map;
import java.util.UUID; @Service
public class ProductOrderServiceImpl implements ProductOrderService { private final RestTemplate restTemplate; @Autowired
public ProductOrderServiceImpl(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
} @Override
@SuppressWarnings("unchecked")
public ProductOrder save(int userId, int productId) { Map<String, Object> productMap = restTemplate.getForObject("http://product-service/api/product/find?id=" + productId, Map.class); ProductOrder productOrder = new ProductOrder();
productOrder.setCreateTime(new Date());
productOrder.setUserId(userId);
productOrder.setTradeNo(UUID.randomUUID().toString()); if (productMap != null) {
productOrder.setProductName(productMap.get("name").toString());
productOrder.setPrice(Integer.parseInt(productMap.get("price").toString()));
}
return productOrder;
}
}

注意:getForObject方法的url中product-service是我在商品服务中配置的名称

启动项目,如果正常情况,Eureka应该显示如图:

访问http://localhost:8781/api/order/save?user_id=1&product_id=2多次,我将多次的返回结果记录在下:

{"id":0,"productName":"iPhone2 data from port=8771","tradeNo":"8beb0fe0-83ef-4d23-ae53-9399bac7eacc","price":2222,"createTime":"2019-05-15T03:21:12.432+0000","userId":1,"userName":null}
{"id":0,"productName":"iPhone2 data from port=8773","tradeNo":"09544f1d-462b-413a-b14a-cc9d599bce39","price":2222,"createTime":"2019-05-15T03:21:48.467+0000","userId":1,"userName":null}
{"id":0,"productName":"iPhone2 data from port=8771","tradeNo":"543e83a7-3e58-48bb-8aba-bd7635a10131","price":2222,"createTime":"2019-05-15T03:21:57.244+0000","userId":1,"userName":null}
{"id":0,"productName":"iPhone2 data from port=8772","tradeNo":"b30bbd40-49e8-4001-917a-0ae36b172463","price":2222,"createTime":"2019-05-15T03:22:06.509+0000","userId":1,"userName":null}

可以观察到,我开启了三个商品服务,这里自动在三个服务中进行了负载均衡,8771-8773随机访问

在SpringBoot启动类中加入Bean是一种方式,还有另一种调用方式:

不过还是推荐第一种

package org.dreamtech.orderservice.service.impl;

import org.dreamtech.orderservice.domain.ProductOrder;
import org.dreamtech.orderservice.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 java.util.Date;
import java.util.Map;
import java.util.UUID; @Service
public class ProductOrderServiceImpl implements ProductOrderService {
private final LoadBalancerClient loadBalancer; @Autowired
public ProductOrderServiceImpl(, LoadBalancerClient loadBalancer) {
this.loadBalancer = loadBalancer;
} @Override
@SuppressWarnings("unchecked")
public ProductOrder save(int userId, int productId) { ServiceInstance instance = loadBalancer.choose("product-service");
String url = String.format("http://%s:%s/api/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()); if (productMap != null) {
productOrder.setProductName(productMap.get("name").toString());
productOrder.setPrice(Integer.parseInt(productMap.get("price").toString()));
}
return productOrder;
}
}

自定义负载均衡策略:

Ribbon默认是轮询策略

比如我想要使用随机策略,配置如下:

product-service:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

注意:服务名的一致,比如区分"_"和"-"

通常情况下,不需要改变策略,轮询策略为最佳

但是如果有一个好机器,一堆差机器,那么可以调整好机器的权重

Spring Cloud(3):Ribbon的使用的更多相关文章

  1. spring cloud 使用ribbon简单处理客户端负载均衡

    假如我们的multiple服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改 ...

  2. spring cloud: 关闭ribbon负载均衡

    spring cloud: 关闭ribbon负载均衡 1.eureka服务 2.2个user服务:7900/7901 3,movie服务 movie服务去请求 user的用户信息,而此时只想请求790 ...

  3. Spring Cloud 之 Ribbon

    新建Spring Boot工程,命名为ribbon 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"? ...

  4. 笔记:Spring Cloud Feign Ribbon 配置

    由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...

  5. Spring cloud 之Ribbon(一)基本使用

    简介 Spring cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它是基于Netflix的Riboon实现的.Ribbon是客户端负载均衡器,这有别语例如Nginx服务端负载 ...

  6. Spring Cloud Gateway Ribbon 自定义负载均衡

    在微服务开发中,使用Spring Cloud Gateway做为服务的网关,网关后面启动N个业务服务.但是有这样一个需求,同一个用户的操作,有时候需要保证顺序性,如果使用默认负载均衡策略,同一个用户的 ...

  7. 从零开始学spring cloud(六) -------- Ribbon

    一.Ribbon介绍 Ribbon就是客户端侧负责均衡实现的一种方式,那么Ribbon是什么呢? Ribbon是Netflix发布的云中间层服务开源项目,其主要功能是提供客户端侧负载均衡算法.Ribb ...

  8. spring cloud 自定义ribbon客户端

    一.自定义Ribbon客户端-[方式一]配置类 1.1.自定义负载规则 增加RibbonConfiguration.java配置类 public class RibbonConfiguration { ...

  9. Spring Cloud Feign Ribbon 配置

    由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...

  10. Spring Cloud之Ribbon与Nginx区别

    客户端负载均衡器 在SpringCloud中Ribbon负载均衡客户端,会从eureka注册中心服务器端上获取服务注册信息列表,缓存到本地. 让后在本地实现轮训负载均衡策略. Ribbon与Nginx ...

随机推荐

  1. python+selenium简单实现拖动元素实例

    from  selenium  import  webdriver#引入ActionChains类from  selenium.webdriver.common.action_chains  impo ...

  2. Linux Screen超简明教程

    1.安装Screen 大多数情况下,系统已经安装好了screen.如果没有,可以用下面的命令来安装: CentOS系统中执行:yum install screen Debian/Ubuntu系统执行: ...

  3. 具体问题:3、hibernate跟Mybatis/ ibatis 的区别,为什么选择?

    第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...

  4. wpf label下划线不显示的问题

    突然发现label设置content的值为字符串时,如果字符串中包含_的话,在展示出来时下划线就不见了,百度了一下,发现了问题根源,说的label的ContentPresenter默认将下划线处理成快 ...

  5. Hive 进阶

    两种情况下不走map-reduce: 1. where ds >' ' //ds 是partition 2. select * from table //后面没有查询条件,什么都没有 1.建表 ...

  6. 【mysql--sql语句小问题总结】

    1:查询过滤条件不为空: <select id="getActivityByIdAndEx" resultMap="BaseResultMap" > ...

  7. C#在Linux上的开发指南

    本人才疏学浅,在此记录自己用C#在Linux上开发的一点经验,写下这篇指南.(给想要在Linux上开发C#程序的朋友提供建议) 目前在Linux上跑的网站:http://douxiubar.com | ...

  8. SQL Server事务回滚对自增键的影响

    SQL Server事务回滚时是删除原先插入导致的自增值,也就是回滚之前你你插入一条数据导致自增键加1,回滚之后还是加1的状态 --如果获取当前操作最后插入的identity列的值:select @@ ...

  9. [CentOS7] 使用磁盘分割建立swap

    声明:本文主要总结自:鸟哥的Linux私房菜-第七章.Linux 磁碟與檔案系統管理,如有侵权,请通知博主 通过 vim /etc/fstab 使该新建的swap分区每次开机都自己挂载 至此,已经完成 ...

  10. Go:表驱动单元测试

    Go:表驱动单元测试 单元测试相当的重要,这几天实习由于单元测试没写好所以被骂了emmm 痛定思痛,立刻上网学习了一下,总算达到了预期的效果,所以写一篇文章记录一下 首先安装gotests $go g ...