资讯专栏INFORMATION COLUMN

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

dreamans / 286人阅读

摘要:在服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于的。配置文件如下在工程的启动类中通过向服务中心注册并且注册了一个通过注册表明,这个是负载均衡的。

转载请标明出处:
http://blog.csdn.net/forezp/a...
本文出自方志朋的博客

在上一篇文章,讲了服务的注册和发现。在服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。

一、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,当你使用@ FeignClient,ribbon自动被应用。

ribbon 已经默认实现了这些配置bean:

IClientConfig ribbonClientConfig: DefaultClientConfigImpl

IRule ribbonRule: ZoneAvoidanceRule

IPing ribbonPing: NoOpPing

ServerList ribbonServerList: ConfigurationBasedServerList

ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

二、准备工作

基于上一节的工程,启动eureka-server 工程;启动service-hi工程,它的端口为8762;将service-hi的配置文件的端口改为8763,并启动它,这时你会发现:service-hi在eureka-server注册了2个,这就相当于一个小的集群。访问localhost:8761如图所示:

三、建一个服务消费者

重新新建一个spring-boot工程,取名为:service-ribbon;
它的pom.xml文件如下:



    4.0.0

    com.forezp
    service-ribbon
    0.0.1-SNAPSHOT
    jar

    service-ribbon
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-ribbon
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.RC1
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    




向服务注册中心注册一个新的服务,这时service-ribbon既是服务提供者,也是服务消费者。配置文件application.yml如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon

在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且注册了一个bean: restTemplate;通过@ LoadBalanced注册表明,这个restRemplate是负载均衡的。

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

这时我们需要测试下,建一个service类:

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

}

通过restTemplate.getForObject方法,service-ribbon 就可以调用service-hi的方法了。并且在调用的工程中并之需要写服务的名,而不是具体的ip.

写一个controller:

/**
 * Created by fangzhipeng on 2017/4/6.
 */
@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;
    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }


}

访问http://localhost:8764/hi?name...浏览器交替显示:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

这说明当我们通过调用restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class),获取service-hi的方法时,已经做了负载均衡,访问了不同的端口的服务。

四、此时的架构

一个服务注册中心,eureka server,端口为8761

service-hi工程跑了两个副本,端口分别为8762,8763,分别向服务注册中心注册

sercvice-ribbon端口为8764,向服务注册中心注册

当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;

源码下载:https://github.com/forezp/SpringCloudLearning/tree/master/chapter2

五、参考资料

本文参考了以下:

spring-cloud-ribbon

springcloud ribbon with eureka

服务消费者

优秀文章推荐:

史上最简单的 SpringCloud 教程 | 终章

史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)

史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69927.html

相关文章

  • 架构~微服务

    摘要:接下来继续介绍三种架构模式,分别是查询分离模式微服务模式多级缓存模式。分布式应用程序可以基于实现诸如数据发布订阅负载均衡命名服务分布式协调通知集群管理选举分布式锁和分布式队列等功能。 SpringCloud 分布式配置 SpringCloud 分布式配置 史上最简单的 SpringCloud 教程 | 第九篇: 服务链路追踪 (Spring Cloud Sleuth) 史上最简单的 S...

    xinhaip 评论0 收藏0
  • 架构~微服务 - 收藏集 - 掘金

    摘要:它就是史上最简单的教程第三篇服务消费者后端掘金上一篇文章,讲述了通过去消费服务,这篇文章主要讲述通过去消费服务。概览和架构设计掘金技术征文后端掘金是基于的一整套实现微服务的框架。 Spring Boot 配置文件 – 在坑中实践 - 后端 - 掘金作者:泥瓦匠链接:Spring Boot 配置文件 – 在坑中实践版权归作者所有,转载请注明出处本文提纲一、自动配置二、自定义属性三、ran...

    church 评论0 收藏0
  • 上最简单SpringCloud教程 | 第三篇: 服务费者(Feign)

    摘要:一简介是一个声明式的服务客户端,它使得写服务变得更简单。同时支持可插拔的编码器和解码器。对添加了支持,同时在中次用相同的。 转载请标明出处: http://blog.csdn.net/forezp/a...本文出自方志朋的博客 上一篇文章,讲述了通过restTemplate+ribbon去消费服务,这篇文章主要讲述通过feign去消费服务。 一、Feign简介 Feign是一个声明式的...

    0x584a 评论0 收藏0
  • 上最简单SpringCloud教程 | 第四篇:断路器(Hystrix)

    摘要:为了保证其高可用,单个服务又必须集群部署。为了解决这个问题,就出现断路器模型。一断路器简介摘自官网已经创建了一个名为的库来实现断路器模式。较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用达到一个阀值是秒次断路器将会被打开。 转载请标明出处: http://blog.csdn.net/forezp/a...本文出自方志朋的博客 在微服务架构中,我们将业务拆分成一个个的服务,...

    Hydrogen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<