# cloud-study
**Repository Path**: linestyle007/cloud-study
## Basic Information
- **Project Name**: cloud-study
- **Description**: cloud-study
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2021-07-11
- **Last Updated**: 2021-11-02
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# cloud-study
Springcloud-study
## 准备工作
### Config配置中心
* cloud-study-config
#### application启动类
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
```
#### application.yml
```yaml
server:
port: 5000
#注册到Eureka服务中⼼
eureka:
client:
service-url:
# 注册到集群,就把多个Eurekaserver地址使⽤逗号连接起来即可;注册到单实例(⾮集群模式),那就写⼀个就ok
defaultZone: http://127.0.0.1:8761/eureka,http://127.0.0.1:8762/eureka
instance:
prefer-ip-address: true #服务实例中显示ip,⽽不是显示主机名(兼容⽼的eureka版本)
# 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以⾃定义它
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: cloud-study-config
cloud:
config:
server:
git:
#配置git服务地址
uri: https://gitee.com/linestyle007/cloud-study-config-repo.git
#配置git⽤户名
username:
#配置git密码
password:
search-paths:
- cloud-study-config-repo
# 读取分⽀
label: master
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
#针对的被调⽤⽅微服务名称,不加就是全局⽣效
#lagou-service-resume:
# ribbon:
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整
# springboot中暴露健康检查等断点接⼝
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接⼝的细节
endpoint:
health:
show-details: always
```
#### pom.xml
```xml
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-bus-amqp
```
### Gateway网关服务
* cloud-study-gateway
#### application启动类
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
```
#### bootstrap.yml
```yaml
server:
port: 9000
eureka:
client:
serviceUrl: # eureka server的路径
defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/
#把 eureka 集群中的所有 url 都填写了进来,也可以只写⼀台,因为各个 eureka server 可以同步注册表
instance:
#使⽤ip注册,否则会使⽤主机名注册了(此处考虑到对⽼版本的兼容,新版本经过实验都是ip)
prefer-ip-address: true
#⾃定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早 期版本是ipAddress
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
# 上⾯这段配置的意思是,配置了⼀个 id 为 service-autodeliver-router 的路由规
# 则,当向⽹关发起请求 http://localhost:9002/autodeliver/checkAndBegin/15
# 45132,请求会被分发路由到对应的微服务上
# 5.3 GateWay路由规则详解
# Spring Cloud GateWay 帮我们内置了很多 Predicates功能,实现了各种路由匹配规
# 则(通过 Header、请求参数等作为条件)匹配到对应的路由。
name: cloud-study-gateway
# 配置中心获取数据库连接
cloud:
config:
name: cloud-study-gateway
profile: dev
label: master
uri: http://127.0.0.1:5000
gateway:
routes: # 路由可以有多个
- id: service-user-router # 我们⾃定义的路由 ID,保持唯⼀
# uri: http://127.0.0.1:8100 # ⽬标服务地址 ⾃动投递微服务(部署多实例) 动态路由:uri配置的应该是⼀个服务名称,⽽不应该是⼀个具体的 服务实例的地址
uri: lb://ghc-service-user # ⽬标服务地址 ⾃动投递微服务(部署多实例) 动态路由:uri配置的应该是⼀个服务名称,⽽不应该是⼀个具体的 服务实例的地址
# gateway⽹关从服务注册中⼼获取实例信息然后负载后路由
predicates: #断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默 认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
- Path=/api/user/**
filters:
- StripPrefix=1
- id: service-code-router # 我们⾃定义的路由 ID,保持唯⼀
# uri: http://127.0.0.1:8000 # ⽬标服务地址
uri: lb://ghc-service-code # ⽬标服务地址
#http://localhost:9002/resume/openstate/1545132
#http://127.0.0.1:8081/openstate/1545132
predicates: #断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默 认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
- Path=/api/code/**
filters:
- StripPrefix=1
- id: service-email-router # 我们⾃定义的路由 ID,保持唯⼀
# uri: http://127.0.0.1:8000 # ⽬标服务地址
uri: lb://ghc-service-email # ⽬标服务地址
#http://localhost:9002/resume/openstate/1545132
#http://127.0.0.1:8081/openstate/1545132
predicates: #断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默 认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
- Path=/api/email/**
filters:
- StripPrefix=1
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
#针对的被调⽤⽅微服务名称,不加就是全局⽣效
ghc-service-user:
ribbon:
#请求连接超时时间
ConnectTimeout: 5000
#请求处理超时时间
ReadTimeout: 5000
#对所有操作都进⾏重试
OkToRetryOnAllOperations: false
####根据如上配置,当访问到故障请求的时候,它会再尝试访问⼀次当前实例(次数 由MaxAutoRetries配置),
####如果不⾏,就换⼀个实例进⾏访问,如果还不⾏,再换⼀次实例访问(更换次数 由MaxAutoRetriesNextServer配置),
####如果依然不⾏,返回失败信息。
MaxAutoRetries: 0 #对当前选中实例重试次数,不包括第⼀次调⽤
MaxAutoRetriesNextServer: 0 #切换实例的重试次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整
# 开启Feign的熔断功能
feign:
hystrix:
enabled: true
# compression:
# request:
# enabled: true # 开启请求压缩
# mime-types: text/html,application/xml,application/json # 设置压缩的数据类型,此处也是默认值
# min-request-size: 2048 # 设置触发压缩的⼤⼩下限,此处也是默认值
# response:
# enabled: true # 开启响应压缩
logging:
level:
# Feign日志只会对日志级别为debug的做出响应
com.lagou.edu.controller.service.ResumeServiceFeignClient: debug
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 15000
```
#### pom.xml
```xml
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
org.springframework.cloud
spring-cloud-commons
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.boot
spring-boot-starter-webflux
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
1.18.4
provided
com.sun.xml.bind
jaxb-core
2.2.11
javax.xml.bind
jaxb-api
com.sun.xml.bind
jaxb-impl
2.2.11
org.glassfish.jaxb
jaxb-runtime
2.2.10-b140310.1920
javax.activation
activation
1.1.1
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
true
com.github.vladimir-bukhtoyarov
bucket4j-core
4.0.0
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-config-client
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.cloud
spring-cloud-dependencies
Greenwich.RELEASE
pom
import
org.apache.maven.plugins
maven-compiler-plugin
11
11
utf-8
org.springframework.boot
spring-boot-maven-plugin
```
### Eureka注册中心服务
* cloud-study-eureka
#### application启动类
```java
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
```
#### application.yml
```yaml
#指定应⽤名称
spring:
application:
name: ghc-eureka
server:
port: 8761
eureka:
instance:
hostname: 8761
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://127.0.0.1:8762/eureka
dashboard:
enabled: true
```
#### pom.xml
```xml
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
```
### user服务
* cloud-study-user
#### application启动类
```java
@SpringBootApplication
@EntityScan("org.example.entity")
@EnableDiscoveryClient
@EnableFeignClients
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
```
#### bootstrap.yml
```yaml
server:
port: 8080
# servlet:
# context-path: /user
#注册到Eureka服务中心
eureka:
client:
serviceUrl:
# 注册到集群, 就把多个server使用,连接
defaultZone: http://127.0.0.1:8761/eureka,http://127.0.0.1:8762/eureka
instance:
prefer-ip-address: true #显示ip而不是主机名
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: ghc-service-user
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
#避免将驼峰命名转换为下划线命名
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# 配置中心获取数据库连接
cloud:
config:
name: ghc-service-code
profile: dev
label: master
uri: http://127.0.0.1:5000
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# 也可以暴露所有的端⼝
management:
endpoints:
web:
exposure:
include: "*"
#针对的被调⽤⽅微服务名称,不加就是全局⽣效
ghc-service-code:
ribbon:
#请求连接超时时间
ConnectTimeout: 5000
#请求处理超时时间
ReadTimeout: 5000
#对所有操作都进⾏重试
OkToRetryOnAllOperations: true
####根据如上配置,当访问到故障请求的时候,它会再尝试访问⼀次当前实例(次数 由MaxAutoRetries配置),
####如果不⾏,就换⼀个实例进⾏访问,如果还不⾏,再换⼀次实例访问(更换次数 由MaxAutoRetriesNextServer配置),
####如果依然不⾏,返回失败信息。
MaxAutoRetries: 0 #对当前选中实例重试次数,不包括第⼀次调⽤
MaxAutoRetriesNextServer: 0 #切换实例的重试次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整
# 开启Feign的熔断功能
feign:
hystrix:
enabled: true
# compression:
# request:
# enabled: true # 开启请求压缩
# mime-types: text/html,application/xml,application/json # 设置压缩的数据类型,此处也是默认值
# min-request-size: 2048 # 设置触发压缩的⼤⼩下限,此处也是默认值
# response:
# enabled: true # 开启响应压缩
logging:
level:
# Feign日志只会对日志级别为debug的做出响应
com.lagou.edu.controller.service.ResumeServiceFeignClient: debug
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 15000
```
#### pom.xml
```xml
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-config-client
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.cloud
spring-cloud-starter-openfeign
```
### code服务
* cloud-study-code
#### application启动类
```java
@SpringBootApplication
@EnableDiscoveryClient
@EntityScan("org.example.entity")
@EnableFeignClients
public class CodeApplication {
public static void main(String[] args) {
SpringApplication.run(CodeApplication.class, args);
}
}
```
#### bootstrap.yml
```yaml
server:
port: 8081
# servlet:
# context-path: /code
#注册到Eureka服务中心
eureka:
client:
serviceUrl:
# 注册到集群, 就把多个server使用,连接
defaultZone: http://127.0.0.1:8761/eureka,http://127.0.0.1:8762/eureka
instance:
prefer-ip-address: true #显示ip而不是主机名
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: ghc-service-code
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
#避免将驼峰命名转换为下划线命名
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# 配置中心获取数据库连接
cloud:
config:
name: cloud-study-code
profile: dev
label: master
uri: http://127.0.0.1:5000
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# 也可以暴露所有的端⼝
management:
endpoints:
web:
exposure:
include: "*"
#针对的被调⽤⽅微服务名称,不加就是全局⽣效
ghc-service-email:
ribbon:
#请求连接超时时间
ConnectTimeout: 5000
#请求处理超时时间
ReadTimeout: 5000
#对所有操作都进⾏重试
OkToRetryOnAllOperations: false
####根据如上配置,当访问到故障请求的时候,它会再尝试访问⼀次当前实例(次数 由MaxAutoRetries配置),
####如果不⾏,就换⼀个实例进⾏访问,如果还不⾏,再换⼀次实例访问(更换次数 由MaxAutoRetriesNextServer配置),
####如果依然不⾏,返回失败信息。
MaxAutoRetries: 0 #对当前选中实例重试次数,不包括第⼀次调⽤
MaxAutoRetriesNextServer: 0 #切换实例的重试次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整
# 开启Feign的熔断功能
feign:
hystrix:
enabled: true
# compression:
# request:
# enabled: true # 开启请求压缩
# mime-types: text/html,application/xml,application/json # 设置压缩的数据类型,此处也是默认值
# min-request-size: 2048 # 设置触发压缩的⼤⼩下限,此处也是默认值
# response:
# enabled: true # 开启响应压缩
logging:
level:
# Feign日志只会对日志级别为debug的做出响应
com.lagou.edu.controller.service.ResumeServiceFeignClient: debug
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 15000
```
#### pom.xml
```xml
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-config-client
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.cloud
spring-cloud-starter-openfeign
```
### email服务
* cloud-study-mail
#### application启动类
```java
@SpringBootApplication
@EnableDiscoveryClient
public class EmailApplication {
public static void main(String[] args) {
SpringApplication.run(EmailApplication.class, args);
}
}
```
#### bootstrap.yml
```yaml
server:
port: 8082
# servlet:
# context-path: /email
#注册到Eureka服务中心
eureka:
client:
serviceUrl:
# 注册到集群, 就把多个server使用,连接
defaultZone: http://127.0.0.1:8761/eureka,http://127.0.0.1:8762/eureka
instance:
prefer-ip-address: true #显示ip而不是主机名
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: cloud-study-email
# 配置中心获取数据库连接
cloud:
config:
name: ghc-service-email
profile: dev
label: master
uri: http://127.0.0.1:5000
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# 也可以暴露所有的端⼝
management:
endpoints:
web:
exposure:
include: "*"
```
#### pom.xml
```xml
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-config-client
org.springframework.cloud
spring-cloud-starter-bus-amqp
javax.mail
mail
1.4.7
org.springframework.boot
spring-boot-configuration-processor
true
```
### rabbitMQ依赖
```scala
# 依赖:openssl@1.1, jpeg, libpng, libtiff, wxmac and erlang
$ brew install jpeg
$ brew install libpng
$ brew install libtiff
$ brew install wxmac
$ brew install erlang
```
## 实现步骤
### 启动服务
* eureka
* config
* gateway
* user
* code
* Email
### 测试
* 注册
* 登录
* 未登录
```scala
网关:http://www.abc.com
# 注册接口
/user/register/{email}/{password}/{code}
http://www.test.com/api/user/register/"+email+"/"+password+"/"+code
http://www.test.com:9000/code/create/376817156@qq.com
http://127.0.0.1:9000/api/code/create/376817156@qq.com
http://www.test.com:9000/api/code/create/abc@qq.com
# 登录接口
# 未登录测试接口
http://www.test.com:8000/api/email/{email}/{code},
```