远程调用常用有RestTemplate和Feign,底层均使用Ribbon实现负载均衡。简单样例,演示远程集群调用。
一、RestTemplate + Ribbon
访问方式:http://localhost:7903/condition/test,然后会执行以下访问:http://microservice-b/condition/self,如果microservice-b有多个节点,会按照负载均衡策略轮流访问。注意为顺利执行样例,microservice-b要实现/condition/self的mapping。
该例子无需额外添加spring-cloud-starter-netflix-ribbon依赖。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.abcd</groupId>
<artifactId>SpringCloudServiceConsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringCloudServiceConsumer</name>
<description>SpringCloudServiceConsumer</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.0</spring-cloud.version>
</properties>
<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>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>-->
<!-- <version>1.4.5.RELEASE</version>-->
<!-- </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>
application.yml
server:
port: 7903
spring:
application:
name: microservice-a
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
ServiceA的java代码如下
Application代码
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceAApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
Controller代码:
@RestController
@RequestMapping("condition")
public class ServiceAController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("self")
public String self(){
return "microService A:7903";
}
@GetMapping("test")
public String test(){
System.out.println("test in service A");
String test = restTemplate.getForEntity("http://microservice-b/condition/self",String.class).getBody();
return "This is ribbon call from microService A to " + test;
}
}
二、Feign + Ribbon
引入spring-cloud-starter-openfeign,feign整合了ribbon。
访问方式:http://localhost:7900/condition/feignTest,然后会访问到microservice-a的 /condition/self接口。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>EurekaClientSample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EurekaClientSample</name>
<description>EurekaClientSample</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.0</spring-cloud.version>
</properties>
<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>
<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>
application.yml
server:
port: 7900
spring:
application:
name: microservice-b
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
.java
Application代码
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class ServiceBApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceBApplication.class, args);
}
}
Controller代码
@RestController
@RequestMapping("condition")
public class ServiceBController {
@GetMapping("self")
public String self(){
return "microService B: 7900";
}
@Autowired
private RemoteClient remoteClient;
//http://localhost:7900/condition/feignTest
@GetMapping("feignTest")
public String feignTest(){
String test = remoteClient.self();
return "This is feign call from microService B to " + test;
}
}
FeignClient代码
@FeignClient("microservice-a")
public interface RemoteClient {
@GetMapping("condition/self")
public String self();
}