Springboot系列之十九:整合springcloud

本文介绍了SpringCloud作为微服务架构的整体解决方案,涵盖了服务发现、负载均衡、断路器、服务网关和分布式配置等核心组件。通过创建Provider和Consumer,结合Eureka注册中心和Ribbon实现客户端负载均衡,展示了SpringCloud在分布式系统中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/qq_28764557/article/details/89423072

一、Spring Boot和Spring Cloud
Spring Cloud

Spring Cloud是一个分布式的整体解决方案。Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局琐,leader选举,分布式session,集群状态)中快速构建的工具,使用Spring Cloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。

SpringCloud分布式开发五大常用组件
  • 服务发现——Netflix Eureka
  • 客服端负载均衡——Netflix Ribbon
  • 断路器——Netflix Hystrix
  • 服务网关——Netflix Zuul
  • 分布式配置——Spring Cloud Config

在这里插入图片描述

二、Spring Cloud 入门

1、创建provider
2、创建consumer
3、引入Spring Cloud
4、引入Eureka注册中心
5、引入Ribbon进行客户端负载均衡


dubbo解决的是rpc。

springcloud是整体的解决方案。

Eureka:注册中心

Ribbon:负载均衡

Hystrix:断路器,A-B-C,中间出现问题用断路器,响应。

zuul:网管,过滤。

注册中心:

   

服务的提供者:

    

 消费者:

     

  启动注册中心:

         配置文件:

              


 
  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: eureka-server # eureka实例的主机名
  6. client:
  7. register-with-eureka: false #不把自己注册到eureka上
  8. fetch-registry: false #不从eureka上来获取服务的注册信息
  9. service-url:
  10. defaultZone: http: //localhost:8761/eureka/

  地址默认值:

 

启动要加注解:


 
  1. package com.atguigu.eurekaserver;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * 注册中心
  7. * 1、配置Eureka信息
  8. * 2、@EnableEurekaServer
  9. */
  10. @EnableEurekaServer //加入这个注解
  11. @SpringBootApplication
  12. public class EurekaServerApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(EurekaServerApplication.class, args);
  15. }
  16. }

启动:

     访问:

34---------------------------------------------------------------------------------------

    

  提供者的功能。

  注册进来:


 
  1. server:
  2. port: 8001
  3. spring:
  4. application:
  5. name: provider-ticket
  6. eureka:
  7. instance:
  8. prefer-ip-address: true # 注册服务的时候使用服务的ip地址
  9. client:
  10. service-url:
  11. defaultZone: http: //localhost:8761/eureka/

  启动。

 

   注册中心有了应用。

   可以注册多个应用。

8001 8002

  

        

      

35---------------------------------------------------------------------------------------

    创建消费者:

         


 
  1. spring:
  2. application:
  3. name: consumer-user
  4. server:
  5. port: 8200
  6. eureka:
  7. instance:
  8. prefer-ip-address: true # 注册服务的时候使用服务的ip地址
  9. client:
  10. service-url:
  11. defaultZone: http: //localhost:8761/eureka/

  发现服务的注解:

         


 
  1. package com.atguigu.consumeruser;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.client.RestTemplate;
  8. @EnableDiscoveryClient //开启发现服务功能
  9. @SpringBootApplication
  10. public class ConsumerUserApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(ConsumerUserApplication.class, args);
  13. }
  14. @LoadBalanced //使用负载均衡机制
  15. @Bean
  16. public RestTemplate restTemplate(){
  17. return new RestTemplate();
  18. }
  19. }

   


 
  1. package com.atguigu.consumeruser;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.client.RestTemplate;
  8. @EnableDiscoveryClient //开启发现服务功能
  9. @SpringBootApplication
  10. public class ConsumerUserApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(ConsumerUserApplication.class, args);
  13. }
  14. @LoadBalanced //使用负载均衡机制
  15. @Bean
  16. public RestTemplate restTemplate(){ //帮我们发送http请求的。
  17. return new RestTemplate();
  18. }
  19. }

消费服务:


 
  1. package com.atguigu.consumeruser.controller;
  2. import org.springframework.beans.factory. annotation.Autowired;
  3. import org.springframework.web.bind. annotation.GetMapping;
  4. import org.springframework.web.bind. annotation.RestController;
  5. import org.springframework.web.client.RestTemplate;
  6. @RestController
  7. public class UserController {
  8. @Autowired
  9. RestTemplate restTemplate;
  10. @GetMapping("/buy")
  11. public String buyTicket(String name){
  12. String s = restTemplate.getForObject( "http://PROVIDER-TICKET/ticket", String. class);
  13. return name+ "购买了"+s;
  14. }
  15. }

36--------------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值