• springcloud学习2:使用feign进行微服务之间的调用


    springcloud学习2:使用feign进行微服务之间的调用

    一、简单说明

    在spring cloud 中有两种服务调用方式,一种是ribbon+restTemplate ,另一种是feign。相对来说,feign因为注解使用起来更简便。而restTemplate需要我们自定义一个RestTemplate,手动注入,并设置成LoadBalance。

    eign是声明式的web service客户端 ,使用feign简化了微服务之间的调用,可以像调用接口一样调用别的服务。

    使用feign的前提是服务提供者和消费者都注册到了eureka中,在服务消费者中集成feign,调用注册到eureka中的服务。

    二、在服务消费者工程中集成feign

    集成feign到工程中,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>
    
        <groupId>com.lyy</groupId>
        <artifactId>demo_consumer_user</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.M8</spring-cloud.version>
        </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>
    
            <!--feign的配置-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
            <!--热部署配置-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.16</version>
            </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>
    
     
    </project>
    

    在工程的启动类上加@EnableFeignClients注解,说明这是一个feign客户端

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

    创建一个接口,在接口上使用@FeignClient注解,如下

    @FeignClient("demo-provider-user")
    public interface IUserInfoClient {
    
        @GetMapping("/provider/user/findAll")
        List<User> findAll();
    }
    

    其中FeignClient注解的默认参数是要调用的服务在eureka server中的服务id。在接口中定义抽象方法,打上对应的springmvc注解,就可以连接到目标服务中的指定接口上,这里的@GetMapping("/provider/user/findAll")中的参数就是这个接口在目标服务中的请求url

    这样就集成完成了,在服务消费者方只要调用这个接口中的方法就完成了对目标微服务的调用。

    public class UserController {
    
        @Autowired
        private IUserInfoClient userInfoClient;
    
        @GetMapping("/findAll")
        public List<User> findAll(){
            List<User> all = userInfoClient.findAll();
            for (User user : all) {
                System.out.println(user);
            }
            return all;
        }
    }
    
    
  • 相关阅读:
    :Netty中的Idle事件
    :Netty中的Idle事件
    大数据分析在石化企业的应用探讨
    全关联优化
    Java中的instanceof关键字
    Java中的instanceof关键字
    第七章 正则模式
    Rac sequence
    监控ping
    第六章 HASH
  • 原文地址:https://www.cnblogs.com/chengxuxiaoyuan/p/12874015.html
Copyright © 2020-2023  润新知