zoukankan      html  css  js  c++  java
  • 第二篇:服务消费者(RestTemplate+ribbon)

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

    Talk is cheap.Show me your code. 

    上一篇,建立了一个eureka server, 一个eureka client,client的注册名是service-hi,用来提供服务。下面来看看如何消费服务:

    一、启动服务。

      1、启动eureka server工程。

      2、启动eureka client工程,端口号8762.

      3、修改eureka client工程的application.yml,将端口号改为8763.启动。

      这样就相当于启动了一个小的服务集群,service-hi集群。当访问http://localhost:8761时会看到

    注册了2个服务,一个端口号8762,一个8763.下面构建ribbon工程消费服务。

    二、建一个服务消费者

      ctrl+n创建一个maven工程,取名叫eureka-service-ribbon,打jar包。

      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.sun</groupId>
      <artifactId>eureka-service-ribbon</artifactId>
      <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        
        
        <name>eureka-service-ribbon</name>
        <description>Demo project for Spring Boot</description>
    
    
        <parent>
            <groupId>com.sun</groupId>
            <artifactId>springcloud-parent</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
        </dependencies>
    </project>
    View Code

      将eureka-service-ribbon注册到服务中心(eureka-server)。application.yml如下:

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

    在eureka-service-ribbon启动类中,通过@EnableEurekaClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

    启动类代码如下:

    package com.sun;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class ServiceRibbonApplication {
    
        public static void main(String[] args) {
            SpringApplication.run( ServiceRibbonApplication.class, args );
        }
    
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
    }
    View Code

    这里再提醒一下,启动类需要放到包的根下才会自动扫描。

    写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的服务名替代了具体的url地址,

    在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

    package com.sun;
    
    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://SERVICE-HI/hi?name="+name,String.class);
        }
    
    
    }
    View Code

    写一个controller,在controller中用调用HelloService 的方法,代码如下:

    package com.sun;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloControler {
    
        @Autowired
        HelloService helloService;
    
        @GetMapping(value = "/hi")
        public String hi(@RequestParam String name) {
            return helloService.hiService( name );
        }
    }
    View Code

    在浏览器上多次访问http://localhost:8764/hi?name=sun,浏览器交替显示:

    hi sun ,i am from port:8762

    hi sun ,i am from port:8763

    说明访问controller时,RestTemplate调用了服务,做了负载均衡,访问了不同的端口的服务实例。

    • 一个服务注册中心,eureka server,端口为8761
    • service-hi工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册
    • sercvice-ribbon端口为8764,向服务注册中心注册
    • 当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;

    参考博客:https://blog.csdn.net/forezp/article/details/81040946

  • 相关阅读:
    SettingsProvider 它SettingsCache
    14、Cocos2dx 3.0三,找一个小游戏开发Scene and Layer:游戏梦想
    open-flash-chart2各种效果
    Android使用surface直接显示yuv数据(三)
    LeetCode:Populating Next Right Pointers in Each Node
    zoj 3203 Light Bulb,三分之二的基本问题
    POJ 2485 Highways
    Leetcode
    android音乐柱状频谱实现
    Android自定义控件实战——水流波动效果的实现WaveView
  • 原文地址:https://www.cnblogs.com/PPBoy/p/9371872.html
Copyright © 2011-2022 走看看