zoukankan      html  css  js  c++  java
  • SpringCloud学习之Feign 的使用(五)

     Feign 是一个声明式的伪RPC的REST客户端,它用了基于接口的注解方式,很方便的客户端配置,刚开始使用时还不习惯,感觉是在客户端写服务端的代码,Spring Cloud 给 Feign 添加了支持Spring MVC注解,并整合Ribbon及Eureka进行支持负载均衡。

    Feign的使用很简单,有以下几步:

    1、添加依赖

    2、启动类添加 @EnableFeignClients 注解支持

    3、建立Client接口,并在接口中定义需调用的服务方法

    4、使用Client接口。

    具体如下首先先添加feign需要的依赖

    1、添加 spring-cloud-starter-openfeign 依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>


          完整的 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 https://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.2.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.xu</groupId>
        <artifactId>service-consumer-feign</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>service-consumer-feign</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Hoxton.M3</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.48</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
        </repositories>
    
    </project>
    

    2、启动类添加 @EnableFeignClients 注解支持

    package com.xu.serviceconsumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    public class ServiceConsumerFeignApplication {
    
        public static void main(String[] args) {
    
            SpringApplication.run(ServiceConsumerFeignApplication.class, args);
        }
    
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
    
            return new RestTemplate();
        }
    }
    


    3、建立Client接口,并在接口中定义需调用的服务方法

    package com.xu.serviceconsumer.service;
    
    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(name = "service-hello")
    public interface UserFeignClient {
    
        @RequestMapping(value="/hi",method = RequestMethod.GET)
        String getName(@RequestParam String name);
    
    }
    


        说明:>> name="service-hello",这里的"service-hello"是注册到Eureka 中的要调用的应用名

                    >> @GetMapping("/hi") 这里的“/hi”是要调用的应用里的相应的方法(这里需注意,如果service-hello服务下面的                                  访问路径是 /hi/hello ,则这里也要写"/hi/hello")。

    4、使用Client接口

    package com.xu.serviceconsumer.controller;
    
    import com.alibaba.fastjson.JSONObject;
    import com.fasterxml.jackson.databind.util.JSONPObject;
    import com.xu.serviceconsumer.service.HelloService;
    import com.xu.serviceconsumer.service.UserFeignClient;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    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.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @RestController
    public class HelloControler {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(HelloControler.class);
    
        @Autowired
        private HelloService helloService;
    
        @Autowired
        private LoadBalancerClient loadBalancerClient;
    
        @Autowired
        private UserFeignClient userFeignClient;
    
        @RequestMapping(value = "/hi")
        public String hi(@RequestParam String name) {
    
            return helloService.hiService(name);
        }
    
        @RequestMapping(value = "/hello")
        public String hello(@RequestParam String name) {
    
            return userFeignClient.getName(name);
        }
    
        @RequestMapping(value = "/log-instance")
        public Object logInstance() {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("SERVICE-HELLO");
            LOGGER.info("{}:{}:{}", serviceInstance.getServiceId(),
                    serviceInstance.getHost(),serviceInstance.getPort());
    
            JSONObject object1 = new JSONObject();
            object1.put("ServiceId",serviceInstance.getServiceId());
            object1.put("Host",serviceInstance.getHost());
            object1.put("Port",serviceInstance.getPort());
            return object1.toJSONString();
        }
    
    }
    
    

    打开浏览器,访问上面Controller中的RestApi请求http://localhost:8767/hello?name=Ronnie结果如下:

     
    看到这里显然我们的请求响应成功了,成功调用了服务提供者的服务,本节课程到此就结束了,各位如有疑问请再下方留言,我会尽快答复,谢谢观赏!

    ===============================================================================

    如果您觉得此文有帮助,可以打赏点钱给我支付宝或扫描二维码

  • 相关阅读:
    什么是静态测试、动态测试、黑盒测试、白盒测试、α测试 β测试
    软件产品质量特性
    目前主要的测试用例设计方法是什么?
    软件的安全性应从哪几个方面去测试?
    软件配置管理的作用?软件配置包括什么?
    HDOJ1003(DP)
    HDOJ5650
    POJ1068(模拟)
    POJ2586(贪心)
    HDOJ1548(BFS)
  • 原文地址:https://www.cnblogs.com/xulijun137/p/12209731.html
Copyright © 2011-2022 走看看