跟我学---三、声明式RESTful客户端:Spring Cloud OpenFeign

OpenFeign 简介
OpenFeign是一个声明式RESTful网络请求客户端。OpenFeign会根据带有注释的函数信息构造出网络请求的模板,在发送网络请求之前,OpenFeign会将函数的参数值设置到这些请求模板中。
1.搭建Eureka 服务注册中心
在idea创建SpringBoot 项目,主要依赖如下:


        1.8
        Greenwich.RELEASE
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

在启动类中添加注释@EnableEurekaServer,代码如下所示:

//会为项目自动配置必须得配置类,标识该服务为注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurakeServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurakeServerApplication.class, args);
    }

}

在application.yml 配置文件中添加一下配置,配置注册中心得端口和标识:

server:
  port: 8761
eureka:
  instance:
    hostname: standalone
    instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
  client:
    register-with-eureka: false #表明该服务不会向Eureka Server注册自己得信息
    fetch-registry: false  #表明该服务不会向Eureka Server获取注册信息
    service-url:    #Eureka Server注册中心得地址,用于Client与Server交流
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eureka-service

2.搭建Euraka服务提供者
在idea创建SpringBoot 项目,主要依赖如下


        1.8
        Greenwich.RELEASE
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

启动类如下:

@SpringBootApplication
public class EurakeClient01Application {

    public static void main(String[] args) {
        SpringApplication.run(EurakeClient01Application.class, args);
    }

}

application.yml 配置如下:

server:
  port: 8762
eureka:
  instance:
    hostname: client
    instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #服务注册中心访问地址

spring:
  application:
    name: feign-service

新建一个controller包,添加一个提供服务的接口,代码如下:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author chencundeng
 * @Description //TODO
 * @Date 2019/2/23
 **/
@RequestMapping("/feign-service")
@RestController
public class FeignServiceController {

    @GetMapping("/say-hello/{name}")
    public String sayHello(@PathVariable("name") String name){
        return "hello ".concat(name).concat("!");
    }

}

3.Feign服务消费者搭建
在idea创建SpringBoot 项目,主要依赖如下


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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

启动类如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class FeignClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignClientApplication.class, args);
    }

}

application.yml 配置如下:

server:
  port: 8763
eureka:
  instance:
    hostname: client
    instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #服务注册中心地址

spring:
  application:
    name: feign-client

新建一个controller包,添加一个Feign客户端接口及提供服务类,代码如下:

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

@FeignClient("feign-service")  //指定调用的远程服务名称
@RequestMapping("/feign-service")
public interface FeignServiceClient {

    @GetMapping("/say-hello/{name}")
    public String sayHello(@PathVariable("name") String name);

}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author chencundeng
 * @Description //TODO
 * @Date 2019/2/22
 **/
@RestController
public class FeignClientController {

    @Autowired
    private FeignServiceClient feignServiceClient;

    @GetMapping("/say-hello/{name}")
    public String sayHello(@PathVariable("name") String name){
        return  feignServiceClient.sayHello(name);
    };


}

4.OpenFeign客户端调用
搭建好上述三个服务后,依次启动三个服务,输入地址http://127.0.0.1:8762/say-hello/feign就可以看到输出语句。

5.例子源码地址如下:
https://gitee.com/mingzhishuyuan/spring-cloud.git

你可能感兴趣的:(微服务)