1.微服务调用的几种方式,异步消息传递,http调用,服务网关调用,服务发现调用nacos;
2.spring提供的restTemplate,发送HTTP请求的客户端工具类;
3.nacos使用服务名报错,需要加Ribbon负载均衡;
RPC(Remote Procedure Call)远程过程调用协议,一种通过网络从远程计算机上请求服务,而不需要了解底层网络技术的协议。RPC它假定某些协议的存在,例如TPC/UDP等,为通信程序之间携带信息数据。在OSI网络七层模型中,RPC跨越了传输层和应用层,RPC使得开发,包括网络分布式多程序在内的应用程序更加容易。
微服务之间的调用可以有以下几种方式:
RestTemplate是Spring Framework提供的一个用于发送HTTP请求的客户端工具类。它简化了在Java应用程序中进行HTTP通信的过程,封装了底层的HTTP连接和请求/响应处理逻辑。
RestTemplate的主要特点和功能包括:
总之,RestTemplate是一个方便易用的HTTP客户端工具类,可以在Java应用程序中发送HTTP请求并处理响应。它是Spring Framework的一部分,与Spring的其他组件集成良好,如Spring Boot、Spring Cloud等。然而,从Spring 5.0版本开始,官方推荐使用WebClient来替代RestTemplate,因为WebClient提供了更强大和灵活的功能。
Ribbon是Netflix开源的一个负载均衡器,它是Spring Cloud中的一个组件,用于在微服务架构中实现客户端的负载均衡。Ribbon可以将客户端请求均匀地分发到多个服务实例上,以提高系统的可用性和性能。
Ribbon的主要特点和功能包括:
总之,Ribbon是一个强大的负载均衡器,可以在微服务架构中实现客户端的负载均衡。它与Spring Cloud的其他组件紧密结合,为构建和管理微服务架构提供了重要的支持。
OpenFeign是一个声明式的Web服务客户端,它是Spring Cloud中的一个组件,用于简化和优化微服务架构中的服务间调用。OpenFeign基于Netflix的Feign库进行了扩展和增强。
OpenFeign的主要特点和功能包括:
总之,OpenFeign简化了微服务架构中的服务间调用,提供了一种简洁、声明式的方式来定义和调用服务API。它与Spring Cloud的其他组件集成良好,是构建和管理微服务架构的重要工具之一。
微服务之间的调用:
生产者(被使用者) —provider
消费者(应用的服务) — consumer
将两个服务注册到nacos
<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.0modelVersion>
<groupId>com.tianjugroupId>
<artifactId>springcloud-restTemplateartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>springcloud-consumermodule>
<module>springcloud-providermodule>
modules>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.13version>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2021.0.5version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2021.0.5.0version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bootstrapartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
project>
spring:
cloud:
nacos:
discovery:
# 能够注册
register-enabled: true
server-addr: http://192.168.111.130:8848/
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
# namespace的id
namespace: 57bca93f-1161-4649-aec3-da79c3aa7cc2
# # 配置单个
group: DEV
name: book-cofig
file-extension: yaml
application:
name: springCloud-provider
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘providerController’: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘book.name’ in value “${book.name}”
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘book.name’ in value “${book.name}”
bootstrap.yml
spring:
cloud:
nacos:
discovery:
# 能够注册
register-enabled: true
server-addr: http://192.168.111.130:8848/
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
# namespace的id
namespace: 57bca93f-1161-4649-aec3-da79c3aa7cc2
# # 配置单个
# group: DEV
# name: book-cofig
# file-extension: yaml
# 配置多个 TODO:配置多个的时候,要加上.yaml后缀,nacos中的命名也要加.yaml
# TODO:如果两个配置里面的值相同,则后面加载进来的会覆盖前面的;
extension-configs:
# - data-id: book-cofig
# group: DEV
# refresh: true
- data-id: book-config1.yaml
group: DEV
refresh: true
- data-id: book-config2.yaml
group: DEV
refresh: true
application:
name: springCloud-provider
application.yml
server:
port: 9099
package com.tianju.cloud.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class, args);
}
}
package com.tianju.cloud.provider.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/provider")
public class ProviderController {
@Value("${book.name}")
private String movieName;
@Value("${spring.application.name}")
private String serverId;
@Value("${server.port}")
private int port;
@GetMapping("/hello")
public String hello(){
String s = "服务"+serverId+",端口:"+port+",电影名:"+movieName;
System.out.println(s);
return s;
}
}
spring:
cloud:
nacos:
discovery:
# 能够注册
register-enabled: true
server-addr: http://192.168.111.130:8848/
application:
name: springCloud-consumer
package com.tianju.consumer.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean(name = "restTemplateA") // 定义名称
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
package com.tianju.consumer.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("/api/consumer")
public class ConsumerController {
@Resource // 根据名称注入
private RestTemplate restTemplateA;
@GetMapping("/nihao")
public String nihao(){
String url = "http://localhost:9099/api/provider/hello";
ResponseEntity<String> forEntity = restTemplateA
.getForEntity(
url,
String.class
);
String body = forEntity.getBody();
System.out.println("消费者获取生产者提供的数据"+body);
return body;
}
}
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-loadbalancerartifactId>
dependency>
package com.tianju.consumer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean(name = "restTemplateA") // 定义名称
public RestTemplate restTemplate(){
return new RestTemplate();
}
@LoadBalanced // 具有软件负载均衡能力
@Bean(name = "restTemplateWith") // 定义名称
public RestTemplate restTemplateWith(){
return new RestTemplate();
}
}
package com.tianju.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("/api/consumer")
public class ConsumerController {
@Resource // 根据名称注入
private RestTemplate restTemplateA;
@GetMapping("/nihao")
public String nihao(){
String url = "http://localhost:9099/api/provider/hello";
ResponseEntity<String> forEntity = restTemplateA
.getForEntity(
url,
String.class
);
String body = forEntity.getBody();
System.out.println("消费者获取生产者提供的数据"+body);
return body;
}
@Resource
private RestTemplate restTemplateWith;
@GetMapping("/nihaox")
public String nihaoX(){
String url = "http://springCloud-provider/api/provider/hello";
ResponseEntity<String> forEntity = restTemplateWith.getForEntity(url, String.class);
String body = forEntity.getBody();
System.out.println("消费者获取生产者提供的数据"+body);
return body;
}
}
1.微服务调用的几种方式,异步消息传递,http调用,服务网关调用,服务发现调用nacos;
2.spring提供的restTemplate,发送HTTP请求的客户端工具类;
3.nacos使用服务名报错,需要加Ribbon负载均衡;