SpringCloud Netflix—微服务架构

一、SpringCloud简介

Spring Cloud 是⼀个基于SpringBoot实现的微服务架构应用开发框架,它为我们进行微服务架构应用开发提
供了服务注册与发现、熔断器、网关路由、配置管理、负载均衡、消息总线、数据监控等⼀系列工具。

在这里插入图片描述
Spring Cloud比较成熟的两个体系:

  • Spring Cloud Netflix
  • Spring Cloud Alibaba

Spring Cloud核心组件

  • Spring Cloud Netflix
    • Eureka 服务注册与发现中心,用于服务治理
    • Feign 服务访问组件(对Ribbon和HyStrix的封装)
    • Ribbon 服务访问组件、进行服务调用,实现了负载均衡(停更了)
    • Hystrix 熔断器,服务容错管理
    • zuul 网关组件
  • Spring Cloud Config 配置管理的组件—分布式配置中心
  • Spring Cloud Bus 消息总线
  • Spring Cloud Consul 服务注册与发现中心(功能类似eureka)
    在这里插入图片描述

二、Eureka—服务注册与发现中心

Eureka—服务注册与发现中心:服务提供者将服务注册到这里,服务消费者在这里调用服务。
Eureka可靠性—可以实现Eureka集群,防止单个节点挂掉,Eureka集群搭建。
Eureka安全性—Spring Security安全框架,设置帐号和密码来限制服务的注册及发现。

服务提供者:以一个标准来划分的service服务。例如以对数据库进行一次操作就划分为一个服务。(是一个完整的springboot模块,可以理解为将单体项目中dao层对数据库的一个增删改查操作提取出来作为一个单独的springboot模块,整个项目只有这一个业务供别人调用)
服务消费者:提供面向前端的api接口,供前端访问(也是一个完整的springboot模块,调用一个或多个服务提供者模块来完成一整个大的操作业务功能,可以理解为单体项目中的controller中的一个业务)

2.1 添加依赖

太新的版本可能不好使 (下面的都好使)
springboot 2.5.9 2.4.4
spring-cloud 2021.0.0 2020.0.5

  • spring web
  • eureka server
<dependency>
	 <groupId>org.springframework.cloud</groupId>
	 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.2 配置服务注册与发现中心

## 设置服务注册与发现中⼼的端⼝
server:
 port: 8761
## 在微服务架构中,服务注册中⼼是通过服务应⽤的名称来区分每个服务的
## 我们在创建每个服务之后,指定当前服务的 应⽤名/项⽬名
spring:
 application:
 name: service-eureka
eureka:
 client:
 ## 192.168.54.59这个ip如果是集群就填写要注册到别的eureka的ip地址,
 ## 否则默认ip是本地localhost,端口号是8761
 service-url:
 defaultZone: http://192.168.54.59:8761/eureka
 ## 设置服务注册与发现中⼼是否为为集群搭建(如果为集群模式,多个eureka节点之间需要相互注册)
 register-with-eureka: false
 ## 设置服务注册与发现中是否作为服务进⾏注册
 fetch-registry: false

2.3 配置启动类

  • @EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class ServiceEurekaApplication {
   
 public static void main(String[] args) {
   
 SpringApplication.run(ServiceEurekaApplication.class, args);
 }
}

2.4 运行及访问

三、Spring Security—安全框架

当完成Eureka的搭建之后,只要知道ip和port就可以随意的注册服务、调用服务,这是不安全的,我们可以
通过设置帐号和密码来限制服务的注册及发现。
在eureka中整合Spring Security安全框架实现帐号和密码验证

3.1 添加的依赖

<dependency>
	 <groupId>org.springframework.boot</groupId>
	 <artifactId>spring-boot-starter-security</artifactId>
</dependency>

3.2 配置文件

##  设置访问eureka的帐号和密码
spring:
 security:
 	user:
	 	name: zhangsan
	 	password: 123456

3.3 添加配置类

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   

	 @Override
	 protected void configure(HttpSecurity http) throws Exception {
   
	 http.csrf().disable();
	 //设置当前服务器的所有请求都要使⽤spring security的认证
	 http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
	 }
}

3.4 服务提供者和服务消费者连接到注册中心都要帐号和密码

## 服务提供者和服务消费者的eureka yml配置都要加账号密码才能访问到服务与注册中心
eureka:
 client:
 	service-url
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值