🎉🎉欢迎光临🎉🎉
🏅我是苏泽,一位对技术充满热情的探索者和分享者。🚀🚀
🌟特别推荐给大家我的最新专栏《Spring 狂野之旅:底层原理高级进阶》 🚀
本专栏纯属为爱发电永久免费!!!
这是苏泽的个人主页可以看到我其他的内容哦👇👇
努力的苏泽
http://suzee.blog.csdn.net/
目录
Spring Cloud的注册发现机制是为了解决微服务架构中服务实例的动态变化和通信的问题。以下是使用Spring Cloud注册发现机制 本文重点讲解其使用方法及原理
I. Spring Cloud的注册发现机制
Eureka的架构设计
Ribbon的用法
Feign的用法
实际用法
Eureka的架构设计
Eureka是Spring Cloud中的一个服务注册和发现组件,它采用了客户端-服务器的架构设计。
Eureka的核心概念
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
} @SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
} Eureka Server的角色和功能
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
} import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
@RestController
class UserController {
@GetMapping("/users")
public String getUsers() {
return "User list";
}
} # 心跳间隔(单位:秒) eureka.instance.lease-renewal-interval-in-seconds=30 # 心跳超时时间(单位:秒) eureka.instance.lease-expiration-duration-in-seconds=90
# 剔除策略启用 eureka.server.eviction.enabled=true # 剔除策略的时间间隔(单位:秒) eureka.server.eviction.interval-time-in-ms=60000
eviction.enabled设置为true表示启用剔除策略,eviction.interval-time-in-ms表示剔除策略的时间间隔。如果在该时间间隔内没有收到服务实例的心跳信息,Eureka Server将自动将其从注册表中剔除。
通过以上示例代码和配置,可以实现服务注册、续约和剔除的功能,确保服务实例在Eureka Server上的动态注册和注销。
Eureka Client的角色和功能
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
}
@RestController
class UserController {
private final RestTemplate restTemplate;
public UserController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/users")
public String getUsers() {
// 通过服务名进行服务发现和调用
String url = "http://service-provider/users";
return restTemplate.getForObject(url, String.class);
}
}
在上述代码中,使用@EnableDiscoveryClient注解启用服务发现功能,并通过RestTemplate发起服务调用。
Eureka Server可以以集群的方式部署,实现高可用性和负载均衡。在集群中,各个Eureka Server之间通过复制注册表信息来保持一致性。
Eureka集群架构的设计和部署
# Eureka Server 1 eureka.client.serviceUrl.defaultZone=http://server1:8761/eureka/ # Eureka Server 2 eureka.client.serviceUrl.defaultZone=http://server2:8762/eureka/
Ribbon是Spring Cloud中的一个客户端负载均衡组件,它与Eureka结合使用可以实现服务间的负载均衡。以下是Ribbon的用法:
Ribbon的作用和特点:
Ribbon的配置方式:
# application.yml
my-service:
ribbon:
eureka:
enabled: true # 启用Eureka集成
listOfServers: localhost:8081,localhost:8082 # 服务实例列表
# 可选:设置负载均衡策略
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule 自定义Ribbon配置:
Feign是Spring Cloud中的一个声明式的REST客户端,它简化了服务间的HTTP通信,并与Eureka集成实现了服务发现和负载均衡。以下是Feign的用法:
Feign的作用和特点:
声明式REST客户端的使用:
Feign的请求参数和路径变量处理:
Feign的错误处理和重试机制:
在Maven项目中,需要添加Spring Cloud Feign的依赖:
org.springframework.cloud spring-cloud-starter-openfeign
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "service-provider")
public interface UserServiceClient {
@GetMapping("/users")
String getUsers();
}
在上述代码中,使用@FeignClient注解指定了服务提供者的名称,这里是service-provider。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableFeignClients
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
@RestController
class UserController {
private final UserServiceClient userServiceClient;
public UserController(UserServiceClient userServiceClient) {
this.userServiceClient = userServiceClient;
}
@GetMapping("/users")
public String getUsers() {
return userServiceClient.getUsers();
}
}
在上述代码中,使用@EnableFeignClients注解启用Feign客户端,并通过自动注入的方式使用UserServiceClient进行服务调用。
在application.properties或application.yml文件中,配置Feign客户端的相关属性,例如:
# Feign客户端的服务地址 service-provider.ribbon.listOfServers=http://localhost:8080
Feign会根据注解配置自动生成相应的HTTP请求,并与Eureka集成实现服务发现和负载均衡的功能。