码迷,mamicode.com
首页 > 其他好文 > 详细

申明式服务调用Feign之一:入门示例

时间:2017-09-13 19:15:31      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:.com   hello   feign   add   plugin   tom   location   编码   pac   

新建一个spring-boot工程,取名为feign-consumer,在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-eureka、Web的起步依赖spring-boot-starter-web,pom代码如下:

<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>
    <groupId>com.dxz</groupId>
    <artifactId>feign-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>   <!--配合spring cloud版本 -->
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <!--设置字符编码及java版本 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--增加eureka-server的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!--增加feign的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!--用于测试的,本例可省略 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--依赖管理,用于管理spring-cloud的依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Brixton.SR3</version>   <!--官网为Angel.SR4版本,但是我使用的时候总是报错 -->
                <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>
</project>

在工程的配置文件application.yml文件,指定程序名、端口号、服务注册地址,代码如下:

spring.application.name=compute-consume-feign
server.port=2231
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

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

package com.dxz.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class ConsumerApplication {

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

定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了远程服务的接口,代码如下:

package com.dxz.feign.remote;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("compute-service")
public interface HelloService {
    
    @RequestMapping(value="/add", method = RequestMethod.GET)
    String hello(@RequestParam("a") Integer a, @RequestParam("b") Integer b, @RequestParam("sn") Integer sn);

}

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

package com.dxz.feign.service;

import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dxz.feign.remote.HelloService;

@RestController
public class AddConsumerController {

    @Autowired
    HelloService helloService;
    
    private AtomicInteger sn = new AtomicInteger(0);
    
    @RequestMapping(value="/feign-consumer", method = RequestMethod.GET)
    public String helloConsumer(@RequestParam Integer a, @RequestParam Integer b) {
        return helloService.hello(a, b, sn.incrementAndGet());
    }
}

启动程序,多次访问http://127.0.0.1:2231/feign-consumer?a=1&b=3,浏览器显示:

技术分享

 技术分享

 

申明式服务调用Feign之一:入门示例

标签:.com   hello   feign   add   plugin   tom   location   编码   pac   

原文地址:http://www.cnblogs.com/duanxz/p/7516676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!