美文网首页
Spring Cloud之旅(三) -- 服务消费端

Spring Cloud之旅(三) -- 服务消费端

作者: 寒飞子 | 来源:发表于2018-06-27 16:54 被阅读14次

    前言

    Spring Cloud里的远程服务调用采用两种方式,一种是RestTemplate,另一种是Feign。两种方式都可以用Ribbon实现负载均衡,其中Feign自带了Ribbon。

    开始创建

    pom.xml

    <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.0.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.asiainfo.aigov</groupId>
        <artifactId>consumer-feign</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>consumer-feign</name>
        <url>http://maven.apache.org</url>
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        </properties>
        
        <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>
    
        <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.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
        </dependencies>
    </project>
    

    application.yml

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8764
    spring:
      application:
        name: consumer-feign
    

    ConsumerFeignApplication

    RestTemplate上加@LoadBalanced表示开启负载均衡

    package com.asiainfo.aigov.consumer_feign;
    
    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;
    
    @EnableDiscoveryClient
    @EnableFeignClients
    @SpringBootApplication
    public class ConsumerFeignApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerFeignApplication.class, args);
        }
        
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
        
    }
    

    IHelloService

    采用Feign方式调用远程服务,Feign也可称为伪HTTP客户端。@FeignClient后面是服务名,@RequestParam("name")必须有,不然值传不过去。另外,接口的方法名和参数不一定要和实际调用的方法一样,调用方法是根据RequestMapping,参数可以多也可以少,这点和dubbo有区别。

    package com.asiainfo.aigov.consumer_feign.service;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient("eureka-client")
    public interface IHelloService {
    
        @RequestMapping("/hello")
        String hello(@RequestParam("name") String name);
        
    }
    

    HelloController

    package com.asiainfo.aigov.consumer_feign.web.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import com.asiainfo.aigov.consumer_feign.service.IHelloService;
    
    @RestController
    public class HelloController {
    
        @Autowired
        private IHelloService helloService;
        
        @Autowired
        private RestTemplate restTemplate;
        
        @RequestMapping("/hello")
        public String hello(String name) {
            return helloService.hello(name);
        }
        
        @RequestMapping("/hello2")
        public String hello2(String name) {
            return restTemplate.getForObject("http://eureka-client/hello?name="+name, String.class);
        }
        
    }
    

    相关文章

      网友评论

          本文标题:Spring Cloud之旅(三) -- 服务消费端

          本文链接:https://www.haomeiwen.com/subject/ihciyftx.html