Ribbon+Eureka实现负载均衡

记录一下Ribbon+Eureka实现负载均衡初次练习
首先是service
主配置文件

server.port=9100
#设置该服务注册中心的hostname,也是域名,可以自己去设置
eureka.instance.hostname=localhost
#由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种自己向自己注册的默认行为
eureka.client.register-with-eureka=false
#表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它不需要去检索其他服务
eureka.client.fetch-registry=false
#指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

记得在启动类上加上@EnableEurekaServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

直接在浏览器地址栏访问 localhost:9100 就可以进入注册中心

实现负载均衡的轮询策略需要多个Provider

Provider01

#指定服务名字 这个名称将在服务消费者时被调用
spring.application.name=010-springcloud-eureka-client-provider
#指定eureka的访问地址
eureka.client.service-url.defaultZone=http://localhost:9100/eureka
#指定Tomcat的端口号需要避免和其他的Tomcat端口冲突
server.port=8081
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class providerController {

    @RequestMapping(value = "/test")
    public String test(){
        return "8081提供者01的ribbon测试";
    }
}

Provider02

#指定服务名字 这个名称将在服务消费者时被调用
spring.application.name=010-springcloud-eureka-client-provider
#指定eureka的访问地址
eureka.client.service-url.defaultZone=http://localhost:9100/eureka
#指定Tomcat的端口号需要避免和其他的Tomcat端口冲突
server.port=8082
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class providerController {

    @RequestMapping(value = "/test")
    public String test(){
        return "8082提供者02的ribbon测试";
    }
}

两个提供者的主配置文件除了端口号完全一样。spring.application.name=010-springcloud-eureka-client-provider
这个name后面的值是随意的,需要一个唯一的名字。两个提供者的name一样

最后需要一个消费者访问
Consumer

#将自己注册到注册中心
spring.application.name=010-springcloud-eureka-ribbon-consumer
#将服务同时注册到多个注册中心,其实当Eureka注册中心实现集群以后,
# 那么将服务注册到任意一个Eureka的注册中心后,数据服务都会同步到其他的Eureka注册中心,
# 但是实际应用时还是建议将服务分别注册到全部的Eureka集群的所有服务中,
# 这个防止某个以Eureka出现故障后服务仍然可以注册成功
eureka.client.service-url.defaultZone=http://localhost:9100/eureka
#指定Tomcat的端口号需要避免和其他的Tomcat端口冲突
server.port=8080
#每间隔2s,向服务端发送一次心跳,证明自己依然"存活"
eureka.instance.lease-renewal-interval-in-seconds=2
#告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我踢出掉
eureka.instance.lease-expiration-duration-in-seconds=10

需要一个配置类

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();
    }
}
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 javax.annotation.Resource;

@RestController
public class providerController {

    @Resource
    private RestTemplate restTemplate;
    @RequestMapping(value = "/test")
    public String test(){
        ResponseEntity<String>result=restTemplate.getForEntity("http://010-SPRINGCLOUD-EUREKA-CLIENT-PROVIDER/test",String.class);
        String body=result.getBody();
        return "8080消费者的ribbon测试,使用了负载均衡"+body;
    }
}

最后将所有的服务一一启动。先启动服务,再启动提供者,最后消费者
在这里插入图片描述
这里是两个提供者。
然后访问消费者的地址,他会一次的去访问提供者1和提供者2

在这里插入图片描述
再次访问这个地址

在这里插入图片描述

依次访问两个提供者。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值