二、Ribbon

一、Ribbon簡介

Spring Cloud Ribbon是一個基於HTTP和TCP的客戶端負載均衡工具,它基於Netflix Ribbon實現。

在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務調用方式,一種是ribbon+restTemplate,另一種是feign。

 

二、搭建服務提供者service-hello

<?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">
    <parent>
        <artifactId>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-hello</artifactId>
    <description>服務提供者</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- springboot 安全監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- eureka服務提供者的依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

</project>
server:
  # springboot設置隨機端口號
  port: 0
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    # 重寫服務的唯一Id,防止多實例對被註冊成一個服務
    instance-id: ${spring.application.name}:${random.int}
    # 設置優先使用ip進行註冊
    prefer-ip-address: true
spring:
  application:
    name: service-hello

 

package com.mo.config;

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * spring監聽器,監聽容器中發佈的事件
 *
 * @author x.pan
 * @email [email protected]
 * @date 2020/4/6 19:31
 */
@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {

    private int port;

    @Override
    public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
        /*
         * 獲取tomcat啓動的真實端口號
         */
        port = webServerInitializedEvent.getWebServer().getPort();
    }

    public int getPort() {
        return this.port;
    }
}
package com.mo;

import com.mo.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author x.pan
 * @email [email protected]
 * @date 2020/4/6 19:02
 */
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceHelloApplication {

    @Autowired
    private ServerConfig serverConfig;

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

    /**
     * 對外提供的一個服務,
     * 可以使用idea開啓多實例
     *
     * @return
     */
    @GetMapping("/hello")
    public String hello() {
        return "hello, " + serverConfig.getPort();
    }
}

 

三、搭建服務消費者ribbon 

<?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">
    <parent>
        <artifactId>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ribbon</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- ribbon負載均衡的依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
    </dependencies>

</project>
server:
  port: 0
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${random.int}
package com.mo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author x.pan
 * @email [email protected]
 * @date 2020/4/6 19:54
 */
@RestController
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 調用SERVICE-HELLO提供的服務
     * RestTemplate通過負載均衡會自動將服務名,解析爲ip:port
     *
     * @return
     */
    @GetMapping("/hello")
    public String hello() {
        return restTemplate.getForObject("http://SERVICE-HELLO/hello", String.class);
    }
}
package com.mo;

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;

/**
 * Ribbon服務啓動器
 *
 * @author x.pan
 * @email [email protected]
 * @date 2020/4/6 19:51
 */
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class, args);
    }


    /**
     * 注入一個開啓了負載均衡的bean:restTemplate
     *
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

效果:請求ribbon服務的hello接口,會交替顯示service-hello兩個服務執行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章