FeignClient

https://blog.csdn.net/u012578322/article/details/84932856

package webber.open.consultation.common.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
*
* @copyright: Copyright (c) 2019
*/
@Component
@FeignClient(name = "vsb", url = "http://123.57.34.235:8080")
public interface UserApi
{
/**
* @param account , password
* @return RestToken
* @Description: 生成token
* @author lujie
* @date 2019/09/09 16:02
*/
@PostMapping(value = "/system/api/user/createAdminUser")
String saveUser(@RequestParam("account") String account, @RequestParam("password") String password);
}

SpringCloud入门(三)之基于Feign的服务间接口调用
什么是Feign
基于Feign的服务间接口调用实战
创建两个微服务
服务提供者
引入spring-boot-starter-web依赖
创建controller类提供接口
配置端口号为8081
服务消费者
引入openfeign依赖
引入服务提供者的接口
引入spring-boot-starter-web依赖
创建controller类提供接口
配置端口号为8082
测试
什么是Feign
Feign是一个声明式WebService客户端。
在SpringCloud用于实现微服务之间的互相调用。
服务消费者无需知道服务提供者的ip和端口,只需要 指定服务名,即可通过注册中心调用到目标服务。

下面我们来实战下如何实现服务间的接口调用

基于Feign的服务间接口调用实战
完整参考代码github

已有基于Eureka注册中心的两个服务;
引入Eureka依赖;
使用注解@EnableEurekaClient声明作为Euraka客户端;
配置服务名和注册中心地址。
ps:如果不配置服务名,则在注册中心显示的服务名为unknown;如果不配置注册中心地址,则无法注册成功,程序启动会报错。

创建两个微服务
创建两个基于Eureka注册中心的微服务,其中一个作为服务消费者,一个作为服务提供者。
如何创建微服务可以参考:

SpringCloud入门(二)之服务注册
SpringCloud入门(一)之Eureka注册中心
服务提供者
把AuthenticationService作为服务提供者,并提供一个http请求接口:GET http://ip:port/user

引入spring-boot-starter-web依赖
在pom文件加入以下依赖引入,版本为2.1.0.RELEASE

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1
2
3
4
创建controller类提供接口
package com.markey.test.authenticationservice.contorller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

@GetMapping("/user")
public String getUser() {
return "小明";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
配置端口号为8081
#配置端口号,默认为8080,已被我的注册中心使用
server.port=8081
#配置服务名,不配置的话,注册中心显示为UnKnown
spring.application.name=AuthenticationService
#配置注册中心地址,不配置的话,程序启动会失败
eureka.client.service-url.defaultZone=http://localhost:8080/eureka/
1
2
3
4
5
6
服务消费者
把CustomService作为服务消费者,并对外提供一个接口http请求:GET http://ip:port/hello

引入openfeign依赖
在pom文件加入以下依赖引入,版本为2.1.0.M2

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
1
2
3
4
ps:这里没有指定版本,是因为使用spring-cloud-dependencies,版本号Greenwich.M3,读者如果没有引用spring-cloud-dependencies的话,可以自行给openfeign依赖加上版本号2.1.0.M2

引入服务提供者的接口
创建一个接口,接口的方法定义即为服务提供者提供的接口,这里是:GET http://ip:port/user

package com.markey.test.customservice.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "AuthenticationService")
@Service
public interface AuthenticationServiceApi {

@GetMapping("/user")
String getUser();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
引入spring-boot-starter-web依赖
在pom文件加入以下依赖引入,版本为2.1.0.RELEASE

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1
2
3
4
创建controller类提供接口
将上一步声明的接口作为一个bean注入;
在方法中调用AuthenticationService提供的接口,就像调用普通bean的方法一样。

package com.markey.test.customservice.controller;

import com.markey.test.customservice.api.AuthenticationServiceApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RootController {

@Autowired
AuthenticationServiceApi authenticationServiceApi;

@GetMapping("/hello")
public String hello() {
//调用服务提供者的接口
return "hello, " + authenticationServiceApi.getUser();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
配置端口号为8082
#配置端口号,默认为8080,已被我的注册中心使用
server.port=8082
#配置服务名,不配置的话,注册中心显示为UnKnown
spring.application.name=CustomService
#配置注册中心地址,不配置的话,程序启动会失败
eureka.client.service-url.defaultZone=http://localhost:8080/eureka/
1
2
3
4
5
6
测试
启动服务注册中心;
启动AuthenticationService服务和CustomService服务
通过浏览器访问/hello接口
————————————————
版权声明:本文为CSDN博主「Markey92」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012578322/article/details/84932856

原文地址:https://www.cnblogs.com/zzl0916/p/11855865.html