Eureka服务发现与消费

通过上篇博文 Eureka服务注册与发现 发布了Eureka服务器和微服务,这次就来研究一下如何使用Eureka注册管理的服务,Eureka服务端还是用上次的Eureka服务端。因为要使用负载均衡,这次要多启动几个注册的服务。

多启动几个提供微服务的客户端,首先复制其中的application.properties,复制几份,把其中server.port改成不同的。

Eureka服务发现与消费

我用的开发工具是idea,在Edit Configuration中配置同时启动多个实例,加载不同配置文件,

--spring.profiles.active=peer1

--spring.profiles.active=peer2 

Eureka服务发现与消费

 

启动两个服务后,在服务器页面可以看到这个服务有两个端口启动Eureka服务发现与消费

 

服务消费

接下来就演示如何调用这些服务,新建sring boot项目

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</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-eureka-client</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>

application.properties

server.port= 8082
spring.application.name=service-consumer-ribbon
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

启动类,用ribbon实现负载均衡

package com.example.eurekaconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class EurekaConsumerApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

新建一个Controller,调用Eureka注册的服务

package com.example.eurekaconsumer.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;

@RestController
public class TestController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/consumer")
    public String test(){
//        service-helloworld为需要调用的服务的注册名称
        String result = this.restTemplate.getForObject("http://service-helloworld/test", String.class);
        return result;
    }
}

启动后,访问,消费服务的接口,即可看到注册的微服务被调用,这个过程并没有使用注册的微服务的ip,

Eureka服务发现与消费Eureka服务发现与消费

从结果看,因为使用了复杂均衡,每次请求会是不同的后台微服务来处理请求