4.Spring Cloud初相识--------Feign负载均衡
前言:
在上一节里,我们学习了ribbon的使用。
我们了解到ribbon是一个客户端负载均衡机制。
而我们今天要讲的Feign呢,也是一款客户端负载均衡机制。
或者这样说,Feign封装了ribbon的负载均衡,实现了面向接口调用服务编程取缔面向服务编程。
ribbon面向服务编程:
@GetMapping("/hello")
public List<String> sayHello() {
List<String> list = new ArrayList<>();
for(int i=0;i<30;i++) {
list.add(restTemplate.getForObject("http://CL-HELLO-PRODUCER/hello", String.class));
}
return list;
}
feign面向接口编程:
@FeignClient(value="CL-HELLO-PRODUCER")
public interface HelloService {
@GetMapping("/hello")
public String sayHello();
}
新建一个服务消费者(cl_hello_consumer_feign):
1.添加依赖
<?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.xm.cloud</groupId>
<artifactId>cl_hello_consumer_feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cl_hello_consumer_feign</name>
<description>This is a Web about springcloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.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>Finchley.SR2</spring-cloud.version>
</properties>
<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>
<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.修改配置
eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/
eureka.client.register-with-eureka=false
3.开启注解
package com.xm.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ClHelloConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ClHelloConsumerFeignApplication.class, args);
}
}
4.添加Service
package com.xm.cloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value="CL-HELLO-PRODUCER")
public interface HelloService {
@GetMapping("/hello")
public String sayHello();
}
5.添加Controller
package com.xm.cloud.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xm.cloud.service.HelloService;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public List<String> sayHello() {
List<String> list = new ArrayList<String>();
for(int i=0;i<10;i++) {
list.add(helloService.sayHello());
}
return list;
}
}
6.测试
0 | “Hello Spring Cloud! 001号机器” |
---|---|
1 | “Hello Spring Cloud! 003号机器” |
2 | “Hello Spring Cloud! 002号机器” |
3 | “Hello Spring Cloud! 001号机器” |
4 | “Hello Spring Cloud! 003号机器” |
5 | “Hello Spring Cloud! 002号机器” |
6 | “Hello Spring Cloud! 001号机器” |
7 | “Hello Spring Cloud! 003号机器” |
8 | “Hello Spring Cloud! 002号机器” |
9 | “Hello Spring Cloud! 001号机器” |