spring cloud(服务消费者(利用ribbon实现服务消费及负载均衡)——初学二)
Ribbon是一个基于HTTP和TCP客户端的负载均衡器,利用ribbon实现服务消费,并实现客户端的负载均衡。
一、准备工作(利用上一节的内容)
启动服务注册中心
启动computer-service
将computer-service的端口修改为2223,再次启动computer-service
访问 localhost:1111
二、创建消费者
1、创建spring boot项目(利用idea的Spring Initializr快速创建项目)
2、添加ribbon依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.daqsoft</groupId>
<artifactId>customer_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>customer_demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
</properties> <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> <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>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、启动类添加注解开启服务,添加RestTemplate实例并开启负载均衡
package com.daqsoft; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
//用来发现注册服务
@EnableDiscoveryClient
public class CustomerDemoApplication { /**
* 创建实例并开启负载均衡
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(CustomerDemoApplication.class, args);
}
}
4、创建消费方来消费add服务
package com.daqsoft; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @Description Created by liaoxx on 2017-6-12.
*/
@RestController
public class CustomController { @Autowired
private RestTemplate template; @RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(){
return template.getForEntity("http://COMPUTE-SERVICE/add?a=15&b=25",String.class).getBody(); }
}
5、修改配置文件
spring.application.name=ribbon-consumer server.port=3333
#服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
6、启动服务,多次访问 localhost:3333/add
在服务提供方不同端口打印出来的信息,实现了对服务的负载均衡
spring cloud(服务消费者(利用ribbon实现服务消费及负载均衡)——初学二)的更多相关文章
- springcloud干货之服务消费者(ribbon)
本章介绍springcloud中的服务消费者 springcloud服务调用方式有两种实现方式: 1,restTemplate+ribbon, 2,feign 本来想一篇讲完,发现篇幅有点长,所以本章 ...
- Spring Cloud第七篇 | 声明式服务调用Feign
本文是Spring Cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...
- Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...
- spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...
- Spring Cloud ---- 服务消费与负载均衡(Rest + Ribbon )
上一篇主要写了基于Eurake的服务的注册,主要就是创建注册中心,创建服务者,将服务者注册到注册中心,完成服务的暴露.这一篇主要写服务的消费与服务消费的负载均衡. 服务的调用方式有两种,Rest + ...
- 白话SpringCloud | 第四章:服务消费者(RestTemple+Ribbon+Feign)
前言 上两章节,介绍了下关于注册中心-Eureka的使用及高可用的配置示例,本章节开始,来介绍下服务和服务之间如何进行服务调用的,同时会讲解下几种不同方式的服务调用. 一点知识 何为负载均衡 实现的方 ...
- 《Spring Cloud》学习(一) 服务治理!
前言:之前网上学习过Spring Cloud,对于工作上需要是足够了,总归对于一些方面一知半解,最近难得有些闲暇时间,有幸读了崔永超先生的<Spring Cloud 微服务实战>,一方面记 ...
- Spring Cloud系列(三):服务消费与负载均衡
上一篇介绍了服务提供者,有了注册中心和服务提供者,我们就可以进行服务消费了.Spring Cloud可以通过RestTemplate+Ribbon和Feign这两种方式消费服务. 我们仍然在上一篇的项 ...
- 基于Spring Cloud和Netflix OSS 构建微服务-Part 1
前一篇文章<微服务操作模型>中,我们定义了微服务使用的操作模型.这篇文章中,我们将开始使用Spring Cloud和Netflix OSS实现这一模型,包含核心部分:服务发现(Servic ...
随机推荐
- Linux系统各发行版镜像下载(借阅)
Linux各个版本资源下载 Linux系统各发行版镜像下载(持续更新) == Linux系统各发行版镜像下载(2014年10月更新),如果直接下载不了,请使用迅雷下载.并且注意,我的下载地址,在 迅 ...
- 第28章:MongoDB-索引--过期索引(TTL)
①过期索引(TTL) TTL索引是让文档的某个日期时间满足条件的时候自动删除文档,这是一种特殊的索引,这种索引不是为了提高查询速度的,TTL索引类似于缓存,缓存时间到了就过期了,就要被删除了 ②范例: ...
- Perl语言入门
Perl 是 Practical Extraction and Report Language 的缩写,可翻译为 "实用报表提取语言". Perl语法基础: (1)Perl程序由声 ...
- 各版本.NET委托的写法回顾(转)
转自:http://www.csharpwin.com/csharpspace/7548r2766.shtml 在<关于最近面试的一点感想>一文中,Michael同学谈到他在面试时询问对方 ...
- Codeforces Round #535 (Div. 3) 1108C - Nice Garland
#include <bits/stdc++.h> using namespace std; int main() { #ifdef _DEBUG freopen("input.t ...
- Surface 2装机必备软件指南
新买的Surface到货了还不知道有什么用,每天就用来划划点点?有点太浪费了吧!跟哥走,哥给你推荐几款Surface 2装机必备的软件~应用商店,走起~ 初次使用看过来:Win8宝典 如果你是一个像我 ...
- hud 3123 GCC
题目 输入:n 和 mod 输出: Output the answer of (0! + 1! + 2! + 3! + 4! + ... + n!)%m. Constrains 0 < T &l ...
- 冲刺博客NO.6
今天做了什么:通过自学已经掌握对多控件的使用并学会使用进度条 遇到的困难:Handler使用错误,对其理解错误,导致很多误会后来百度发现 Handler是Android系统消息机制抽象出来的一个类(并 ...
- Dnsmasq安装与配置-搭建本地DNS服务器
默认的情况下,我们平时上网用的本地DNS服务器都是使用电信或者联通的,但是这样也导致了不少的问题,首当其冲的就是上网时经常莫名地弹出广告,或者莫名的流量被消耗掉导致网速变慢.其次是部分网站域名不能正常 ...
- 第5件事 做一个有taste的产品人
1.taste的意思是品位,也就是说产品经理应该是一个有品位的产品人.什么叫品位呢?品位指的是对事物有分辨与鉴赏的能力.品位是形象的展示,品位是内在气质的复出,品位是人生价值的体验,品位是道德修养的内 ...