pring-cloud 服务发现与消费(以ribbon为例)

ribbon是spring-cloud中作为服务消费者的一种角色,客户端可以通过它来对服务提供者的服务进行消费,

比如本例中是服务提供者注册到注册中心,服务提供者提供了一个服务接口,返回一个hello字符串,我们通过ribbon将这个接口调用,再不暴露真实服务提供者的地址的同时,获取服务提供者的服务

前提:

按照之前几个教程,搭建出注册中心、服务提供者。这里可以使用分片的注册中心,也可以不使用,这里暂时定为使用之前搭好的分片注册中心,服务提供者仅提供一个即可。

准备工作:

1、启动注册中心

按照之前的教程,分别使用peer1和peer2进行启动两个分片的注册中心,如果是单节点直接启动项目即可

启动后可以查看下localhost:1111或localhost:1112,如图

2、启动服务提供者

这里为了查看负载均衡的情况,所以需要启动两个服务提供者

按照之前教程中,分别开启两个terminal进行指定端口启动(两个相同的服务也可以在各自的配置文件中配置不同的端口),两个终端指令分别如下:

  1. cd target
  2. java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8080

复制代码

  1. cd target
  2. java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8081

复制代码

启动结果:

至此,准备工作已经完成

正文:

1、ribbon服务搭建

新建一个maven项目,不使用模板即可。项目名为robbin-customer,导入依赖参考如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.hellxz</groupId>
  7. <artifactId>ribbon-customer</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.5.9.RELEASE</version>
  13. <relativePath/>
  14. </parent>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-eureka</artifactId>
  23. <version>RELEASE</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-ribbon</artifactId>
  28. <version>RELEASE</version>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>RELEASE</version>
  37. <scope>import</scope>
  38. <type>pom</type>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. </project>

复制代码

新建springboot启动类,将RestTemplate交给Spring容器进行管理

  1. package com.cnblogs.hellxz;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.client.RestTemplate;
  8. /**
  9. * @Author : Hellxz
  10. * @Description: 消费应用
  11. * @Date : 2018/4/16 15:45
  12. */
  13. @EnableDiscoveryClient
  14. @SpringBootApplication
  15. public class CustomerApplication {
  16. [url=home.php?mod=space&uid=4377]@Bean[/url] //将此Bean交给spring容器
  17. @LoadBalanced //通过此注解开启负载均衡
  18. RestTemplate restTemplate(){
  19. return new RestTemplate();
  20. }
  21. public static void main(String[] args) {
  22. SpringApplication.run(CustomerApplication.class, args);
  23. }
  24. }

复制代码

在src/resources目录下创建一个application.yml进行配置注册信息相关,本文使用了分片的注册中心,单节点请配置一个defaltZone即可

  1. server:
  2. port: 9000 #为ribbon-customer指定服务端口
  3. spring:
  4. application:
  5. name: ribbon-customer #指定应用名
  6. #指定eureka注册中心地址
  7. eureka:
  8. client:
  9. serviceUrl:
  10. defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/

复制代码

在启动类同级目录创建CustomerController,注入RestTemplate进行调用服务接口

  1. package com.cnblogs.hellxz;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.client.RestTemplate;
  7. /**
  8. * @Author : Hellxz
  9. * @Description: 消费者应用
  10. * @Date : 2018/4/16 15:54
  11. */
  12. @RestController
  13. public class CustomerController {
  14. @Autowired
  15. //注入restTemplate
  16. private RestTemplate restTemplate;
  17. @RequestMapping(value="/ribbon-customer", method=RequestMethod.GET)
  18. public String helloCustomer(){
  19. //这里注释掉,因为之前想当然使用了直链访问服务提供者的接口,这样是不会返回结果的,而且会报错
  20. //return restTemplate.getForEntity("http://localhost:8080/hello",String.class).getBody();
  21. //使用restTemplate调用微服务接口
  22. return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
  23. }
  24. }

复制代码

注意:上述代码第24行给出了一个错误的演示,如果出现访问ribbon接口报错,出现白色报错页面,请检查这里

http://www.ljhseo.com/
http://www.xyrjkf.net/
http://www.xyrjkf.cn/
http://www.xyrjkf.com.cn/
http://www.zjdygsi.cn/
http://www.zjdaiyun.cn/
http://www.jsdygsi.cn/
http://www.xyrjkf.top/
http://www.xyrjkf.com/
http://www.daiyunzj.cn/
http://ljhseo.com/
http://xyrjkf.net/
http://xyrjkf.cn/
http://xyrjkf.com.cn/
http://zjdygsi.cn/
http://zjdaiyun.cn/
http://jsdygsi.cn/
http://xyrjkf.top/
http://xyrjkf.com/
http://daiyunzj.cn/

至此ribbon消费者应用搭建完成,启动测试

测试:

访问http://localhost:1111/ 我们发现这个ribbon消费者应用已经注册到注册中心中了

访问http://localhost:9000/ribbon-customer

还记得服务提供者项目中的如图方法中如果有访问则会打印信息,因为启动了两个服务提供者,这里可以测试ribbon的负载均衡

查看服务提供者输出

而第二个终端没有,依旧显示到Resolving eureka endpoints via configuration这行

刷新页面查看终端,由于ribbon的默认负载均衡的实现是轮询的,所以可能出现多次访问同一服务,多刷新几次页面,一定会在另一个Termical中显示的!

结语:

Ribbon作为服务消费者,可以在用户获取到服务提供者提供的服务的同时,不向用户暴露接口地址。可以看到,这里调用服务接口的时候使用的是服务提供者的服务名代替主机名,这在服务治理框架中,这种特性很重要。

由于本人之前是先学的jhipster生成器,所以,提前预报一下,之后还会有一种名为Feign的技术可以替代Ribbon,本系列博客均为学习笔记,实际操作中,可能会存在一个应用既是服务提供者又是服务消费者的情况。

原文地址:https://www.cnblogs.com/ljhseocom/p/8999514.html

时间: 2024-08-30 14:26:27

pring-cloud 服务发现与消费(以ribbon为例)的相关文章

Spring Cloud 服务发现和消费

服务的发现和消费 有了服务中心和服务提供者,下面我们来实现一个服务的消费者: 服务消费者主要完成两个任务--服务的发现和服务的消费,服务发现的任务是由Eureka客户端完成,而服务消费的任务是由Ribbon完成. Ribbon是一个基于HTTP和TCP客户端的负载均衡器.它可以在通过客户端中配置ribbonServerList服务端列表去轮询访问以达到负载均衡目的. 当Ribbon和Eureka同时使用时,Ribbon的服务实例清单RibbonServerList会被DiscoveryEnabl

SpringCloud 教程 之二 服务发现(Eureka Discovery Client + Ribbon)

1.启动服务注册中心 可参考 SpringCloud 教程 之一 服务注册中心及服务注册(Eureka Server + Eureka Client) 第1节 创建[服务注册中心]工程,即 Eureka Server.创建并运行工程. 2.创建服务提供者工程 2.1.新建Spring Boot 工程 springcloud-eureka-provider 2.2.工程pom.xml文件添加如下依赖: <dependency> <groupId>org.springframework

spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient

使用服务发现的时候提到了两种注解,一种为@EnableDiscoveryClient,一种为@EnableEurekaClient,用法上基本一致,今天就来讲下两者,下文是从stackoverflow上面找到的对这两者的解释: There are multiple implementations of "Discovery Service" (eureka, consul, zookeeper). @EnableDiscoveryClient lives in spring-cloud

(4).NET CORE微服务 Micro-Service ---- Consul服务发现和消费

上一章说了  Consul服务注册  现在我要连接上Consul里面的服务 请求它们的API接口 应该怎么做呢? 1.找Consul要一台你需要的服务器 1.1 获取Consul下的所有注册的服务 using (var consulClient = new ConsulClient(c => c.Address = new Uri("http://127.0.0.1:8500"))) { var services = consulClient.Agent.Services().R

Spring Cloud构建微服务架构—服务消费(Ribbon)

Spring Cloud RibbonSpring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具.它是一个基于HTTP和TCP的客户端负载均衡器.它可以通过在客户端中配置ribbonServerList来设置服务端列表去轮询访问以达到均衡负载的作用. 当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务实例列表.同时它也会用N

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

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 eureka-server eureka-client 1 Ribbon工程搭建 1.1 创建spring boot工程:eureka-ribbon 1.2 pom.xml所需要依赖的jar包 <dependency> <groupId>org.springframework.clo

Spring Cloud官方文档中文版-服务发现:Eureka客户端

官方文档地址为:http://cloud.spring.io/spring-cloud-static/Brixton.SR7/#_spring_cloud_netflix 文中例子我做了一些测试在:http://git.oschina.net/dreamingodd/spring-cloud-preparation Spring Cloud Netflix This project provides Netflix OSS integrations for Spring Boot apps th

SpringCloud服务发现注册Eureka +Ribbon + Feign

在本期将学习以下知识点: 什么是服务注册和发现? 基于Eureka的注册服务器 服务生产者 结合Ribbon服务消费者 结合Feign的服务生产者和消费者 什么是服务注册和发现 假设有2个微服务A和B分别在端点http:// localhost:8181 /和http:// localhost:8282 /上运行,如果想要在A服务中调用B服务,那么我们需要在A服务中键入B服务的url,这个url是负载均衡器分配给我们的,包括负载平衡后的IP地址,那么很显然,B服务与这个URL硬编码耦合在一起了,

Spring Cloud官方文档中文版-服务发现:Eureka服务端

官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR3/#spring-cloud-eureka-server 文中例子我做了一些测试在:http://git.oschina.net/dreamingodd/spring-cloud-preparation Service Discovery: Eureka Server 服务发现:Eureka服务端 How to Include Eureka Server 如何创建Eurek