springcloud框架的简单搭建(消费服务基于feign)

上一篇文章主要介绍了如何搭建一个简单的springcloud框架。不过,我们搭建好框架就是为了消费它使用它,那么这篇文章就来看看如何去消费使用我们之前搭建起来的服务吧!

首先本文是基于上一篇文章进行的。

一、Feign简介

Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API。

二、启动程序

继续用上一节的工程, 启动eureka-server,端口为8080; 启动eureka-client两次,端口分别为8081 、8082.

三、创建一个feign的服务

我们首先要新建一个spring-boot工程,取名为serice-feign。

这次我们要选择三个依赖:

对应的pom.xml文件内容为:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.zhouxiaoxi
    eureka-feign
    0.0.1-SNAPSHOT
    eureka-feign
    Demo project for Spring Boot

    
        1.8
        Greenwich.SR1
    

    
        
            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
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


接下来我们就需要配置application.yml文件了:

server:
  #端口号
  port: 8083
spring:
  application:
    #端口名称
    name: eureka-feign
eureka:
  client:
    serviceUrl:
      #服务注册地址
      defaultZone: http://localhost:8080/eureka/

在程序的启动类EurekaFeignApplication,加上@EnableFeignClients注解开启Feign的功能:

package com.zhouxiaoxi.eurekafeign;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class EurekaFeignApplication {

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

}

现在我们需要定义一个feign接口,通过@FeignClient(“服务名”)注解来指定调用哪个服务。
比如在下面的代码中调用了eureka-client服务的“/hello”接口,代码如下:

package com.zhouxiaoxi.eurekafeign.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(value = "eureka-client")
public interface SchedualServiceHello {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);

}

在Web层的controller层,对外暴露一个”/hello”的API接口,通过上面定义的Feign客户端SchedualServiceHello 来消费服务。代码如下:

package com.zhouxiaoxi.eurekafeign.controller;

import com.zhouxiaoxi.eurekafeign.service.SchedualServiceHello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    SchedualServiceHello schedualServiceHello;

    @GetMapping(value = "/hello")
    public String sayHi(@RequestParam String name) {
        return schedualServiceHello.sayHiFromClientOne( name );
    }

}

启动程序,多次访问http://localhost:8083/hello?name=feign,浏览器显示内容为:

hello feign ,i am from port:8081
hello feign ,i am from port:8082

至此我们这个服务消费的框架就搭建完毕了。。。

你可能感兴趣的:(intellij-idea,spring,java)