Spring Cloud 学习系列:(五)实现客户端侧负载均衡——Ribbon

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
3、建立一个服务消费者

(1)、pom.xml 继承父 pom 文件,并加入以下依赖。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

com.riemann

microservice-eureka-ribbon

0.0.1-SNAPSHOT

com.riemann

microservice-provider-service-ribbon

0.0.1-SNAPSHOT

jar

microservice-provider-service-ribbon

Eureka Ribbon Client

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-ribbon

(2)、在工程的配置文件指定服务的注册中心地址为http://peer1:8761/eureka/,程序名称为 microservice-provider-service-ribbon,程序端口为8763。application.yml 如下:

server:

port: 8763

spring:

application:

name: microservice-provider-service-ribbon

eureka:

client:

service-url:

defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

(3)、在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

package com.riemann.microserviceproviderservicehi;

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.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

@SpringBootApplication

@EnableEurekaClient

@EnableDiscoveryClient

public class MicroserviceProviderServiceRibbonApplication {

public static void main(String[] args) {

SpringApplication.run(MicroserviceProviderServiceRibbonApplication.class, args);

}

@Bean

@LoadBalanced

RestTemplate restTemplate() {

return new RestTemplate();

}

}

(4)、写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

package com.riemann.microserviceproviderservicehi;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.web.client.RestTemplate;

@Service

public class HelloService {

@Autowired

RestTemplate restTemplate;

public String hiService(String name) {

return restTemplate.getForObject(“http://microservice-provider-service-hi/hi?name=” + name, String.class);

}

}

(5)、写一个controller,在controller中用调用HelloService 的方法,代码如下:

package com.riemann.microserviceproviderservicehi;

import org.springframework.beans.factory.annotation.Autowired;

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

import org.springframework.web.bind.annotation.RequestParam;

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

@RestController

public class HelloControler {

@Autowired

HelloService helloService;

@GetMapping(value = “/hi”)

public String hi(@RequestParam String name) {

return helloService.hiService(name);

}

}

最后

学习视频:

大厂面试真题:

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
ng name) {

return helloService.hiService(name);

}

}

最后

学习视频:

[外链图片转存中…(img-5gWTMnKg-1714660063817)]

大厂面试真题:

[外链图片转存中…(img-fvYDTgZW-1714660063818)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 22
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值