前言:

必需学会SpringBoot基础知识

简介:

spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。

工具:

JDK8

apache-maven-3.5.2

IntelliJ IDEA 2017.3 x64

ribbon 简介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies. —–摘自官网

ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。

一、准备工作

本篇是基于第一篇为基础, 首先, 启动 eureka-server:8761 和 eureka-client 项目的dev:8762, pro:8763 分别启动两个实例. 临时搭建一个小集群, 然后访问 127.0.0.1:8761 查看 eureka-client 两个实例是否启动了.

二、新建服务消费者 (eureka-ribbon)

2.1 pom.xml

<?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.lwc</groupId>
<artifactId>eureka-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-ribbon</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>Edgware.SR2</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>

2.2  通过 application.yml 跳转 application-dev.yml 配置程序名, 端口 和指定注册中心 如下:

spring:
application:
name: eureka-ribbon
profiles:
active: ${@profileActiv@:dev}
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764

2.3  通过@EnableDiscoveryClient向服务中心注册, 并且利用ioc注入@Bean, 然后通过@LoadBalanced表明restTemplate()开启负载均衡功能; 如下:

package com.lwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* @author Eddie
*/
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication { public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}
}
package com.lwc.template;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; /**
* @author eddie.lee
* @Package com.lwc.template
* @ClassName RestConfig
* @description
* @date created in 2018-03-26 19:54
* @modified by
*/
@Component
public class RestConfig { @Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
} }

2.4 创建服务层测试使用, ribbonService() 里面url使用了eureka-client服务器名称代替127.0.0.1:8762; 如下:

package com.lwc.service;

/**
* @author eddie.lee
* @Package com.lwc.service
* @ClassName RibbonService
* @description
* @date created in 2018-03-26 19:55
* @modified by
*/
public interface RibbonService { String ribbonService(String name); }
package com.lwc.service.impl;

import com.lwc.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; /**
* @author eddie.lee
* @Package com.lwc.service.impl
* @ClassName RibbonServiceImpl
* @description
* @date created in 2018-03-26 19:57
* @modified by
*/
@Service
public class RibbonServiceImpl implements RibbonService { @Autowired
private RestTemplate restTemplate; @Override
public String ribbonService(String name) {
final String url = "http://eureka-client/eureka/client?name=" + name;
return restTemplate.getForObject(url, String.class);
} }

2.5 创建 controller 作为调用测试服务层使用; 如下:

package com.lwc.controller;

import com.lwc.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @author eddie.lee
* @Package com.lwc.controller
* @ClassName RibbonControler
* @description 服务消费者
* @date created in 2018-03-26 20:02
* @modified by
*/
@RestController
@RequestMapping("/eureka")
public class RibbonControler { @Autowired
private RibbonService ribbonService; @GetMapping("/ribbon")
public String Ribbon(@RequestParam String name) {
return ribbonService.ribbonService(name);
} }

新手解析: ribbon相当于 请求ribbon的时候会随机访问eureka-client:8762 和 eureka-client:8763,

为什么? 如果一台eureka-client:8762挂了, 还有eureka-client:8763, 保证服务正常使用;

eureka-ribbon
http://localhost:8764/eureka/ribbon?name=eddie


标签

2-1

源码下载
https://github.com/eddie-code/SpringCloudDemo#springclouddemo

【SpringCloud】第二篇: 服务消费者(rest+ribbon)的更多相关文章

  1. springcloud干货之服务消费者(ribbon)

    本章介绍springcloud中的服务消费者 springcloud服务调用方式有两种实现方式: 1,restTemplate+ribbon, 2,feign 本来想一篇讲完,发现篇幅有点长,所以本章 ...

  2. SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  3. SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  4. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/ 本文出自方志朋的博客 在上一篇文章,讲了 ...

  5. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  6. Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)

    Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...

  7. 跟我学SpringCloud | 第二篇:注册中心Eureka

    Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...

  8. 「 从0到1学习微服务SpringCloud 」05服务消费者Fegin

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  9. 【SpringCloud】第三篇: 服务消费者(Feign)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

随机推荐

  1. 【luogu P3375 KMP字符串匹配】 模板

    题目链接:https://www.luogu.org/problemnew/show/P3375 精华:在每次失配后不从头匹配而是尝试找一个新的开始并且是新开始的位置最长的相同前缀和后缀. 实际上KM ...

  2. JavaScript中的Map和Set

    JavaScript的默认对象表示方法{}可以视为其他语言中的Map或者Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串,但实际上Numbe ...

  3. Vue nodejs商城-订单模块

    一.订单列表渲染 新建OrderConfirm.vue订单确认页面,添加路由 src/router/index.js添加路由 import OrderConfirm from '@/views/Ord ...

  4. Django-rest-framework(六)filter,ordering,search

    filter queryset 使用request.user相关的queryset class PurchaseList(generics.ListAPIView): serializer_class ...

  5. 并发编程之多线程基础-join方法及优先级(五)

    join()方法作用 当在主线程当中执行到t1.join()方法时,就认为主线程应该把执行权让给t1 优先级 现代操作系统基本采用时分的形式调度运行的线程,线程分配得到的时间片的多少决定了线程使用处理 ...

  6. iOS-GCD使用详解

    前言 对初学者来说,GCD似乎是一道迈不过去的坎,很多人在同步.异步.串行.并行和死锁这几个名词的漩涡中渐渐放弃治疗.本文将使用图文表并茂的方式给大家形象地解释其中的原理和规律. 线程.任务和队列的概 ...

  7. java各种业务解决方案总结

    最近有点时间,突然感慨良多,感觉辛苦工作这么久什么都没有,总结了以前的工作,将接触的主要工具列出来,希望给大家解决问题做参考.相关工具都是实践检验过的 1.数据库 (1).内存数据库 redis (2 ...

  8. fastRPC的数据库服务

    根据整理的RPC模型,在此上,根据最近的项目,发布了DB服务,操作数据库.以RPC模型,发布数据库的操作服务,主要发送SQL语句,在服务端执行:同时引入了流行的数据库连接池:服务端还发布了文件接收服务 ...

  9. 初探css3

    属性选择器: 1.完全匹配的属性选择器. 就是完全匹配的字符串. [id=article]{ color:red; } 2.包含匹配选择器.包含有指定的字符串. 语法是:[attribute*=val ...

  10. c++stl应用入门

    在这篇中,我会讲几个简单易懂且比较常用的stl函数,这些函数在noip系列考试中往往被允许使用(既然让用我们自然不用手码了...) (末尾有惊喜!) 1.sort 绝大部分刚入门的oier第一个接触的 ...