美文网首页
springcloud-服务调用篇(ribbon)

springcloud-服务调用篇(ribbon)

作者: 翟文龙 | 来源:发表于2019-03-20 10:25 被阅读0次

    一、场景描述

    我们的微服务已经注册到了注册中心,不同的服务之间如何进行调用?这里我们采用feign或者ribbon都可以实现

    二、ribbon实现

    1. Idea-->file-->new-->project-->spring initializr-->next-->修改必要信息-->next
    2. 分别选择Cloud Routing-->Ribbon
    3. 再选择 Cloud Discovery-->Eureka Discovery
    4. 使用ribbon我们需要使用restTemplete,需要web支持,选择Web-->web
    5. 点击next-->finish
    6. 查看生成的pom文件如下
    <?xml version="1.0" encoding="UTF-8"?>
    <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.example</groupId>
        <artifactId>eurekaribbon</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>eurekaribbon</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-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>${spring-cloud.version}</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>
    </project>
    

    5.我们摘出我们需要的依赖,一个是启用eureka,一个是引入ribbon,一个是引入Web,

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    1. application.properties文件中添加eureka配置
    spring.application.name=eurekaribbon
    server.port=8085
    eureka.client.serviceUrl.defaultZone=http://eureka1:8001/eureka/ ,http://eureka1:8002/eureka/,http://eureka1:8003/eureka/
    
    1. 启动类添加 @EnableDiscoveryClient
    2. 启动类中添加restTemplete Bean,完整的启动类如下
    package com.example.eurekaribbon;
    
    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 EurekaribbonApplication {
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
        public static void main(String[] args) {
            SpringApplication.run(EurekaribbonApplication.class, args);
        }
    }
    
    1. 这样我们就可以基于ribbon来调用其他服务了,重点是restTemplete上面的@LoadBalanced注解
    2. 我们可以简单写一个调用DEMO的hello服务的实现方式如下:
    package com.example.eurekaribbon;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import javax.annotation.Resource;
    @RestController
    public class Hello {
        @Resource
        RestTemplate restTemplate;
        @RequestMapping(value = "/hello")
        public  String hello(){
                return restTemplate.getForObject("http://DEMO/hello",String.class);
        }
    }
    

    ==这里我们引入的RestTemplete是我们在启动类中定义的以@LoadBalanced注解的bean。默认使用的就是ribbon,已经支持负载均衡==

    1. 我们可以把注册中心,demo服务启动,再启动现有的ribbon服务。调用localhost:8085/hello。得到返回值helloWord

    相关文章

      网友评论

          本文标题:springcloud-服务调用篇(ribbon)

          本文链接:https://www.haomeiwen.com/subject/nzoamqtx.html