springcloud(五)服务消费者(Feign)负载均衡

本文介绍了如何在SpringCloud中使用Feign作为服务消费者实现负载均衡。首先,配置了Eureka Server和两个service-client实例。接着,创建了一个名为service-feign的新项目,添加了必要的依赖,并在application.yml中进行了配置。通过@EnableFeignClients注解启用Feign功能,定义了一个Feign接口指明调用service-client服务。在Web层Controller中,使用该Feign客户端调用服务,通过浏览器访问验证了负载均衡的效果,每次请求会交替显示来自不同端口的服务响应。
摘要由CSDN通过智能技术生成

1.准备工作(设置idea开启多个springboot实例)https://blog.csdn.net/qq_42014192/article/details/89245306

启动eureka-server,端口为8761; 启动service-client 两次,端口分别为8762 、8773.

2.新建一个spring-boot工程,取名为:service-feign;

https://blog.csdn.net/qq_42014192/article/details/88742559

3.pom.xml依赖

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springcloud_serice-feign_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud_serice-feign_demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4.配置文件application.yml如下:

server:
  port: 8765 #服务端口

spring:
  application:
    name: service-feign

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

5.在程序的启动类@EnableFeignClients注解开启Feign的功能

@SpringBootApplication
//通过注解@EnableEurekaClient 表明自己是一个eurekaclient
@EnableEurekaClient
//通过@EnableDiscoveryClient向服务中心注册
@EnableDiscoveryClient
//加上@EnableFeignClients注解开启Feign的功能
@EnableFeignClients
public class SpringcloudSericeFeignDemoApplication {

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

}

6.定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-client服务的“/client”接口,代码如下:

@FeignClient(value = "service-client")
public interface HelloService {

    @RequestMapping(value = "/client",method = RequestMethod.GET)
     String hiService(@RequestParam(value = "name") String name);
}

7.在Web层的controller层,对外暴露一个"/client"的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping(value = "/client")
    public String client(@RequestParam String name){
        return helloService.clientService(name);
    }
}

8.在浏览器上多次访问http://localhost:8765/client?name=forezp,浏览器交替显示:

client forezp,i am from port:8762
client forezp,i am from port:8763

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值