使用RestTemplate进行应用间通信

spring cloud学习笔记

上文 使用多个 Eureka Server
下文 使用Feign进行应用间通信

使用RestTemplate进行应用间通信

上文中我们已经准备好了 Eureka Server,在测试环境中,就不需要多个Eureka了,启动一个应用即可,为了方便我们打包运行
java -jar .\target\eureka-0.0.1-SNAPSHOT.jar

我们再创建两个 Spring Boot 项目,分别为product,consumer 并修改配置文件注册到 Eureka 中

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

记得在启动类添加注解 @EnableEurekaClient

product项目中

添加controller

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ServerController {
    @GetMapping("echo")
    public String echoeMessage(HttpServletRequest request){
        return "this is server "+ request.getServerName()+":"+request.getServerPort();
    }
}
consumer 项目中

添加config类,其中@LoadBalanced注解帮助提供一个负载均衡的RestTemplate Bean

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

添加 controller,其中 restTemplate直接在url中用在Eureka中注册的应用名代替地址和端口,在product项目中,我们配置的应用名

spring:
  application:
    name: product

代码调用(应用名不区分大小写)
restTemplate.getForObject("http://product/echo",String.class);

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class ClientController {

    private RestTemplate restTemplate;

    @Autowired
    public ClientController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/product-msg")
    public String getProductMsg(){
        String resp = restTemplate.getForObject("http://product/echo",String.class);
        log.info("response={}",resp);
        return resp;
    }
}

通过-Dserver.port=xxxx设置不同的端口,启动多个product实例,idea中:


或者打包执行 java -jar xxxxx.jar --server.port=xxxx
我们启动两个实例,使用8080和8081端口

启动consumer,反复请求地址http://localhost:8180/product-msg
将会得到不同的返回:

this is server 192.168.1.107:8081
this is server 192.168.1.107:8080
this is server 192.168.1.107:8081
this is server 192.168.1.107:8080

可以发现,请求在两个服务器间轮换执行
我们可以在client的配置文件中配置负载均衡的规则,我们把负载均衡规则改为 随机

#应用名
PRODUCT:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

所有规则均在 com.netflix.loadbalancer包下并实现 com.netflix.loadbalancer.IRule接口,默认使用 com.netflix.loadbalancer.RoundRobinRule

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • 一、两种应用间的通信方式 二、Spring Cloud中两种restful调用方式:(示例:用订单服务调用商品服务...
    薛定谔的猫_1406阅读 572评论 1 0
  • 与大狮子先生领完证后就开始琢磨蜜月旅行的计划,因为大狮子无法出国,蜜月目的地只能选择在国内,周遭的同学同事蜜月大多...
    狮子家的喵小姐阅读 734评论 0 1
  • 我不懂你说的再会是什么套路 只想静静地抱着你感受你的温度 你的眼神总是带着难以猜透的自负 却看不清我脸上戏剧般的苦...
    墨竹轻踏雪阅读 224评论 2 5
  • 把家里收拾的无比干净 从床 卫生间 小柜子 书桌 和电视墙 后来袭叔叔说“挺干净” 值了!
    只是找一个地方码子_阅读 198评论 0 0