SpringCloud之Ribbon

前面的話】書接上文,本文的某些知識依賴個人上一篇文章:SpringCloud之Eureka,若是沒有看過能夠先移步去看一下。另外在微服務架構中,業務都會被拆分紅一個個獨立的服務,服務與服務的通信是基於http restful的。Spring cloud有兩種服務調用方式,一種是ribbon+restTemplate,另外一種是feign。在這一篇文章首先講解下基於ribbon+rest。java


壹、Ribbon簡介

  • ribbon是一個負載均衡客戶端,能夠很好的控制htt和tcp的一些行爲。Feign默認集成了ribbon。git

  • ribbon 已經默認實現了這些配置bean:github

IClientConfig ribbonClientConfig: DefaultClientConfigImpl

IRule ribbonRule: ZoneAvoidanceRule

IPing ribbonPing: NoOpPing

ServerList ribbonServerList: ConfigurationBasedServerList

ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

貳、準備工做

  • 新建一個ribbon子工程lovin-ribbon-client,用於後面的操做。下面是主要的pom依賴
<parent>
        <artifactId>lovincloud</artifactId>
        <groupId>com.eelve.lovincloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lovin-ribbon-client</artifactId>
    <packaging>jar</packaging>
    <name>ribbonclient</name>
    <version>0.0.1</version>
    <description>ribbon的client</description>

    <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-security</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • 這裏爲了安全,我這裏仍是添加spring-boot-starter-security
server:
  port: 8805   # 服務端口號
spring:
  application:
    name: lovinribbonclient     # 服務名稱
  security:
    basic:
      enabled: true
    user:
      name: lovin
      password: ${REGISTRY_SERVER_PASSWORD:lovin}
eureka:
  client:
    serviceUrl:
      defaultZone: http://lovin:lovin[@localhost](https://my.oschina.net/u/570656):8881/eureka/   # 註冊到的eureka服務地址
  • 配置spring-boot-starter-security,這裏爲了方便我這裏放開全部請求
package com.eelve.lovin.cofig;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * [@ClassName](https://my.oschina.net/u/3112573) SecurityConfig
 * @Description TDO
 * [@Author](https://my.oschina.net/arthor) zhao.zhilue
 * [@Date](https://my.oschina.net/u/2504391) 2019/8/16 14:12
 * @Version 1.0
 **/
@Configuration
@Order(0)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()
                .and().csrf().disable();
    }
}
  • 而後向程序的ioc容器中注入一個bean: restTemplate;並經過@LoadBalanced註解代表這個restRemplate開啓負載均衡的功能。
package com.eelve.lovin;

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;

/**
 * @ClassName LovinRibbonClientApplication
 * @Description TDO
 * @Author zhao.zhilue
 * @Date 2019/8/15 16:59
 * @Version 1.0
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class LovinRibbonClientApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
  • 而後編寫一個HelloService
package com.eelve.lovin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName HelloService
 * @Description TDO
 * @Author zhao.zhilue
 * @Date 2019/8/15 17:02
 * @Version 1.0
 **/
@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    public String getHello() {
        //這裏的**lovineurkaclient**是我上一篇文章新建的eureka客戶端的名稱
        return restTemplate.getForObject("http://lovineurkaclient/hello",String.class);
    }
}
  • 再編寫一個HelloController
package com.eelve.lovin.controller;

import com.eelve.lovin.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName HelloController
 * @Description TDO
 * @Author zhao.zhilue
 * @Date 2019/8/15 17:05
 * @Version 1.0
 **/
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("hello")
    public String hello(){
        return helloService.getHello();
    }
}

叄、啓動測試

  • 依次啓動eureka的服務端和兩個客戶端,以及新建的lovin-ribbon-client 咱們能夠看到服務已經所有啓動成功 咱們能夠看到服務已經所有啓動成功
  • 而後訪問http://localhost:8805/hello 咱們能夠看到已經能夠經過ribbon調到咱們創建的eureka客戶端了 咱們能夠看到已經能夠經過ribbon調到咱們創建的eureka客戶端了
  • 再次請求接口觀察返回 咱們能夠看到咱們調到了經過ribbon負載的另一個接口 咱們能夠看到咱們調到了經過ribbon負載的另一個接口了,到這裏咱們就已經弄好了一個簡單的ribbon負載。

肆、網絡架構

  • 咱們能夠看到咱們調用的服務再也不是像再上一篇文章中的直接訪問對應的服務,而是經過Ribbon的負載均衡的去調用的,並且這裏說明一點,Ribbon的默認機制是輪詢。 目前的網絡架構

相關文章
相關標籤/搜索