SpringCloud生产消费者
生产者与消费者
上一篇文章介绍了Euarka
的搭建,SpringCloud服务注册中心
本篇文章,我们搭建俩个服务,生产者服务与消费者服务。
本文就以电商系统为例:服务生产者,订单查询服务order-server
,服务消费者order-clien
t
说明:
order-server
服务提供查询订单信息的功能order-client
作为消费者服务,查询订单信息。
生产者服务搭建
选择我们需要的依赖,具体依赖如下
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
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请求包含俩种方法
getForObject
、getForEntity
,他们有什么区别呢?
- 从接口的签名上,可以看出一个是直接返回预期的对象,一个则是将对象包装到
ResponseEntity
封装类中 - 如果只关心返回结果,那么直接用
getForObject
即可 - 如果除了返回的实体内容之外,还需要获取返回的header等信息,则可以使用
getForEntity
@Override
@Nullable
public T getForObject(String url, Class responseType, Object... uriVariables) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}
@Override
@Nullable
public T getForObject(String url, Class responseType, Map uriVariables) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}
@Override
@Nullable
public T getForObject(URI url, Class responseType) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor);
}
@Override
public ResponseEntity getForEntity(String url, Class responseType, Object... uriVariables)
throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
ResponseExtractor> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables));
}
@Override
public ResponseEntity getForEntity(String url, Class responseType, Map uriVariables)
throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
ResponseExtractor> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables));
}
@Override
public ResponseEntity getForEntity(URI url, Class responseType) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
ResponseExtractor> 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 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 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 postForObject(String url, @Nullable Object request, Class responseType,
Object... uriVariables) throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}
@Override
@Nullable
public T postForObject(String url, @Nullable Object request, Class responseType,
Map uriVariables) throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}
@Override
@Nullable
public T postForObject(URI url, @Nullable Object request, Class responseType)
throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters());
return execute(url, HttpMethod.POST, requestCallback, responseExtractor);
}
@Override
public ResponseEntity postForEntity(String url, @Nullable Object request,
Class responseType, Object... uriVariables) throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType);
ResponseExtractor> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables));
}
@Override
public ResponseEntity postForEntity(String url, @Nullable Object request,
Class responseType, Map uriVariables) throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType);
ResponseExtractor> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables));
}
@Override
public ResponseEntity postForEntity(URI url, @Nullable Object request, Class responseType)
throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType);
ResponseExtractor> responseExtractor = responseEntityExtractor(responseType);
return nonNull(execute(url, HttpMethod.POST, requestCallback, responseExtractor));
}
接下来,我们启动,服务消费者,
此时访问我们的eureka
服务,
已经有俩个服务注册上来了,如下:
最后我们来验证一下,服务消费者是否能调用到服务提供者,
在浏览器输出http://localhost:8082/info
根据返回的信息,确认服务调用成功。
小结
以上就是我们基于eureka
的服务注册发现机制,实现的服务之间的相互调用。
当然,这里边还有很多的细节需要讨论,后续的文章,继续和大家讨论。