SpringCloud的Ribbon+RestTemplate的三种使用方式

阅读好文章,请随手 点击上面,关注我们,免费订阅

方式一:直接使用new实例化RestTemplate对象

@GetMapping("getUserList")

public List getUserList(){

    RestTemplate template = new RestTemplate();

    return template.getForObject(

"http://localhost:8080/getUserList",//

List.class);

}

缺点:

1、url硬编码,如果ip有变动,需要在代码中更改

2、如果client为集群,有多个url,该方法只能配一个url;不能使用集群模式

方式二:注入LoadBalancerClient ,获得应用名称为providerServcidName(备注:服务提供者名称)的应用的其中一个实例,获得url,再使用RestTemplate获取数据,实现负载均衡

@RestController

public class UserController {

    

@Autowired

    private LoadBalancerClient loadBalancerClient;

 

    @GetMapping("getUserList")

    public List getUserList() {

        RestTemplate template = new RestTemplate();

         // 选择服务实例,根据传入的服务名serviceId,

         // 从负载均衡器中挑选一个对应服务的实例。 

        ServiceInstance instance = loadBalancerClient

              .choose("providerServcidName");

        String url = String.format("http://%s:%s", 

              instance.getHost(), 

      instance.getPort() + "/getUserList");

        return template.getForObject(url, List.class);

    }

}

方式三:RestTemplate通过配置注入Spring容器来使用

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对象,直接调用getForObject方法,注意url中直接写应用名称,不要写ip:port

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

public class UserController {

 

    @Autowired

    private RestTemplate restTemplate;

 

    @GetMapping("getUserList")

    public String getUserList() {

        return restTemplate

    .getForObject("http://providerServcidName/getUserList", List.class);

    }

}

如果您觉得本文不错,请别忘了动动手指点击右上角分享到您的朋友圈!

一个聚百万人脉的技术圈子

JAVA乐园
▲长按二维码“识别”关注

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud Nacos是一个非常流行的微服务注册中心和配置中心,而Ribbon则是一个负载均衡组件。结合起来,可以实现服务的自动发现和负载均衡。 下面是一个简单的示例: 1. 在pom.xml文件中添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> ``` 2. 在application.properties或application.yml中配置Nacos和Ribbon: ```yaml # Nacos配置 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 # Ribbon配置 service-provider.ribbon.listOfServers=localhost:8081,localhost:8082 service-provider.ribbon.NIWSServerListClassName=com.netflix.loadbalancer.ConfigurationBasedServerList ``` 3. 创建一个Ribbon的配置类,用于自定义负载均衡策略等: ```java @Configuration public class RibbonConfig { @Bean public IRule ribbonRule() { return new RandomRule(); // 随机负载均衡策略 } @Bean public IPing ribbonPing() { return new PingUrl(false, "/health"); // 检查服务健康状态的URL } } ``` 4. 在服务消费方的代码中,注入RestTemplate对象,并使用@LoadBalanced注解: ```java @RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @GetMapping("/hello") public String hello() { String url = "http://service-provider/hello"; return restTemplate.getForObject(url, String.class); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } } ``` 在这里,我们使用了"http://service-provider/hello"作为服务提供方的URL,而不是具体的IP和端口。这是因为Ribbon会根据服务名自动选择一个可用的实例。 以上就是使用Spring Cloud Nacos和Ribbon调用服务的基本步骤。需要注意的是,在使用Nacos作为注册中心时,服务提供方需要在启动时注册自己。可以参考Nacos的官方文档或者Spring Cloud官方文档进行配置和使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值