springcloud ribbon通过自定义yml配置文件实现负载均衡

1.pom文件:

<!-- 注册eureka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 打印日志 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--  web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.启动类:

package com.ljf.weifuwu.springcloud.ribbon.yml;

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;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableEurekaClient
public class RibbonYmlConsumerApp
{
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main( String[] args )
    {    //ngix是服务端的负载均衡;ribbon是客户端的负载均衡器
        SpringApplication.run(RibbonYmlConsumerApp.class, args);
        System.out.println( "ribbon yml consumer启动起来了!" );
    }
}

3.controller:
package com.ljf.weifuwu.springcloud.ribbon.yml.controller;
import com.ljf.weifuwu.springcloud.ribbon.yml.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class RibbonYmlController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/consumer-ribbon/{id}")
    public User findById(@PathVariable Long id) {
                ServiceInstance serviceInstance = this.loadBalancerClient.choose("ms-eureka-provider");
              System.out.println("随机:" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());
        return this.restTemplate.getForObject("http://ms-eureka-provider/eureka-provider/" + id, User.class);
    }
}

4.model:

package com.ljf.weifuwu.springcloud.ribbon.yml.model;

import java.math.BigDecimal;

public class User {
    private Long id;

    private String username;

    private String name;

    private Short age;

    private BigDecimal balance;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return this.age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }
}

5.配置文件:

server:
  port: 8001
spring:
  application:
    name: ms-ribbon-yml-consumer
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://ljf:123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

ms-eureka-provider:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

对ms-eureka-provider模块项目实行ribbon的负载均衡策略为随机。

6.启动服务:ms-erueka-center(8761)、ms-ribbon-yml-consumer(8001)、ms-eureka-provider(7900)、ms-eureka-provider(7901)

7.查看效果:

随机::ms-eureka-provider:192.168.1.14:7900
随机::ms-eureka-provider:192.168.1.14:7900
随机::ms-eureka-provider:192.168.1.14:7900
随机::ms-eureka-provider:192.168.1.14:7901
随机::ms-eureka-provider:192.168.1.14:7900
随机::ms-eureka-provider:192.168.1.14:7901
随机::ms-eureka-provider:192.168.1.14:7901
随机::ms-eureka-provider:192.168.1.14:7900
随机::ms-eureka-provider:192.168.1.14:7901
2020-06-07 22:35:08.571  INFO 7496 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值