Spring cloud netfix 新手教程(2021版)| 第三篇 服务调用 OpenFegin和LoadBalanced注解

本文介绍了如何在SpringCloudNetflix架构中创建ShowService模块,并通过Eureka服务发现和OpenFeign实现对GoodService的负载均衡访问。展示了如何使用XML配置和@LoadBalanced注解以及FeignClient进行服务调用的测试过程。
摘要由CSDN通过智能技术生成

创建Show Service子模块

将Show Service注册到Eureka Server,通过open fegin和loadbalance注解两种方式调用Good Service。 Show Service子模块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.example</groupId>
        <artifactId>spring-cloud-netflix-learn</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>spring-cloud-netflix-learn-eureka-show-service</artifactId>

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

    <dependencies>
        <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.example</groupId>
            <artifactId>spring-cloud-netflix-learn-common</artifactId>
            <version>1.0.0</version>
        </dependency>


    </dependencies>

</project>

spring-cloud-netflix-learn-common 通用模块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.example</groupId>
        <artifactId>spring-cloud-netflix-learn</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>spring-cloud-netflix-learn-common</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

</project>

配置properties

server.port=7201


eureka.client.service-url.defaultZone=http://localhost:7001/eureka/


eureka.instance.lease-renewal-interval-in-seconds=2


eureka.instance.lease-expiration-duration-in-seconds=10


eureka.instance.prefer-ip-address=true


eureka.instance.instance-id=netflix-learn-show-application-${server.port}


spring.application.name=show-application



management.endpoints.web.exposure.include=*

主动类开启Eureka Client 和 Feign Client

package com.tubabaxuebiancheng;



import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ShowApplication  {
    public static void main(String[] args)  {
        org.springframework.boot.SpringApplication.run(ShowApplication.class, args);
    }
}

通过LoadBalance注解调用Good Service

配置RestTemplate

package com.tubabaxuebiancheng.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


//怎么切换负载均衡算法?
@Configuration
public class ShowApplicationConfiguration {

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


}

调用Good Service 通过RestTemplate

package com.tubabaxuebiancheng.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class ShowController {


    @Autowired
    private RestTemplate restTemplate;

    private String goodServiceUrl = "http://GOOD-APPLICATION";

    @GetMapping("/show/goods")
    public List showGoods(){
        ResponseEntity<List> entity = restTemplate.getForEntity(goodServiceUrl + "/goods", List.class);
        List goods = entity.getBody();
        return goods;
    }
}

测试

file

通过OpenFeign调用Good Service

创建Fegin Client

package com.tubabaxuebiancheng.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@FeignClient("GOOD-APPLICATION")
public interface GoodClient {

    @GetMapping("/goods")
    public List getGoods();
}

调用Good Service 通过Feign client

package com.tubabaxuebiancheng.controller;


import com.tubabaxuebiancheng.feign.GoodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class ShowController {


    @Autowired
    private RestTemplate restTemplate;

    private String goodServiceUrl = "http://GOOD-APPLICATION";

    @GetMapping("/show/goods")
    public List showGoods(){
        ResponseEntity<List> entity = restTemplate.getForEntity(goodServiceUrl + "/goods", List.class);
        List goods = entity.getBody();
        return goods;
    }

    @Autowired
    private GoodClient goodClient;

    @GetMapping("/show/goods/by-feign")
    public List showGoodsByFeign(){
        return goodClient.getGoods();
    }
}

测试

file

原文链接 兔爸爸学编程

本文由博客一文多发平台 OpenWrite 发布!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值