eureka client 消费者

本文介绍了如何使用Spring Cloud中的Feign客户端进行服务消费,重点讲解了如何集成FeignClient并演示了通过ribbon实现负载均衡。首先,通过添加spring-cloud-starter-openfeign依赖,然后创建FeignClient接口并配置负载均衡,最后在启动类中启用FeignClients并展示简单调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一个文章介绍了如何搭建一个 消费者 eureka client,如何注册到server,这篇文章介绍如何消费。

一般情况下有2种方式:一种是ribbon+restTemplate,另一种是feign,来调用服务

1、 ribbon+restTemplate

这里需要注意的就是负载均衡

2、feign

2.1、pom.xml添加openfeign

<?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>
        <groupId>cn.dails</groupId>
        <artifactId>dails-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath></relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dms-asset</artifactId>
    <name>dms-asset 客户资产</name>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </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.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>dms-asset</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2、添加一个接口FeignClient

package cn.dms.service.feignClient;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "dms-user")
public interface UserService {

    @RequestMapping(value = "/user/private/findUser",method = RequestMethod.GET)
    String findUser(@RequestParam(value = "name") String name);
}

2.3、启动类添加@EnableFeignClients

package cn.dms;

import cn.dms.service.feignClient.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@RestController
public class AssetApplication {

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

    @Value("${server.port}")
    String port;

    @Autowired
    UserService userInterface;

    @RequestMapping("/hello")
    public String home(@RequestParam(value = "name", defaultValue = "屌丝") String name) {
        return userInterface.findUser("屌丝");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值