springcloud使用到两种消费工具,ribbon和feign

  ribbon实现了服务的负载均衡

  feign默认集成了ribbon,一般情况下使用feign作为消费端

搭建消费者项目(RibbonDemo)

1、创建pom.xml

<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.cppdy</groupId>
<artifactId>RibbonDemo</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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-web</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>Dalston.RC1</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> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>

2、创建application.yml配置文件

eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9000/eureka/
server:
port: 9003
spring:
application:
name: cppdy-ribbon

3、创建HelloService类

package com.cppdy.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class HelloService { @Autowired
private RestTemplate template; public String hellService(String name) { return template.getForObject("http://CPPDY-HELLO/hello?name" + name, String.class);
} }

4、创建HelloController类

package com.cppdy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.cppdy.service.HelloService; @RestController
public class HelloController { @Autowired
private HelloService helloService; @RequestMapping("hello")
public String hello(String name) {
return helloService.hellService(name);
}
}

5、创建启动类(RibbonApp)

package com.cppdy;

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; @SpringBootApplication
@EnableDiscoveryClient
public class RibbonApp { public static void main(String[] args) {
SpringApplication.run(RibbonApp.class, args);
} @Bean
@LoadBalanced
RestTemplate template() { return new RestTemplate();
} }

6、先启动EurekaDemo(注册中心项目),再启动2个ProduceDemo(服务提供者项目)端口号分别设置为9001、9002,再启动RibbonDemo(消费者项目)端口号设置为9003,一直访问http://localhost:9003/hello,会自动轮流调用9001和9002的hello方法(负载均衡)

Ribbon服务消费者的更多相关文章

  1. spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

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

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

  3. SpringCloud的服务消费者 (二):(rest+feign/ribbon)声明式访问注册的微服务

    采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...

  4. SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解

    前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...

  5. SpringCloud(2)服务消费者(rest+ribbon)

    1.准备工作 这一篇文章基于上一篇文章的工程.启动eureka-server 工程,端口为 8761.分别以端口 8762 和 8763 启动 service-hi 工程.访问 localhost:8 ...

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

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

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

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

  8. 第二篇:服务消费者(RestTemplate+ribbon)

    第一篇讲了服务的注册,这篇来说说服务的调用,服务与服务的通讯是基于http restful,springcloud的服务调用是通过ribbon方式的,客户端的负载均衡. Talk is cheap.S ...

  9. 玩转SpringCloud(F版本) 二.服务消费者(1)ribbon+restTemplate

    上一篇博客有人问我,Springcloud系列会不会连载 ,大家可以看到我的标签分类里已经开设了SpringCloud专题,所以当然会连载啦,本人最近也是买了本书在学习SpringCloud微服务框架 ...

随机推荐

  1. Coursera, Deep Learning 4, Convolutional Neural Networks - week4,

    Face recognition One Shot Learning 只看一次图片,就能以后识别, 传统deep learning 很难做到这个. 而且如果要加一个人到数据库里面,就要重新train ...

  2. Flume思维导图

  3. python面向对象--私有和继承

    一. 私有属性和私有方法 应用场景 在实际开发中,对象的某些属性或方法可能只希望在对象的内部使用,而不希望在外部被访问到 私有属性 就是 对象 不希望公开的属性 (属性即类里面的变量) 私有方法 就是 ...

  4. Liunx/RHEL6.5 Oracle11 安装记录

    1.创建用户组 groupadd oinstall #创建用户组oinstall groupadd dba #创建用户组dba useradd -g oinstall -g dba -m oracle ...

  5. 推荐使用string

    C-string(char* const char*) basic_string<>特化版本:string charwstring wchar_tu16string char16_tu32 ...

  6. 运行APP显示两个APP图标,一个打不开,删除一个后,另一个也会消失。

    可能原因:你添加了两个intent-filter 的LAUNCHER 事件,这种情况尤其在一个项目多个module的时候容易出现 <intent-filter>               ...

  7. python(四)类变量和实例变量

    转载自[1] 实际这是个实例变量是否指向类变量的问题. python的类变量和实例变量,顾名思义,类变量是指跟类的变量,而实例变量,指跟类的具体实例相关联的变量,具体体现为self.x 等.实际要注意 ...

  8. bootstrap登录界面

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  9. 001_获取nginx证书

    一. 以下命令可以获取nginx域名的证书 openssl s_client -showcerts -connect www.jyall.com:443 < /dev/null 2>&am ...

  10. zabbix3.2监控vcenter和exsi信息

    简介 为了解 ESXI虚拟主机的运行状况,通过zabbix进行监控,图形展示ESXI虚拟主机当前的状态,避免因为esxi服务器因为资源利用率过高导致 概述 从 Zabbix 2.2.0 开始支持对 V ...