第五章 声明式服务调用Feign

快速入门

  • 在本节中,继续使用第三章的eureka集群,注册服务,第四章eureka客户端来做作服务的提供者,之后通过feign提供的申明式服务绑定功能来实现对该服务的接口的调用。

服务提供方添加代码

  • 创建controller
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    private final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Value("${spring.application.name}")
    private String applicationName;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        logger.info("访问的服务名称是:"+applicationName);
        return "hello world :"+applicationName;
    }

}

创建服务消费方

  • 创建springboot项目添加依赖,
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</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>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  • 在resource下创建application.yml,添加配置。
spring:
  application:
    name: feign-consumer

server:
  port: 9001

eureka:
  client:
    service-url:
      defaultZone: http://springcloudtest.com:1111/eureka/,http://springcloudtest.com:1112/eureka/
  • 创建服务调用接口HelloService通过注解@FeignClient注解指定服务名来绑定服务,然后在使用springmvc的注解绑定具体的服务接口。
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("hello-client1")
public interface HelloService {

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

注意:这里的服务名不区分大小写,所以使用hello-client1或者HELLO-CLINET1都是可以的。

  • 创建一个HelloController,来实现对Feign客户端的调用。
import com.dome.service.HelloService;
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.RestController;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @RequestMapping(value = "/feign-consumer",method = RequestMethod.GET)
    public String helloConsumer(){
        return helloService.hello();
    }
}
  • 最后创建主类,EurekaClient1Application并通过@EnableFeignClients注解开启feign的支持功能。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class EurekaClient1Application {

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

测试验证

  • 分别启动注册中心和两个客户端。
    服务注册成功
  • 使用浏览器测试服务访问服务http://localhost:9001/feign-consumer
    服务调用成功
  • 通过访问可以看到通过访问消费者的接口,我们访问到了提供者的服务,通过feign我们只需要定义服务绑定的接口,就可以实现服务调用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值