Table of contents
Distributed Service Governance_Create Payment Service Producer
Distributed Service Governance_Creating Service Consumers
Service call_difference between Dubbo and OpenFeign
Service call_microservice access to OpenFeign
Distributed Service Governance_Create Payment Service Producer
Create service provider project cloud-provider-payment8001
The POM file introduces dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
Write the main startup class
@Slf4j
//注解开启服务注册与发现功能
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
log.info("************PaymentMain8001 启动成功 ********");
}
}
Note: @EnableDiscoveryClient: Enable the registration discovery service
Write a YML configuration file
server:
port: 8001
spring:
application:
# 微服务名字
name: provider-payment
cloud:
nacos:
discovery:
# Nacos服务地址
server-addr: 192.168.66.101:8848
View Nacos console service
Distributed Service Governance_Creating Service Consumers
Create service provider project cloud-consumer-order80
The POM file introduces dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
Write the main startup class
@SpringBootApplication
@Slf4j
@EnableDiscoveryClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
log.info("************ OrderMain80 启动成功 ********");
}
}
Write a YML configuration file
server:
port: 80
spring:
application:
# 微服务名字
name: consumer-order
cloud:
nacos:
discovery:
# Nacos服务地址
server-addr: 192.168.66.101:8848
View Nacos console service
Service call_difference between Dubbo and OpenFeign
Review Dubbo
Apache Dubbo is a microservice development framework that provides two key capabilities of RPC communication and microservice governance. This means that the microservices developed using Dubbo will have mutual remote discovery and communication capabilities. At the same time, using the rich service governance capabilities provided by Dubbo, service governance demands such as service discovery, load balancing, and traffic scheduling can be realized.
Communication performance
From the analysis of communication performance, SpringCloud's communication uses Openfeign (feign) components. Feign is based on the Http transport protocol, and the underlying implementation is Rest. From the OSI 7-layer model, Rest belongs to the application layer.
Note: In high concurrency scenarios, the performance is not ideal and becomes a performance bottleneck.
The communication protocol of the Dubbo framework adopts the RPC protocol, which belongs to the transport layer protocol, and its performance is naturally higher than rest. Improve the performance of interaction, maintain long connection and high performance.
Note: Dubbo has better performance, such as supporting asynchronous calls, and Netty has better performance.
Real-time effect feedback
1. The bottom layer of OpenFeign is that Feign adopts the ___ protocol.
A RPC
B HTTP
C TCP
All of the above are wrong
2. The communication protocol of the Dubbo framework adopts the RPC protocol, which belongs to the ___ protocol.
A application layer
B transport layer
C session layer
D above are all wrong
Service call_microservice access to OpenFeign
Create project cloud-consumer-openfeign-order80
Add OpenFeign dependency
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
Add feign annotation to the main startup class
@EnableFeignClients
@SpringBootApplication
@Slf4j
@EnableDiscoveryClient
public class OrderOpenFeign80 {
public static void main(String[] args) {
SpringApplication.run(OrderOpenFeign80.class,args);
log.info("*************OrderOpenFeign80 启动成功 ********");
}
}
Create a YML configuration file
server:
port: 80
spring:
application:
# 微服务名字
name: consumer-order
cloud:
nacos:
discovery:
# Nacos服务地址
server-addr: 192.168.66.101:8848
feign:
client:
config:
default:
# ⽹络连接阶段1秒超时 7
connectTimeout: 1000
# 服务请求响应阶段2秒超时
readTimeout: 2000
Create the microservice interface to call
@Service
@FeignClient(value = "provider-payment",fallback = TemplateServiceFallback.class)
public interface PaymentService {
@GetMapping("/payment/index")
String index();
}
Create control layer
/**
* 订单控制层
*/
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private PaymentService paymentService;
/**
* 测试OpenFeign
* @return
*/
@GetMapping("index")
public String index(){
return paymentService.index();
}
}
test
Request http://192.168.66.101:80/order/index