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

前言

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

开始创建

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    
    com.asiainfo.aigov
    consumer-feign
    0.0.1-SNAPSHOT
    jar
    consumer-feign
    http://maven.apache.org
    
    
        UTF-8
        1.8
        Finchley.RELEASE
    
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

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之旅(三) -- 服务消费端)