spring cloud

Posted Prince_Chang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud相关的知识,希望对你有一定的参考价值。

spring cloud简介

Spring Cloud是一个分布式框架,Spring Cloud是建立在Spring Boot上面的。


 

spring cloud组件

Eureka   注册中心 [ju?ri?k?]
Feign     调用 [fe?n]
Hystrix   容错 [h?st‘r?ks]
Ribbon   负载远射
Zuul     网关
Config    
Sleuth


 

创建Eureka注册中心

1、IDEA:new-->project-->Spring Initializr-->输入项目名-->Spring Cloud Discovery-->Eureka Server
Discovery [d??sk?v?ri] 发现
2、在启动类添加注解@EnableEurekaServer
3、配置application.properties

技术图片
# 自己的端口号
server.port=9990
# 自己是注册中心,发布自己的地址,让别人找到自己
# 集群配置,http://127.0.0.1:9991/eureka,http://127.0.0.1:9992/eureka
eureka.client.service-url.defaultZone=http://127.0.0.1:9990/eureka/
# 是否把当前项目注册到注册中心,注册中心客户端项目设置为true,此项目是注册中心服务端
eureka.client.register-with-eureka=false
# 默认是true,注册中心集群同步数据,这里使用单机方式,所以关闭
eureka.client.fetch-registry=false
# 安全配置
spring.security.user.name=root
spring.security.user.password=root
View Code

 

4、springboot引入spring-boot-starter-security做安全校验后,自动开启CSRF安全认证,任何一次服务请求默认都需要CSRF 的token,而Eureka-client不会生成该token,故启动时会报如上错误。
在启动类里添加如下内部类代码:

技术图片
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}
View Code

 

5、启动,测试
这个地址 http://127.0.0.1:9990/eureka 是给注册中心客户端使用的
浏览器直接访问http://127.0.0.1:9990


 

创建服务提供者

1、IDEA:new-->project-->Spring Initializr-->输入项目名-->Spring Cloud Discovery-->Eureka Discovery Client
2、在启动类添加注解@EnableEurekaClient
3、配置application.properties

技术图片
# 自己的端口
server.port=8800
# 把自己注册到注册中心后,别人用这个名字引用我
spring.application.name=user-provider
# 注册到哪里,即注册中心地址
eureka.client.service-url.defaultZone=http://root:root@127.0.0.1:9990/eureka/
logging.level.root=trace
View Code

 

4、启动,测试
浏览器访问http://127.0.0.1:9990以查看服务提供者


 

创建 config server
1、IDEA:new-->project-->Spring Initializr-->输入项目名-->Spring Cloud Config-->Config Server
2、在启动类添加注解@EnableEurekaClient
3、配置application.yml

技术图片
# 自己的端口
server.port=8800
# 把自己注册到注册中心后,别人用这个名字引用我
spring.application.name=user-provider
# 注册到哪里,即注册中心地址
eureka.client.service-url.defaultZone=http://root:root@127.0.0.1:9990/eureka/
logging.level.root=trace
View Code

 

4、启动,测试
浏览器访问http://127.0.0.1:9990以查看服务提供者

 

 

 

 

 

 

 

 

 

使用Feign实现声明式REST调用

以上是关于spring cloud的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Keycloak 保护 Angular 8 前端和使用网关、eureka 的 Java Spring Cloud 微服务后端

spring cloud - particle云架构代码结构

Spring Cloud微服务架构代码结构详细讲解

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)

spring cloud云服务架构 - particle云架构代码结构详细讲解

spring cloud云服务架构 - particle云架构代码结构详细讲解