SpringCloud 笔记 (二)---- 简单搭建一个服务消费者,实现简单的ribbon负载均衡

在实现之前

  1. 启动服务注册中心eureka-server
  2. 为了试验负载均衡,我们通过java –jar命令行的方式启动两个不同的端口hello-service
Java –jar hello-service-0.0.1-SNAPSHOT.jar –server.port=8082
Java –jar hello-service-0.0.1-SNAPSHOT.jar –server.port=8083

此时可发现注册中心中hello-service有了两个实例单元,如下:
这里写图片描述

创建消费者工程ribbon-consumer

Pom依赖

省去了基本的父依赖,web依赖与maven打包插件等

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- ribbon -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!-- 此版本和springboot版本有关,可查官网,我这里用的springboot1.5,所以用了Dalston -->
                <version>Dalston.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

主类

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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/*
 * 注册为eureka客户端应用,获得服务发现的能力
 */
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {

    @Bean//spring bean实例
    @LoadBalanced//开启客户端负载均衡
    RestTemplate restTemplate() {
        return new RestTemplate();
    }


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

请求

@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value="/ribbon-consumer",method=RequestMethod.GET)
    public String helloConsumer() {
        return restTemplate.getForEntity("http://HELLO-SERVICE/user/hello", String.class).getBody();
    }
}

Application.properties:

#name
spring.application.name=ribbon-consumer

#port ,can't be same with other
server.port=9001

# register,the same as the service,other you'll not find the service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

此时服务提供者的地址应是http://localhost:1111/eureka/,必须一致否则发现不了
启动此消费者,注册中心中发现消费者
这里写图片描述
再通过http://localhost:9001/ribbon-consumer请求:
这里写图片描述
成功返回。
而且我们可以在ribbon-consumer应用的控制台中看到如下信息:
Ribbon输出了当前客户端维护的HELLO-SERVICE的服务列表情况,其中包含了各个实例的位置,其就是按照此信息进行轮询访问,实现基于客户端的负载均衡。另外还输出了一些其他非常有用的信息,如对各个实例的请求总数量,第一次连接信息,上一次连接信息,总得请求失败数量等。
DynamicServerListLoadBalancer for client HELLO-SERVICE initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=HELLO-SERVICE,current list of Servers=[CC-PC:8083, CC-PC:8082]

此次请求8083服务控制台打印:
2017-09-04 10:56:47.573 INFO 8104 — [nio-8083-exec-6] c.e.clientservice.web.UserController : /hello,host:CC-PC, service_id:hello-service
再次请求,8082服务控制台打印:
2017-09-04 10:56:02.498 INFO 7560 — [io-8082-exec-10] c.e.clientservice.web.UserController : /hello,host:CC-PC, service_id:hello-service

证明ribbon实现了负载均衡,轮询访问。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值