1.服务消费模型
1.1.服务消费模型介绍
分布式系统中,各个微服务会部署多个实例,如何将服务消费者均匀分摊到多个服务提供者实例上,就要使用到负载均衡器
1.2.Feign
openFeign是spring cloud 的成员,feign可以方便的使用接口来实现服务消费模型
2.快速创建项目
新建模块
选择 Nacos Service Discovery / Spring Web / OpenFeign 组件
2.1.依赖
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.2.启动类增加 注解
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class ScaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ScaConsumerApplication.class, args);
}
}
2.3.新建Feign 接口
package com.turing.scaconsumer.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* (服务提供者 spring.application.name: 注册在 nacos )
*
*/
@FeignClient("sca-provider")
public interface ProviderFeign {
@GetMapping("/providerTest/hello")
public String hello();
@RequestMapping("/providerTest/hasParams")
public String hasParams(@RequestParam("str") String str, @RequestParam("i") Integer i);
}
与服务提供者类 方法 相对应 参数 建议使用注解
2.4.在服务消费者对应类中 通过 @Autowried 注入接口
@Autowired
private ProviderFeign providerFeign;
2.5.设置访问超时
yml 格式
feign:
client:
config:
default:
#建立连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间
ConnectTimeOut: 5000
#指建立连接后从服务端读取到可用资源所用的时间
ReadTimeOut: 5000
properties 格式
feign.client.config.default.connect-timeout=5000
feign.client.config.default.read-timeout=5000