SpringCloud搭建分布式服务架构(通俗易懂,步骤清晰)(转载)

问题引入:什么是SpringCloud?(在了解这个之前需要有微服务的概念)

基于springBoot的一套实现微服务的框架,提供了微服务所需的配置管理,基于Http协议的restful风格(返回异步数据)

SpringCould组件架构图

在这里插入图片描述

由于在一台电脑上演示分布式项目,需要创建多个项目模块

步骤:

创建父类工程maven工程,修改pom文件,将打包方式改为pom
创建服务工程(注册中心)SpringBoot模块,选择需要导入的包,具体见下截图
在这里插入图片描述

在这里插入图片描述

在SpringBoot文件中添加eureka服务配置
#设置服务器端口
server.port:9000
#设置应用程序名称 名字不许用下划线,不支持服务调用
spring.application.name=sc-demo-server
#主机地址
eureka.instance.hostname=localhost
#表示不向注册中心注册
eureka.client.register-with-eureka=false
#表示不向注册中心调用服务
eureka.client.fetch-registry=false
#服务的地址
eureka.client.service-url.defaultZone=http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka

1
2
3
4
5
6
7
8
9
10
11
12
13
在这个模块的启动类上添加eureka注解开启服务
@SpringBootApplication
@EnableEurekaServer //启动eureka,开启服务
public class ScDemoServerApplication {
public static void main(String[] args) {
SpringApplication.run(ScDemoServerApplication.class, args);
}
}

1
2
3
4
5
6
7
8
这时候运行你的启动类,在浏览器输入

http://localhost:自己设置的端口号/ 看到这个,说明eureka服务配置成功

在这里插入图片描述

接下来配置提供者与服务者

创建eureka的服务提供者 SpringBoot工程,具体见图(注意组件的区别)
[

在这里插入图片描述

这个模块的启动类上添加eureka注解开启服注意注解的使用
@SpringBootApplication
@EnableEurekaClient
public class ScDemoProviderApplication {

public static void main(String[] args) {
    SpringApplication.run(ScDemoProviderApplication.class, args);
}

}

1
2
3
4
5
6
7
8
9
10
SpringBoot的配置文件中发布服务配置
#服务器端口
server.port=9001
#配置发布服务地址
spring.application.name=sc-demo-provider
eureka.client.service-url.defaultZone=http://localhost:9000/eureka
1
2
3
4
5
测试eureka服务者

在模块中创建一个包写一个实体类

public class Student {
private Integer xh;
private String name;
private String sex;
private Integer age;
setter和getter 构造
}

1
2
3
4
5
6
7
8
编写控制器

@RestController
public class TestController {
//接收请求(服务) 返回json
@RequestMapping("/getData")
public Student getData(){
//返回一个学生 --调数据库
return new Student(101,“张三”,“人妖”,21);
}
}

1
2
3
4
5
6
7
8
9
10
11
在浏览器输入http://localhost:自己设置的服务提供者端口号/控制器请求名

页面会返回学生的json数据则成功

创建eureka的服务消费者 SpringBoot工程

创建流程与提供者相同

消费者的配置文件如下:

#服务器
server.port=9002
#服务的名称
spring.application.name=eureka-consumer
#指定注册中心:eureka服务器
eureka.client.service-url.defaultZone=http://localhost:9000/eureka
1
2
3
4
5
6
在启动类上开启eureka

package com.sc.dome;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@SpringBootApplication
public class ScConsumerApplication {

public static void main(String[] args) {
    SpringApplication.run(ScConsumerApplication.class, args);
}

@Bean   //交给spring容器管理
@LoadBalanced  // 支持使用服务名称发现服务进行调用,且支持负载
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
开发控制器调用服务

package com.sc.dome.controller;

import com.sc.dome.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**

  • @Auther: Mr.zhou
  • @Date: 2020/8/13 13:44
    */
    @RestController
    public class StudentController {
    @Autowired
    private RestTemplate restTemplate; // 从spring容器自动注入RestTemplate对象
    @RequestMapping(“showStudent”)
    public Student showStudent(){
    Student stu = restTemplate.getForObject(“http://SC-PROVIDER/getStudent”, Student.class);
    return stu;
    }
    }

温馨提示:最后测试的时候需要开启三个启动类哦

原文链接:https://blog.csdn.net/qq_41530601/article/details/107983860?utm_medium=distribute.pc_feed.none-task-blog-personrec_tag-8.nonecase&depth_1-utm_source=distribute.pc_feed.none-task-blog-personrec_tag-8.nonecase&request_id=5f35aebbb51ffe58eedc11e2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一套用于构建分布式系统的工具集,它提供了一系列的组件和库,可以帮助开发者快速搭建分布式架构。下面是基于Spring Cloud搭建分布式架构步骤: 1. 注册中心:Spring Cloud提供了服务注册与发现的功能,可以使用Eureka、Consul、Zookeeper等作为注册中心。通过将所有微服务都注册到注册中心,其他微服务就可以通过注册中心来发现和调用。 2. 服务提供者与消费者:通过Spring Cloud的负载均衡和服务调用功能,我们可以轻松实现服务提供者和消费者的通信。服务提供者将自己的服务注册到注册中心后,消费者可以通过调用注册中心的接口来获取服务列表,并通过负载均衡策略选择一个提供者进行调用。 3. 断路器和降级:为了保证系统的稳定性,Spring Cloud提供了断路器和降级的支持。当某个服务不可用或超时时,断路器可以自动切断对该服务的调用,并返回预先设定的默认值或执行降级逻辑,避免级联故障。 4. 配置中心:Spring Cloud提供了分布式配置中心,可以将配置文件集中管理,并在所有微服务中进行统一的快速更新和下发。通过配置中心,可以实现对不同环境的配置分离和动态更新。 5. 网关和路由:Spring Cloud Gateway和Zuul都是Spring Cloud提供的网关组件,可以用于统一管理和转发微服务的请求。通过配置路由规则,可以实现请求的转发、限流、鉴权等功能,提供更加灵活和安全的访问控制。 通过以上步骤,我们可以基于Spring Cloud搭建一个完整的分布式架构。它帮助我们简化了分布式系统的开发和部署,提供了许多功能模块,包括服务注册与发现、服务调用、断路器和降级、配置中心、网关和路由等。这使得我们可以更加方便地构建和管理分布式系统,提高开发效率和系统的可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值