我们将一个复杂的业务,拆解成了多个微服务,但微服务中间还需要互相访问才对,springcloud给我们提供了feign,可以很好的解决微服务之间的访问问题,并且还支持负载均衡、熔断等特性,微服务注册到eureka中以后,我们只需要关注服务接口,不需要知道服务来自哪里,服务的ip、端口是什么,这对我们应用架构部署来说,真是个福音。
使用feign非常简单,下面详细介绍下过程。
- 创建一个springboot工程,pom如下
<?xml version="1.0" encoding="UTF-8"?>
<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.liusx</groupId>
<artifactId>feign-auth</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>feign-auth</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<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>
- 修改启动类
@SpringBootApplication
@EnableFeignClients
public class FeignAuthApplication {
public static void main(String[] args) {
SpringApplication.run(FeignAuthApplication.class, args);
}
}
- 修改application.yml
spring:
application:
name: liusx-feign-auth
server:
port: 9081
eureka:
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
client:
register-with-eureka: false
fetch-registry: true
serviceUrl:
defaultZone: http://user:123456@${eureka.instance.hostname}:8761/eureka/ #注册 中心已经开启认证
- 创建service,访问来自liusx-servers-auth中的auth/login服务
package com.liusx.feign.auth.service;
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(value = "liusx-servers-auth")
public interface IAccountService {
@RequestMapping(value = "/auth/login", method = RequestMethod.POST)
String login(@RequestParam("userName") String userName,
@RequestParam("password") String password);
}
- 创建controller,在controller中,我们访问service的login方法,而在service中,具体login实现其实是来自liusx-servers-auth服务
package com.liusx.feign.auth.controller;
import com.liusx.feign.auth.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AccountController {
@Autowired
IAccountService accountService;
@PostMapping("/login")
public String login(String userName, String password) {
return accountService.login(userName, password);
}
}
- 工程创建到这里已经结束,测试下看看对不对,启动liusx-servers-eureka、liusx-servers-auth、liusx-feign-auth3个工程
- 访问地址 http://localhost:9081/login ,可以看到已经成功拿到返回值了