文章目录
- 1. 简介
- 2. 工程实例
- 2.1 注册中心 springcloud-study-eureka-server
- 2.1.1 依赖 pom.xml
- 2.1.2 配置文件 application.properties
- 2.1.3 启动类 EurekaServerApplication.java
- 2.2 服务提供者 springcloud-study-hello-service
- 2.2.1 依赖 pom.xml
- 2.2.2 配置文件 application.yml
- 2.2.3 控制器 HelloController.java
- 2.2.4 启动类 HelloServiceApplication.java
- 2.3 springcloud-study-feign
- 2.3.1 依赖 pom.xml
- 2.3.2 配置文件 application.yml
- 2.3.3 Fegin 接口 HelloFeign
- 2.3.4 调用 feign 接口的控制器
- 2.3.5 启动类 FeignApplication
- 3. 启动并测试
- 3.1 注册中心的情况
- 3.2 测试访问
- 参考文献
1. 简介
Feign 是一个声明式的伪Http客户端,使用时只需要创建一个接口并注解。
Feign 默认集成了 Ribbon,并和 Eureka 结合,默认实现了负载均衡的效果。
2. 工程实例
在这篇文章: SpringBoot2 整合 SpringCloud Ribbon 实现负载均衡实例 中我们使用RestTemplate+Ribbon去消费服务,本篇文章主要讲述如何通过Feign去消费服务。
工程目录如下图所示:
root 工程 spring-study,root 工程的 pom.xml 如下所示:
<?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.syrdbt</groupId>
<artifactId>study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springcloud-study-eureka-server</module>
<module>springcloud-study-feign</module>
<module>springcloud-study-hello-service</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
2.1 注册中心 springcloud-study-eureka-server
工程目录如下所示:
2.1.1 依赖 pom.xml
pom.xml 的依赖如下所示:
<?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">
<parent>
<artifactId>study</artifactId>
<groupId>com.syrdbt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eurkea-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
2.1.2 配置文件 application.properties
配置文件如下所示:
# 服务注册中心端口号
server.port=8761
# 服务注册中心主机名
eureka.instance.hostname=127.0.0.1
# 不向注册中心注册自己
eureka.client.register-with-eureka=false
# 不需要检索服务
eureka.client.fetch-registry=false
# 注册中心地址,会自动填充 主机名 和 端口号
eureka.client.service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.1.3 启动类 EurekaServerApplication.java
package com.syrdbt.study.eureka.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author syrdbt
* @date 2019-10-12
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2.2 服务提供者 springcloud-study-hello-service
工程目录如下所示:
2.2.1 依赖 pom.xml
pom.xml 如下所示:
<?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">
<parent>
<artifactId>study</artifactId>
<groupId>com.syrdbt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.2 配置文件 application.yml
eureka:
client:
serviceUrl:
#注册中心的地址
defaultZone: http://localhost:8761/eureka/
server:
#当前服务端口号
port: 8762
spring:
application:
#当前应用名称,当前应用的名称
name: service-hi
2.2.3 控制器 HelloController.java
package com.syrdbt.study.hello.service.controller;
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;
/**
* @author syrdbt
* @date 2019-10-12
*/
@RestController
public class HelloController {
@Value("${server.port}")
private String port;
@RequestMapping(value = "hello", method = RequestMethod.GET)
public String index() {
return "eurekaClient:Hello World " + this.port;
}
}
2.2.4 启动类 HelloServiceApplication.java
package com.syrdbt.study.hello.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author syrdbt
* @date 2019-10-12
*/
@SpringBootApplication
@EnableEurekaClient
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
}
2.3 springcloud-study-feign
2.3.1 依赖 pom.xml
<?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">
<parent>
<artifactId>study</artifactId>
<groupId>com.syrdbt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
</project>
2.3.2 配置文件 application.yml
application.yml:
server:
port: 8765
spring:
application:
# 该服务的名字
name: server-feign
eureka:
client:
serviceUrl:
# eureka服务的地址
defaultZone: http://localhost:8761/eureka/
2.3.3 Fegin 接口 HelloFeign
通过@ FeignClient(“服务名”),来指定调用哪个服务。下面的示例中调用了service-hi服务的“/hello”接口,HelloFeign.java 代码如下:
package com.syrdbt.study.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author syrdbt
* @date 2019-10-12
*/
@FeignClient(name = "service-hi")
public interface HelloFeign {
@RequestMapping(value="/hello")
String hello();
}
2.3.4 调用 feign 接口的控制器
HelloController.java,通过上面定义的Feign客户端 HelloFeign 来消费服务。
package com.syrdbt.study.feign.controller;
import com.syrdbt.study.feign.HelloFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author syrdbt
* @date 2019-10-12
*/
@RestController
public class HelloController {
@Autowired
private HelloFeign helloFeign;
@GetMapping(value="/hello")
public String getHi() {
return helloFeign.hello();
}
}
2.3.5 启动类 FeignApplication
package com.syrdbt.study.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author syrdbt
* @date 2019-10-12
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
3. 启动并测试
需要启动4个实例。
⚠️注意:HelloServiceApplication 需要启动两个,两个的端口号分别为 8762 和 8763 。
IDEA 启动多实例教程:。
3.1 注册中心的情况
访问 http://localhost:8761/ 。
注册中心注册的 实例如下所示:
3.2 测试访问
访问:http://localhost:8765/hello 。
运行截图如下所示:
参考文献
- SpringCloud 微服务实战 - 作者 :崔永超老师
- https://www.fangzhipeng.com/springcloud/2018/08/03/sc-f3-feign.html