一、Eureka注册中心
Eureka是Spring Cloud中的一个服务注册和发现框架,通常用于实现微服务中的服务治理。Eureka Server作为服务注册中心,负责管理各个服务实例的状态信息,Eureka Client则作为服务提供者和服务消费者向Eureka Server注册/查询服务信息。
在创建Eureka Server时,我们需要添加spring-cloud-starter-netflix-eureka-server依赖,并在启动类上添加@EnableEurekaServer注解。
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
在创建Eureka Client时,我们需要添加spring-cloud-starter-netflix-eureka-client依赖,并在启动类上添加@EnableDiscoveryClient注解。同时,还需要在application.yml配置文件中指定服务名称和注册中心地址。
spring:
application:
name: service-a
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
二、Ribbon负载均衡
Ribbon是Spring Cloud中的一个客户端负载均衡器,能够实现服务提供者之间的负载均衡。当服务消费者向服务注册中心获取服务列表时,Ribbon会通过负载均衡算法选择一个服务实例,将请求发送到该实例上。
在创建Ribbon客户端时,我们需要添加spring-cloud-starter-loadbalancer依赖,并在RestTemplate中注入LoadBalancerClient,使用该客户端发起请求时,会经过负载均衡器选择一个服务实例。代码示例:
@RestController
public class TestController {
private final RestTemplate restTemplate;
private final LoadBalancerClient loadBalancerClient;
public TestController(RestTemplate restTemplate, LoadBalancerClient loadBalancerClient) {
this.restTemplate = restTemplate;
this.loadBalancerClient = loadBalancerClient;
}
@GetMapping("/test")
public String test() {
ServiceInstance instance = loadBalancerClient.choose("service-a");
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/hello";
return restTemplate.getForObject(url, String.class);
}
}
三、Feign调用
Feign是Spring Cloud中的一个声明式HTTP客户端,它使用注解方式定义接口,Spring Cloud会自动生成实现该接口的代理对象。在调用其他服务时,我们只需要定义一个接口,并添加@FeignClient注解即可。同时,Feign还支持负载均衡和服务发现功能。
在创建Feign客户端时,我们需要添加spring-cloud-starter-openfeign依赖,并在启动类上添加@EnableFeignClients注解。Feign还提供了@PathVariable、@RequestParam等注解,用于设置请求参数。代码示例:
@FeignClient("service-a")
public interface ServiceAFeignClient {
@GetMapping("/hello")
String hello();
}
@RestController
public class TestController {
private final ServiceAFeignClient serviceAFeignClient;
public TestController(ServiceAFeignClient serviceAFeignClient) {
this.serviceAFeignClient = serviceAFeignClient;
}
@GetMapping("/test")
public String test() {
return serviceAFeignClient.hello();
}
}
四、Hystrix熔断器
Hystrix是Spring Cloud中的一个熔断器,用于实现服务的降级和熔断。当服务出现故障或响应缓慢时,Hystrix会自动触发熔断逻辑,避免请求在服务间传递导致整个系统崩溃。
在创建使用Hystrix的服务时,我们需要添加spring-cloud-starter-netflix-hystrix依赖,并在接口中添加@HystrixCommand注解定义服务降级逻辑。代码示例:
@RestController
@DefaultProperties(defaultFallback = "fallback")
public class TestController {
private final RestTemplate restTemplate;
public TestController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/test")
@HystrixCommand
public String test() {
return restTemplate.getForObject("http://service-a/hello", String.class);
}
private String fallback() {
return "fallback";
}
}
五、Gateway网关
Gateway是Spring Cloud中的一个网关组件,用于实现服务的路由和过滤。通过Gateway,我们能够实现动态路由、请求重试、过滤请求等功能,使服务提供者更加灵活和智能。
在创建Gateway网关时,我们需要添加spring-cloud-starter-gateway依赖,并在配置文件中定义路由规则。Gateway的路由规则定义采用yaml格式,代码示例:
spring:
cloud:
gateway:
routes:
- id: service-a
uri: http://localhost:8080
predicates:
- Path=/service-a/**
- id: service-b
uri: http://localhost:8081
predicates:
- Path=/service-b/**
其中id为路由规则唯一标识符,uri为目标服务地址,predicates为匹配规则。
六、Config配置中心
Config是Spring Cloud中的一个配置中心,用于集中管理各个服务的配置信息。在Config中,我们能够实现配置文件的动态刷新、加密和解密等功能,使系统更加安全和易于维护。
在创建Config服务时,我们需要添加spring-cloud-config-server依赖,并在启动类上添加@EnableConfigServer注解。同时,还需要在启动类的配置文件中指定Git仓库地址和配置文件路径。代码示例:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
search-paths: '{profile}'
七、Bus消息总线
Bus是Spring Cloud中的一个消息总线,用于实现Config配置文件的动态刷新。在使用Bus时,我们需要向Config服务注册上Bus,并在需要刷新配置的服务中触发/actuator/bus-refresh接口,此时Config将发送消息通知其他使用该配置文件的服务进行配置更新。
在使用Bus时,我们需要添加spring-cloud-starter-bus-amqp依赖,并在配置文件中指定RabbitMQ的连接信息和交换机。同时,在每个服务的配置文件中添加management.endpoints.web.exposure.include属性,开启/actuator/bus-refresh接口。代码示例:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
bus:
enabled: true
stream:
rabbit:
bindings:
input:
destination: config
group: service-a
bindings:
output:
destination: config
group: service-a
bindings:
output:
destination: config
group: service-b
management:
endpoints:
web:
exposure:
include: bus-refresh
八、Sleuth链路追踪
Sleuth是Spring Cloud中的一个链路追踪组件,通过在服务调用过程中添加TraceId和SpanId等信息,实现对服务调用全链路监控和性能调优。
在使用Sleuth时,我们只需要添加spring-cloud-starter-sleuth依赖,Spring Cloud会自动为服务添加Trace信息。代码示例:
@RestController
public class TestController {
private final RestTemplate restTemplate;
public TestController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/test")
public String test() {
return restTemplate.getForObject("http://service-a/hello", String.class);
}
}
总结
Spring Cloud是一个非常强大的微服务框架,其中提供了丰富的组件和工具,使得服务治理、负载均衡、熔断降级、路由过滤、配置管理等功能更加便捷、灵活和智能。在使用Spring Cloud时,我们需要根据业务需求灵活选择组件,同时结合各个组件的特点和优势,以实现更加优秀的微服务架构。
原创文章,作者:UOQRB,如若转载,请注明出处:https://www.506064.com/n/332984.html
微信扫一扫
支付宝扫一扫