SpringCloud之Ribbon

SpringCloud之Ribbon的使用

个人记录学习



一、准备工作

1.修改服务消费方pom文件

<!--    添加ribbon依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

2. 修改服务消费启动类

package com.yun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author kaka
 * @description consumer
 * @date 2022-07-11 13:26
 */
@SpringBootApplication
@EnableDiscoveryClient  //当前使用eureka的server
public class EuerkaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EuerkaConsumerApplication.class);
    }
    //注入RestTemplate ,使用 @LoadBalanced注解(Ribbon负载均衡,默认轮换)
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

3.编写服务提供方方法

1.服务方代码

  /**
     * @description:获取调用的地址
     * @author: kaka
     * @date: 2022-07-12 17:35:51
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/yettl")
    public String yettl(){
        ServiceInstance serviceInstance =loadBalancerClient.choose("EUREKA-SERVICE");
        return  serviceInstance.getServiceId()+serviceInstance.getHost()+serviceInstance.getPort();
    }

2. 消费方调用

package com.yun;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author kaka
 * @description \
 * @date 2022-07-11 13:28
 */
@RestController
@RequestMapping("/Hello")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    
    @RequestMapping("/tengman")
    public String tengman(){
        String froObject = restTemplate.getForObject("http://EUREKA-SERVICE/hello/yettl",String.class);
        return froObject;
    }
}

4.配置多个服务提供方

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
完成后将一个注册中心,两个服务提供方,一个服务消费方全部启动起来。

5. 进行测试

1. 启动后可以看到注册中心有两个服务提供方(说明刚才的配置已经成功)

在这里插入图片描述

2. 调用消费方接口

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

HappyAcmen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值