您的位置:首页 > 编程语言 > Java开发

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

2017-09-04 11:03 1101 查看

在实现之前

启动服务注册中心eureka-server

为了试验负载均衡,我们通过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实现了负载均衡,轮询访问。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: