SpringCould之服务消费者(Ribbon)

本文介绍如何在SpringCloud中使用Ribbon实现服务消费者的负载均衡。通过创建一个消费者工程项目,配置RestTemplate Bean并启用负载均衡功能,实现对多个服务实例的轮询访问。

SpringCloud 提供的服务消费者有两种(此处使用Ribbon实现)

  • Ribbon +RestTemplate
  • fegin




    和SpringCloud其他服务一样 
    创建一个消费者工程项目(在父工程spring-cloud下创建一个 spring-cloud-ribbon 工程项目)

    pom.xml
    <?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>
    
        <parent>
            <groupId>com.funtl</groupId>
            <artifactId>hello-spring-cloud-dependencies</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
        </parent>
    
        <artifactId>hello-spring-cloud-web-admin-ribbon</artifactId>
        <packaging>jar</packaging>
    
        <name>hello-spring-cloud-web-admin-ribbon</name>
        <url>http://www.funtl.com</url>
        <inceptionYear>2018-Now</inceptionYear>
    
        <dependencies>
            <!-- Spring Boot Begin -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- Spring Boot End -->
    
            <!-- Spring Cloud Begin -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
            <!-- Spring Cloud End -->
    
            <!-- 解决 thymeleaf 模板引擎一定要执行严格的 html5 格式校验问题 -->
            <dependency>
                <groupId>net.sourceforge.nekohtml</groupId>
                <artifactId>nekohtml</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.einblatt.hello.spring.cloud.web.admin.ribbon.WebAdminRibbonApplication</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    通过 @EnbleDisconverClient 注解注册到服务中心
    package com.funtl.hello.spring.cloud.web.admin.ribbon;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class WebAdminRibbonApplication {
        public static void main(String[] args) {
            SpringApplication.run(WebAdminRibbonApplication.class, args);
        }
    }

    配置程序端口号等
    application.yml
    spring:
      application:
        name: hello-spring-cloud-web-admin-ribbon
      thymeleaf:
        cache: false
        mode: LEGACYHTML5
        encoding: UTF-8
        servlet:
          content-type: text/html
    
    server:
      port: 8764
    
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/   #Eureka服务注册中心地址
     配置RestTemplata 的Bean使用 并且通过@LoadBalanced 注解开启负载均衡功能
     
    package com.funtl.hello.spring.cloud.web.admin.ribbon.config;
    
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    @Configuration
    public class RestTemplateConfiguration {
    
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }

     创建测试用的Service

    在这里我们直接用的程序名替代了具体的 URL 地址,在 Ribbon 中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的 URL 替换掉服务名
    package com.funtl.hello.spring.cloud.web.admin.ribbon.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    @Service
    public class AdminService {
    
        @Autowired
        private RestTemplate restTemplate;
    
        public String sayHi(String message) {
            return restTemplate.getForObject("http://HELLO-SPRING-CLOUD-SERVICE-ADMIN/hi?message=" + message, String.class);
        }
    }
    创建测试用的Controller
    package com.funtl.hello.spring.cloud.web.admin.ribbon.controller;
    
    import com.funtl.hello.spring.cloud.web.admin.ribbon.service.AdminService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class AdminController {
    
        @Autowired
        private AdminService adminService;
    
        @RequestMapping(value = "hi", method = RequestMethod.GET)
        public String sayHi(@RequestParam String message) {
            return adminService.sayHi(message);
        }
    }
     将Eureka服务端开启 

     访问 http://localhost:8764/hi?message=HelloRibbon

     浏览器交替显示(实现负载均衡)
    Hi,your message is :"HelloRibbon" i am from port:8762
    Hi,your message is :"HelloRibbon" i am from port:8763
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值