欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring could 之服务消费者(rest+ribbon)

程序员文章站 2022-06-23 22:22:43
...

ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。

ribbon 已经默认实现了这些配置bean:

IClientConfig ribbonClientConfig: DefaultClientConfigImpl

IRule ribbonRule: ZoneAvoidanceRule

IPing ribbonPing: NoOpPing

ServerList ribbonServerList: ConfigurationBasedServerList

ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

二、准备工作

这一篇文章基于上一篇文章《spring could 之服务的注册与发现(Eureka) 》的工程,启动eureka-server 工程;启动eurekaclient工程,它的端口为8762;将eurekaclient的配置文件的端口改为8763,并启动,这时你会发现:eurekaclient在eureka-server注册了2个实例,这就相当于一个小的集群。访问localhost:8761如图所示:
spring could 之服务消费者(rest+ribbon)
新建模块could_ribbon
工程目录结构:
spring could 之服务消费者(rest+ribbon)
1、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>

    <groupId>com.xuan</groupId>
    <artifactId>could_ribbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>could_ribbon</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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>Dalston.SR4</spring-cloud.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>${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>

2、应用配置文件application.yml:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: localhost
    statusPageUrl: http://${eureka.instance.hostname}:${server.port}/
    healthCheckUrl: http://${eureka.instance.hostname}:${server.port}/health
    homePageUrl: http://${eureka.instance.hostname}:${server.port}/
    prefer-ip-address: true
server:
  port: 8764
spring:
  application:
    name: cooldribbon

3、应用主入口程序:

package com.xuan;

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 CouldRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(CouldRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced // 负载均衡 @LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

4、ribbon负载均衡功能使用测试
使用restTemplate.getForObject()调用eurekaclient提供的服务(跑两个eurekaclient工程实例(端口分别在6782,6783)),XUANCLIENT是eurekaclient工程配置文件里的应用名称(尽量简单,不使用特殊字符)

package com.xuan.service.impl;

import com.xuan.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.client.RestTemplate;

/**
 * Created by chenqixuan on 17/10/27.
 */
@Service
public class UserServiceImpl implements com.xuan.service.UserService {

    @Autowired
    RestTemplate restTemplate;

    @Override
    @Validated
    public String hiService(String name) {
        return restTemplate.getForObject("http://XUANCLIENT/?name="+name,String.class);
    }
}

controller层调用:

package com.xuan.controller;

import com.xuan.service.UserService;
import com.xuan.service.impl.UserServiceImpl;
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;

/**
 * Created by chenqixuan on 17/10/27.
 */
@RestController
public class HomeController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/")
    public String home(){

        return userService.hiService("ooooo");
    }

    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name){
        return userService.hiService(name);
    }



}

一个服务注册中心,eureka server,端口为8761
eurekaclient工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册
cooldribbon端口为8764,向服务注册中心注册
当cooldribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;
spring could 之服务消费者(rest+ribbon)
效果图:
spring could 之服务消费者(rest+ribbon)
spring could 之服务消费者(rest+ribbon)