代码音符

搭建spring cloud eureka注册中心(四):将eureka客户端作为服务提供者创建一个消费者(LoadBalancer方式)

创建时间: 2023-9-8 20:14

修改时间: 2023-9-10 22:16

浏览: 42

在聚合maven工程spring-cloud-demo中创建子模块eureka-consumer,配置pom如下:

<?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>
    <parent>
        <groupId>com.createdpro</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>eureka-consumer</artifactId>

    <name>eureka-consumer</name>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

</project>

创建启动类EurekaConsumerApplication:

package com.createdpro.springcloud;

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

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaConsumerApplication {

    /*
     * 注入一个restTemplate对象,在后面controller中需要用到
     */
    @Bean
    public RestTemplate register() {
        return new RestTemplate();
    }

    public static void main(String[] args) {

        new SpringApplicationBuilder(EurekaConsumerApplication.class)
                .web(WebApplicationType.SERVLET)
                .run(args);

    }

}

创建配置文件application.properties:

spring.application.name=eureka-consumer

server.port=31000

eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka

创建一个controller.Controller.java,用于消费服务提供者的服务:

package com.createdpro.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
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
@Slf4j
public class Controller {

    @Autowired
    private LoadBalancerClient client;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/sayHello")
    public String sayHello() {
        ServiceInstance instance = client.choose("eureka-client");
        if (instance == null) {
            return "no available instances";
        }
        String target = String.format("http://%s:%s/hello", instance.getHost(), instance.getPort());
        log.info("url is {}", target);

        return restTemplate.getForObject(target, String.class);
    }

}

启动服务后,使用http://localhost:31000/sayHello,即可作为消费者消费接收到服务提供者的服务。

创建时间: 2023-9-8 20:14

修改时间: 2023-9-10 22:16

浏览: 42

*本文遵循 CC BY-NC-SA 许可协议。转载请注明出处!