zoukankan      html  css  js  c++  java
  • Ribbon服务消费者

    springcloud使用到两种消费工具,ribbon和feign

      ribbon实现了服务的负载均衡

      feign默认集成了ribbon,一般情况下使用feign作为消费端

    搭建消费者项目(RibbonDemo)

    1、创建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.cppdy</groupId>
        <artifactId>RibbonDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath />
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <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>
        </dependencies>
    
        <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>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
    </project>

    2、创建application.yml配置文件

    eureka:
      client:
        service-url:
          defaultZone: http://127.0.0.1:9000/eureka/
    server:
      port: 9003
    spring:
      application:
        name: cppdy-ribbon

    3、创建HelloService类

    package com.cppdy.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    @Service
    public class HelloService {
    
        @Autowired
        private RestTemplate template;
    
        public String hellService(String name) {
    
            return template.getForObject("http://CPPDY-HELLO/hello?name" + name, String.class);
        }
    
    }

    4、创建HelloController类

    package com.cppdy.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.cppdy.service.HelloService;
    
    @RestController
    public class HelloController {
    
        @Autowired
        private HelloService helloService;
    
        @RequestMapping("hello")
        public String hello(String name) {
            return helloService.hellService(name);
        }
    }

    5、创建启动类(RibbonApp)

    package com.cppdy;
    
    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 RibbonApp {
    
        public static void main(String[] args) {
            SpringApplication.run(RibbonApp.class, args);
        }
    
        @Bean
        @LoadBalanced
        RestTemplate template() {
    
            return new RestTemplate();
        }
    
    }

    6、先启动EurekaDemo(注册中心项目),再启动2个ProduceDemo(服务提供者项目)端口号分别设置为9001、9002,再启动RibbonDemo(消费者项目)端口号设置为9003,一直访问http://localhost:9003/hello,会自动轮流调用9001和9002的hello方法(负载均衡)

  • 相关阅读:
    占位
    提高班整风带给我的思考
    Servlet笔记
    CommandArgument传多个值
    asp.net中怎么判断request的一个值是否为空
    asp.net中cookie中文乱码的解决
    datatable的手工构造过程
    .net c#日期时间函数大全
    【转载】[.net程序员面试题]
    javascript自动生成表格行
  • 原文地址:https://www.cnblogs.com/jiefu/p/10055669.html
Copyright © 2011-2022 走看看