前言:

必需学会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. 【转】Uint8Array 转为 string,解决中文乱码

    来源: <http://stackoverflow.com/questions/8936984/uint8array-to-string-in-javascript/22373197> / ...

  2. TDD: 解除依赖

    1  A类依赖B 类,可以把B类提取成IB接口,解除AB 之间的依赖关系. 通过创建实现了IB接口的BStub 装代码,可以模拟B类进行测试. 这是针对接口编程的典型.适合构造代价大,变化多的情况.应 ...

  3. Java 加密Excel文件(打开时需输入密码)

    收集:author: lifq package com.XXX.XXX.utils; import java.io.IOException; import com.jxcell.CellExcepti ...

  4. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f4-hystrix/ 本文出自方志朋的博客 在微服务架构中, ...

  5. ndk-build 学习笔记

    # 必须以local_path 开头# 定位源文件LOCAL_PATH := $(call my-dir) #引入clear-vars.mk文件,清除除local_path以外的其他local_< ...

  6. 'version' contains an expression but should be a constant. @ line 13, column 11问题的解决

    <modelVersion>4.0.0</modelVersion> <groupId>cy.nad.cyg</groupId> <artifac ...

  7. iOS之查看代码运行的时间

    有时候我们想要准确的知道某段代码.某个循环执行的时间,然后分析效率等问题,这个时候就需要执行时间是多少.正好看到网上已经有人做了这个工作,我就直接摘下来了.正好也用了宏的方式计算时间,我们只要在需要计 ...

  8. 你不知道的javaScript笔记(3)

    对象 对象可以通过两种形式定义: 声明形式和构造形式 声明形式语法: var myObj = {key:value} 构造形式语法: var myObj = new Object(); myObj.k ...

  9. 如何用hexo搭建个人博客. 亲测有效

    搭建博客: 安装node.js和git 以管理员身份进入cmd. 输入:  npm install -g cnpm --registry=https://registry.npm.taobao.org ...

  10. 洛谷P2439 [SDOI2005]阶梯教室设备利用(带权区间覆盖)

    题目背景 我们现有许多演讲要在阶梯教室中举行.每一个演讲都可以用唯一的起始和终止时间来确定,如果两个演讲时间有部分或全部重复,那么它们是无法同时在阶级教室中举行的.现在我们想要尽最大可能的利用这个教室 ...