微服务中,很多服务系统都在独立的进程中运行,通过各个服务系统之间的协作来实现一个大项目的所有业务功能。服务系统间 使用多种跨进程的方式进行通信协作,而RESTful风格的网络请求是最为常见的交互方式之一。
spring cloud提供的方式:
1.RestTemplate
2.Feign
一、服务提供者创建
在上一篇文章中我们介绍了服务的注册与发现,在此基础上我们将之前创建的eureka-client作为服务消费方创建一个服务提供方。
1.按照上篇文章中创建eureka-client的方式创建eureka-provider
其中application的配置如下
#指定启动端口号
server.port=5202
#设置服务注册中心的URL,用于client和server端交流
eureka.client.service-url.defaultZone=http://localhost:5200/eureka/
#指定服务名称
spring.application.name=eureka-provide
2.创建服务提供方的ProviderController
1.在包下创建controller.ProviderController,如下图:
2.写入被调用的提供方法
package shadowcoder.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@GetMapping("/hello")
public String hello() {
return "Hello from Eureka Provider!";
}
}
二、RestTemplate方式调用
RestTemplate是Spring框架提供的一个工具类,主要用于简化访问RESTful服务的过程。它是从Spring3.0开始支持的一个HTTP请求工具,它封装了底层的HTTP请求细节,让我们可以以更加优雅和简洁的方式调用RESTful API。
1.启动类配置一个RestTemplate
的Bean,并标记为@LoadBalanced
,以便Spring Cloud能够为其添加负载均衡支持
package com.shadowcoder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Bean
@LoadBalanced // 使得RestTemplate具有负载均衡的能力
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
2.在eureka-client包下创建controller.ConsumerController,结构如图
3.在ConsumerController写入调用服务提供者的API:/call-hello1
package com.shadowcoder.controller;
import com.netflix.appinfo.InstanceInfo;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.netflix.discovery.EurekaClient;
import java.util.List;
@RestController
public class ConsumerController {
@Autowired
EurekaClient client;
private final RestTemplate restTemplate;
@Autowired
public ConsumerController(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
@GetMapping("/call-hello")
public String callHello() {
//获取服务名为eureka-provide的服务实例
List<InstanceInfo> instances = client.getInstancesByVipAddress("eureka-provide", false);
InstanceInfo instanceInfo = instances.get(0);
//打印调用的接口
System.out.println("http://" + instanceInfo.getHostName() +":"+ instanceInfo.getPort() + "/hello");
String url = "http://" + instanceInfo.getHostName() +":"+ instanceInfo.getPort() + "/hello";
//服务提供者的API
String response = restTemplate.getForObject(url, String.class);
return "Response from Service Provider: " + response;
}
}
(备注):通过打印的调用接口发现 实际调用的是eureka的主机名而不是spring.application.name的eureka-provider
4.测试调用接口:/call-hello1
访问localhost:5201/call-hello可以发现出现成功调用了eureka-provide的/hello方法
三、Feign方式调用
Feign是一个声明式的Web服务客户端,它使得编写HTTP客户端变得更简单。在Spring Cloud中,Feign可以很容易地与Eureka等服务发现机制集成,从而实现对微服务的调用。
1.在eureka-client中添加Feign依赖
<?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>
<parent>
<groupId>com.shadowcoder</groupId>
<artifactId>SpringCloudTest</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>eureka-client</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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>
</dependencies>
</project>
2.启动类上添加@EnableFeignClients
注解来启用Feign客户端,代码如下
package com.shadowcoder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Bean
@LoadBalanced // 使得RestTemplate具有负载均衡的能力
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3.包下新增一个接口service.ConsumerService
代码如下:
package com.shadowcoder.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "eureka-provide")//value填写提供方的spring.application.name=eureka-provide
public interface ConsumerService {
//提供方的调用API
@GetMapping("/hello")
String hello();
}
4.在ConsumerController注入ConsumerService以及写入调用服务提供者的API:/call-hello2
package com.shadowcoder.controller;
import com.netflix.appinfo.InstanceInfo;
import com.shadowcoder.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.netflix.discovery.EurekaClient;
import java.util.List;
@RestController
public class ConsumerController {
@Autowired
EurekaClient client;
@Autowired
private ConsumerService consumerService;
private final RestTemplate restTemplate;
@Autowired
public ConsumerController(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
@GetMapping("/call-hello")
public String callHello() {
//获取服务名为eureka-provide的服务实例
List<InstanceInfo> instances = client.getInstancesByVipAddress("eureka-provide", false);
InstanceInfo instanceInfo = instances.get(0);
//打印调用的接口
System.out.println("http://" + instanceInfo.getHostName() + ":" + instanceInfo.getPort() + "/hello");
String url = "http://" + instanceInfo.getHostName() + ":" + instanceInfo.getPort() + "/hello";
//服务提供者的API
String response = restTemplate.getForObject(url, String.class);
return "Response from Service Provider: " + response;
}
@GetMapping("/call-hello2")
public String callHello2() {
return "Response from Service Provider2: " + consumerService.hello();
}
}
5.测试调用接口:/call-hello2
访问localhost:5201/call-hello2可以发现出现成功调用了eureka-provide的/hello方法