SpringCloud之Ribbon

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

壹、Ribbon簡介

  • ribbon是一個負載均衡客戶端,能夠很好的控制htt和tcp的一些行爲。Feign默認集成了ribbon。
  • ribbon 已經默認實現了這些配置bean:
    ~~~

IClientConfig ribbonClientConfig: DefaultClientConfigImplgit

IRule ribbonRule: ZoneAvoidanceRulegithub

IPing ribbonPing: NoOpPingweb

ServerList ribbonServerList: ConfigurationBasedServerListspring

ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter安全

ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer~~~restful

貳、準備工做

  • 新建一個ribbon子工程lovin-ribbon-client,用於後面的操做。下面是主要的pom依賴

~~~pom lovincloud com.eelve.lovincloud 1.0-SNAPSHOT 4.0.0 網絡

<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**
~~~yaml
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:8881/eureka/   # 註冊到的eureka服務地址
~~~
- 配置**spring-boot-starter-security**,這裏爲了方便我這裏放開全部請求
~~~java
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;架構

/**app

  • @ClassName SecurityConfig
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 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開啓負載均衡的功能。
~~~java
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**
~~~java
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**
~~~java
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
![咱們能夠看到服務已經所有啓動成功](https://user-gold-cdn.xitu.io/2019/8/29/16cdb47b09a516e5?w=1886&h=935&f=png&s=128533)
咱們能夠看到服務已經所有啓動成功
- 而後訪問http://localhost:8805/hello
![咱們能夠看到已經能夠經過ribbon調到咱們創建的eureka客戶端了](https://user-gold-cdn.xitu.io/2019/8/29/16cdb47b34fcd087?w=1335&h=586&f=png&s=23468)
咱們能夠看到已經能夠經過ribbon調到咱們創建的eureka客戶端了
- 再次請求接口觀察返回
![咱們能夠看到咱們調到了經過ribbon負載的另一個接口](https://user-gold-cdn.xitu.io/2019/8/29/16cdb47b81d4250b?w=1332&h=552&f=png&s=23047)
咱們能夠看到咱們調到了經過ribbon負載的另一個接口了,到這裏咱們就已經弄好了一個簡單的ribbon負載。複製代碼

肆、網絡架構

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

---

相關文章
相關標籤/搜索