手把手搭建SpringCloud,第二篇——Ribbon实现客户端负载均衡

开发工具:STS

新建项目前,请确认工作环境已经配置好了Maven。

本章提及的Eureka Server 请阅读上一篇博客这里 https://blog.csdn.net/Lee_SmallNorth/article/details/95975546


简介:

 Eureka 与 Ribbon配合使用的架构图

 


注意:以下方法是新建两个项目,一个作为普通接口调用(可直接新建一个项目然后配置EurekaClient就可以了),一个作为负载均衡使用,或者直接拷贝,然后把配置端口号修改一下。

新建项目1,作为普通接口使用

 application.yml

server:
  port: 1004
  
eureka:
  client:
    service-url:
      #配置Eureka Server的地址,因为是要注册服务到Eureka Server
      defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
  instance:
    #将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
    prefer-ip-address: true
    #指明ip
    instance-id: 10.8.65.38:dev-ribbon-client:1004
    #告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
    lease-expiration-duration-in-seconds: 2
    #每间隔1s向服务器发送一次心跳,证明自己还活着
    lease-renewal-interval-in-seconds: 1
    
spring:
  application:
    name: dev-ribbon-client  

 启动类添加注解

 编写测试Controller

package com.portal.controller;

import java.util.HashMap;
import java.util.Map;

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

@RestController
public class RibbonController {
	
	@GetMapping("/getRibbon")
	public Map<String, Object> getRibbon()
	{
		Map<String, Object> map = new HashMap<>();
		map.put("姓名", "张三");
		map.put("年龄", 25);
		map.put("性别", "男");
		map.put("电话", "18020000111");
		return map;
	}

}

启动测试,没有问题的话继续 


新建项目2,作为负载均衡使用

 

application.yml

server:
  port: 1003
  
eureka:
  client:
    service-url:
      #配置Eureka Server的地址,因为是要注册服务到Eureka Server
      defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
  instance:
    #将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
    prefer-ip-address: true
    #指明ip
    instance-id: 10.8.65.38:dev-ribbon-client:1003
    #告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
    lease-expiration-duration-in-seconds: 2
    #每间隔1s向服务器发送一次心跳,证明自己还活着
    lease-renewal-interval-in-seconds: 1
    
spring:
  application:
    name: dev-ribbon-client  

这里要注意看下,我是把项目1配置文件中服务名称和项目2一致的。

启动类

package com.portal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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 //开启服务注册
public class SpringcloudRibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudRibbonApplication.class, args);
	}
	/**
	 * ribbon + resttemplate调用方式
	 * @return
	 */
	@Bean //注入实体类
	@LoadBalanced //实现负载均衡
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}

}

 编写测试类Controller

package com.portal.controller;

import java.util.Map;

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 RibbonController {
	
	@Autowired
	private RestTemplate restTemplate;
	
	@GetMapping("/getRibbonMap")
	public Map<String,Object> getRibbonMap()
	{
		//http://dev-ribbon-client/getRibbon   这个就是项目1的测试方法,通过Eureka中服务名称直接调用
		return restTemplate.getForObject("http://dev-ribbon-client/getRibbon", Map.class);
	}

}

到此项目搭建完毕,准备测试,启动Eureka Server ,项目1,项目2,进入Eureka管理页面

 测试负载均衡接口

成功,礼花大炮哄起来,哈哈哈


总结:笔者写博客不仅仅是为了分享,也是为了给自己做笔记。

以上方法亲测有效,如果问题可下方回复,

不当之处,请多指教

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值