一、新建两个 Eureka Client
新建过程参考上一篇文章,建好之后我们将使用 Feign 组件实现在服务提供者1 (client01) 中调用服务提供者2 (client02) 的接口,client02 为了提供一个接口给 client01 调用,要在原有的基础上,在 HelloController 上新增一个接口。
/**
* 新增一个接口供 client01 调用
* @return 自己的端口和服务名
*/
@RequestMapping(value = "/forclient01", method = RequestMethod.GET)
public String forClient01() {
return "I am from port: " + port+ " ,my name is "+ name;
}
该类完整代码如下
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
//该注解可以读取配置文件中的值赋予下面的变量
@Value("${server.port}")
private String port;
@Value("${spring.application.name}")
private String name;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(@RequestParam(value = "name", defaultValue = "dhsg") String name) {
return "Hello " + name + " ,I am from port:" + port;
}
/**
* 新增一个接口供 client01 调用
* @return 自己的端口和服务名
*/
@RequestMapping(value = "/forclient01", method = RequestMethod.GET)
public String forClient01() {
return "I am from port: " + port+ " ,my name is "+ name;
}
}
二、修改 client01 的 pom 文件
添加如下依赖
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
三、client01 的启动类添加注解 @EnableFeignClients
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 添加注解 @EnableFeignClients 注解开启Feign的功能
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class Client01Application {
public static void main(String[] args) {
SpringApplication.run(Client01Application.class, args);
}
}
四、client01 新建一个 service 包,再新建一个 IUserOtherApiService 接口
package com.dhsg.sc.client01.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 添加注解 @FeignClient(value = "client02") 的作用是能够访问服务提供者 client02 的接口
*/
@Service
@FeignClient(value = "client02")
public interface IUseOtherApiService {
/**
* 调用 client01 的 /forclient01 接口
*
* @return 获取到 client01 的端口和服务名并返回
*/
@RequestMapping(value = "/forclient01", method = RequestMethod.GET)
String getClient02Name();
}
五、在 client01 的 HelloController 上新增一个接口
/**
* 该接口是通过 feign 组件,访问 client02 的接口
*
* @return client02 的端口和服务名
*/
@RequestMapping(value = "/getclient02name", method = RequestMethod.GET)
public String getclient02name() {
return userOtherApiService.getClient02Name();
}
该类完整代码如下
import com.dhsg.sc.client01.service.IUseOtherApiService;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private IUseOtherApiService userOtherApiService;
//该注解可以读取配置文件中的值赋予下面的变量
@Value("${server.port}")
private String port;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(@RequestParam(value = "name", defaultValue = "dhsg") String name) {
return "Hello " + name + " ,I am from port:" + port;
}
/**
* 该接口是通过 feign 组件,访问 client02 的接口
*
* @return client02 的端口和服务名
*/
@RequestMapping(value = "/getclient02name", method = RequestMethod.GET)
public String getclient02name() {
return userOtherApiService.getClient02Name();
}
}