二、ribbon 实现客户端侧负载均衡 实例

83 阅读1分钟

1、 工具及软件版本

JDK: 1.8

Spring Boot: 2.3.0.RELEASE

Spring Cloud: Hoxton.SR6

Maven: 3.5.3

2、实现客户端侧负载均衡

2.1、创建Maven项目,artifactId是ribbon-server,pom.xml内容如下:

<project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSch…"

xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4…

4.0.0

com.baiyilanjian.cloud

ribbon-server

1.0.0

jar

org.springframework.boot

spring-boot-starter-parent

2.3.0.RELEASE

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-dependencies

Hoxton.SR6

pom

import

org.springframework.boot

spring-boot-maven-plugin

2.2、spring boot的启动类,添加注解,@LoadBalanced 实例化RestTemplete,代码如下:

package com.baiyilanjian.cloud;

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.context.annotation.Primary;

import org.springframework.web.client.RestTemplate;

/**

  • Ribbon 实现客户端侧负载均衡

  • @author

*/

@SpringBootApplication

@EnableEurekaClient

public class RibbonServerApplication {

@LoadBalanced

@Bean

RestTemplate loadBalanced() {

return new RestTemplate();

}

@Primary

@Bean

RestTemplate restTemplate() {

return new RestTemplate();

}

public static void main(String[] args) {

SpringApplication.run(RibbonServerApplication.class, args);

}

}

2.3,修改application.yml配置文件,代码如下:

spring:

application:

name: ribbon-server#服务名称

server:

指定该Eureka实例的端口

port:8086

logging:

level:

com.netflix: warn

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8081/eureka/

2.4、编写测试Controller TestController.java
package com.baiyilanjian.cloud.controller;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

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

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.MediaType;

import org.springframework.http.ResponseEntity;

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

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

import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

import java.util.Map;

@RestController

@RequestMapping("/test")

public class TestController {

private static final Loggerlogger = LoggerFactory.getLogger(TestController.class);

@Autowired

private RestTemplaterestTemplate;

@RequestMapping("test")

public String helloRibbon()throws JsonProcessingException {

ObjectMapper objectMapper =new ObjectMapper();

RestTemplate restTemplate =new RestTemplate();

String url ="http://xxxx";

HttpHeaders headers =new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);

Map map =new HashMap<>();

map.put("name","");

map.put("password","");

String str = objectMapper.writeValueAsString(map);

HttpEntity request =new HttpEntity(str, headers);

ResponseEntity response = restTemplate.postForEntity( url, request , String.class );

System.out.println(response.getBody());

return response.getBody();

}

}