Spring Cloud 入門Eureka -Consumer服務消費(一)


這里介紹:LoadBalancerClient接口,它是一個負載均衡客戶端的抽象定義,下面我們就看看如何使用Spring Cloud提供的負載均衡器客戶端接口來實現服務的消費。

引用之前的文章中構建的eureka-server作為服務注冊中心eureka-client作為服務提供者作為基礎。

1、pom.xml  和eureka-client一樣的配置

<?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.example</groupId>
    <artifactId>eurekaConsumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.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>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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>Dalston.SR3</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, server使用了8000,client使用了8001端口,這里我們使用8002

spring.application.name=eurekaConsumer
server.port=8002
#指定eureka-servcer注冊中心的地址 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3、Spring Cloud  使用的是rest api, 這里我們初始化RestTemplate,用來真正發起REST請求

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumerApplication {
    
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

4、創建一個消費接口

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ClientConsumerController {

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/consumer")
    public String all() {
        // 發起REST請求
        return restTemplate.getForObject(getUrl("eurekaClient", "/all"), String.class);
    }

    /**
     * 獲取指定url
     * @param clientApplicationName 指定的服務提供名
     * @param interfaceName 需要消費的接口名
     * @return
     */
    private String getUrl(String clientApplicationName, String interfaceName) {
        // 使用loadBalancerClient的choose函數來負載均衡的選出一個eurekaClient的服務實例
        ServiceInstance serviceInstance = loadBalancerClient.choose(clientApplicationName);
        // 獲取之前eurekaClient /all接口地址
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + interfaceName;
        System.out.println(url);
        return url;
    }
    
}

 

最后依次啟動注冊服務server,服務提供者client ,服務消費consumer,可以看到,訪問http://localhost:8000/服務中心結果如下:

再然后訪問http://localhost:8002/consumer,去消費  eurekaclient 的 all接口

由此可以看出,消費服務 ,需要指定服務提供方,和需要消費的接口,而引入LoadBalancerClient ,需要host,port,並且配置也很不友好,下面一篇會介紹Ribbon

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM