写点什么

SpringCloud 之服务提供者与消费者

发布于: 2020 年 05 月 23 日
SpringCloud之服务提供者与消费者
生产者与消费者



上一篇文章我们介绍了Euarka的搭建,本篇文章,我们搭建俩个服务,生产者服务与消费者服务。

我们就以电商系统为例:服务生产者,订单查询服务order-server,服务消费者order-client

说明:order-server 服务提供查询订单信息的功能

order-client作为消费者服务,查询订单信息。

生产者服务搭建








选择我们需要的依赖,具体依赖如下:

<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>


spring-cloud-starter-netflix-eureka-client 表示该服务是一个eureka的客户端,

订单服务会注册到eureka服务端上

依赖添加完成后,我们需要在SpringBoot项目的入口类上加上@EnableDiscoveryClient注解,

表示开启服务注册到eureka服务上:

@SpringBootApplication
@EnableDiscoveryClient
public class OrderServiceApplication {

public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}

}


接下来,需要我们对订单服务进行必要的配置:

server:
port: 8081

spring:
application:
name: order-service

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka


说明:server.prot:订单服务的端口

spring.application.name:订单服务的名称,

这个名称会注册到eureka服务上

eureka.client.service-url.defaultZone:eureka服务的地址。

经过简单的配置以后,我们启动服务,访问eureka服务,http://localhost:8761,如下图:





我们将订单服务以及注册到eureka服务上了。此事,我们提供一个订单查询的接口,模拟订单查询功能。

@RestController
@RequestMapping("query")
public class OrderQueryController {
@Autowired
OrderQueryService queryService;

@RequestMapping("info")
public String queryOrder(){
return queryService.queryOrder();
}
}


@Service
public class OrderQueryService {

public String queryOrder(){
return "订单信息查询成功";
}
}


消费者服务搭建



只需要像搭建服务提供者一样搭建服务消费者就可以,搭建完成以后,我们使用RestTemplate来调用订单服务进行订单信息查询,具体配置及代码如下:

server:
port: 8082

spring:
application:
name: order-client

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka




//提供一个RestTemplate实例
@SpringBootApplication
@EnableDiscoveryClient
public class OrderClientApplication {

public static void main(String[] args) {
SpringApplication.run(OrderClientApplication.class, args);
}

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

//编写测试接口
@RestController
public class ClientController {
@Autowired
ClientService clientService;

@RequestMapping(value = "/info",method = RequestMethod.GET)
public String queryOrderInfo(){
return "restTemplate访问服务提供者返回的信息:"+clientService.queryOrderInfo();
}
}

//模拟具体查询业务逻辑
@Service
public class ClientService {

@Autowired
RestTemplate restTemplate;

public String queryOrderInfo(){
System.out.println("通过restTemplate访问服务提供者");
return restTemplate.getForEntity("http://ORDER-SERVER/query/info", String.class).getBody();
}

}


RestTemplate 介绍:

是spring框架提供的可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可。相较于之前常用的HttpClient,RestTemplate是一种更优雅的调用RESTful服务的方式。

源码分析



部分源码如下,可以看出,它包含了Get、Post、Put等请求,比如Get请求包含俩种方法getForObjectgetForEntity,他们有什么区别呢?

  • 从接口的签名上,可以看出一个是直接返回预期的对象,一个则是将对象包装到 ResponseEntity 封装类中

  • 如果只关心返回结果,那么直接用 getForObject 即可

  • 如果除了返回的实体内容之外,还需要获取返回的header等信息,则可以使用 getForEntity

@Override
@Nullable
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}

@Override
@Nullable
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}

@Override
@Nullable
public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor);
}

@Override
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables)
throws RestClientException {

RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables));
}

@Override
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)
throws RestClientException {

RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables));
}

@Override
public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.GET, requestCallback, responseExtractor));
}


// HEAD

@Override
public HttpHeaders headForHeaders(String url, Object... uriVariables) throws RestClientException {
return nonNull(execute(url, HttpMethod.HEAD, null, headersExtractor(), uriVariables));
}

@Override
public HttpHeaders headForHeaders(String url, Map<String, ?> uriVariables) throws RestClientException {
return nonNull(execute(url, HttpMethod.HEAD, null, headersExtractor(), uriVariables));
}

@Override
public HttpHeaders headForHeaders(URI url) throws RestClientException {
return nonNull(execute(url, HttpMethod.HEAD, null, headersExtractor()));
}


// POST

@Override
@Nullable
public URI postForLocation(String url, @Nullable Object request, Object... uriVariables)
throws RestClientException {

RequestCallback requestCallback = httpEntityCallback(request);
HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor(), uriVariables);
return (headers != null ? headers.getLocation() : null);
}

@Override
@Nullable
public URI postForLocation(String url, @Nullable Object request, Map<String, ?> uriVariables)
throws RestClientException {

RequestCallback requestCallback = httpEntityCallback(request);
HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor(), uriVariables);
return (headers != null ? headers.getLocation() : null);
}

@Override
@Nullable
public URI postForLocation(URI url, @Nullable Object request) throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request);
HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor());
return (headers != null ? headers.getLocation() : null);
}

@Override
@Nullable
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,
Object... uriVariables) throws RestClientException {

RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}

@Override
@Nullable
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,
Map<String, ?> uriVariables) throws RestClientException {

RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}

@Override
@Nullable
public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
throws RestClientException {

RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters());
return execute(url, HttpMethod.POST, requestCallback, responseExtractor);
}

@Override
public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request,
Class<T> responseType, Object... uriVariables) throws RestClientException {

RequestCallback requestCallback = httpEntityCallback(request, responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables));
}

@Override
public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request,
Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {

RequestCallback requestCallback = httpEntityCallback(request, responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables));
}

@Override
public <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType)
throws RestClientException {

RequestCallback requestCallback = httpEntityCallback(request, responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.POST, requestCallback, responseExtractor));
}



接下来,我们启动,服务消费者,此时访问我们的eureka服务,已经有俩个服务注册上来了,如下:





最后我们来验证一下,服务消费者是否能调用到服务提供者,在浏览器输出http://localhost:8082/info





根据返回的信息,确认服务调用成功。

总结:以上就是我们基于eureka的服务注册发现机制,实现的服务之间的相互调用。当然,这里边还有很多的细节需要讨论,后续的文章,继续和大家讨论。

如有收获请关注微信公众号!!!





发布于: 2020 年 05 月 23 日阅读数: 57
用户头像

强化内功、持续改进、不断叠加、保持耐心 2018.03.23 加入

行走在江湖的程序员 个人网站:http://blog.triumphxx.com.cn 微信公众号:北漂码农有话说

评论

发布
暂无评论
SpringCloud之服务提供者与消费者