zoukankan      html  css  js  c++  java
  • (1-2)SpringCloud:服务的消费者rest+ribbon

      服务发现的任务是由Eureka客户端完成,而服务的消费任务由Ribbon完成。Ribbon是一个基于HTTP和TCP的客户端负载据衡器,它可以通过客户端中配置ribbonServerList服务端列表去轮询访问以达到负载均衡的作用。当Ribbon与Eureka联合使用时,Ribbon的服务实例清单RibbonServerList会被DiscoveryEnableNIWSServerList重写,扩展成从Eureka注册中心获取服务端列表。这里不再对Ribbon做过多的介绍。后面会详细的介绍和分析

      下面会通过一个示例来看看Eurka的服务治理体系下如何实现服务的发现与消费。

      一、创建一个独立的maven项目

      二、添加相关的maven依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <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>
    <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>

       三、application.yml配置文件

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

      四、写启动类。

    package org.hope;
    
    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 ServiceRibbonApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ServiceRibbonApplication.class, args);
        }
    
        @Bean
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
    }

       五、写service来调用服务提供者

    package org.hope.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    @Service
    public class HelloService {
        @Autowired
        RestTemplate restTemplate;
    
        public String hiService(String name) {
            return restTemplate.getForObject("http://localhost:8762/hello?name=" + name, String.class);
         //SERVICE-HI是服务提供者的实例名称,这里用这种形式调用服务也是可以的。
        
    return restTemplate.getForObject("http://SERVICE-HI/hello?name=" + name, String.class);
    } }

       六、写一个controller来调用service

    package org.hope.web;
    
    import org.hope.service.HelloService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloControler {
    
        @Autowired
        HelloService helloService;
    
        @RequestMapping(value = "/hello")
        public String hi(@RequestParam String name){
            return helloService.hiService(name);
        }
    
    }

      七、测试一下看是否成功。

      1.先启动Eureka注册中心

      2.再运行ServiceApplication.java

      3.最后运行ribbon,在浏览器输入http://localhost:8764/hello?name=wali

    证明调用成功!!!

     https://gitee.com/huayicompany/springcloud-learn/tree/master/lesson1

    参考:

    [1] 博客,http://blog.csdn.net/forezp/article/details/70148833

    [2] 博客,http://www.cnblogs.com/skyblog/p/5133752.html

    [3] 《SpringCloud微服务实战》,电子工业出版社,翟永超

  • 相关阅读:
    开发移动端项目如何利用Chrome浏览器连接真机测试
    有关浏览器开发者工具使用的技巧
    有关前端实时可视化工具的使用 ==实现边改代码边看效果
    vue的生命周期介绍beforeCreate(创建前)、created(创建后)、beforeMount(载入前)、mounted(载入后)、beforeUpdate(更新前)、updated(更新后)、beforeDestroy(销毁前)、destroyed(销毁后)
    echart中重新定义引导线的文字换行<br>不起作用
    使用mock数据填写表格同时带点击查看更多
    有关前后端分离前端如何使用mock数据
    echar图柱状图和折线图混合加双侧y轴坐
    【Oracle】DBMS_STATS.GATHER_TABLE_STATS
    【PostgreSQL-9.6.3】Red Hat 4.4.7下的安装
  • 原文地址:https://www.cnblogs.com/happyflyingpig/p/7967421.html
Copyright © 2011-2022 走看看