当前位置: 首页 > 工具软件 > tags cloud > 使用案例 >

springcloud配置详解

阙沛
2023-12-01

在这里插入代码片## 1. 版本信息,父项目,依赖管理
pom文件

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!‐‐ lookup parent from repository ‐‐>
</parent>
<properties>
<project.build.sourceEncoding>UTF‐8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF‐8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring‐cloud.version>Edgware.SR3</spring‐cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐dependencies</artifactId>
<version>${spring‐cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐maven‐plugin</artifactId>
</plugin>
</plugins>
</build>

2. eureka-注册中心

  1. pom添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐eureka‐server</artifactId>
</dependency>
  1. 启动类增加注解

@EnableEurekaServer

  1. 配置文件中增加application.yml
server:
port: 自定义
#eureka注册中心配置
eureka:
instance.hostname: localhost #一般为域名
client:
register‐with‐eureka: false
fetch‐registry: false
server:
enable‐self‐preservation: false
eviction‐interval‐timer‐in‐ms: 3000
  1. 启动Eureka,并访问 域名:端口号,如下

    http://localhost:8080

3.eureka-客户端-user

01 pom文件

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐eureka‐server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐feign</artifactId>
</dependency>

02 启动类注解增加

@EnableEurekaClient // 开启客户端注解
@EnableFeignClients // 开启服务之间调用 采用feign

03 在application.yml中增加

server:
port: xxx
spring:
application:
name: user‐service #服务名
#连接注册中心
eureka:
client:
service‐url:
defaultZone: http://localhost:8080/eureka/
instance:
prefer‐ip‐address: true #支持域名直接解析ip

04 新增api接口模块,增加接口

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>
/**
*
* @param str 简单数据类型必须增加@RequestParam注解
* @return
*/
@RequestMapping("sayHello")
String sayHello(@RequestParam("str") String str);
05 回到本项目中,pom依赖api接口模块,并实现接口
@RestController
@Service
public class UserServiceImpl implements UserService{
@Override
public String sayHello(String str) {
// throw new RuntimeException();
return "hello,user1,"+str;
}
}

4.服务调用(调用user服务为例)

01 在调用的项目中依赖api模块
02 pom中增加

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐eureka‐server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐feign</artifactId>
</dependency>

03 在启动类增加

@EnableEurekaClient
@EnableFeignClients

04 application配置文件中增加

server:
port: xxx
spring:
application:
name: xxxxxx
#连接注册中心
eureka:
client:
service‐url:
defaultZone: http://localhost:8080/eureka/
instance:
prefer‐ip‐address: true #支持域名直接解析ip

05 创建client包,新增UserServiceClient类

/**
* FeignClient中value的值为调用服务的配置文件中的spring.application.name的值
*/
@FeignClient(value = "user‐service")
public interface UserServiceClient extends UserService {
}

06 在controller中调用,注入Client

@Autowired
private UserServiceClient userService;

5. 网关配置zuul

01 在pom中新增

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐eureka</artifactId>
</dependency>

02 在bootstrap.yml配置文件中新增

server:
port: 6064
spring:
application:
name: dj‐zull # 配置中心的名称
#连接注册中心
eureka:
client:
service‐url:
defaultZone: http://localhost:6060/eureka/
instance:
prefer‐ip‐address: true #支持域名直接解析ip
zuul:
add‐host‐header: true
forceOriginalQueryStringEncoding: true
prefix: /api #访问前缀
host:
socket‐timeout‐millis: 100000
connect‐timeout‐millis: 100000
routes:
user‐route: #名称自定义,必须为xxx‐route
path: /user/** #访问路径
sensitive‐headers: "*"
serviceId: user‐service #user服务的名称
custom‐sensitive‐headers: true
order‐route:
path: /order/**
sensitive‐headers: "*"
serviceId: order‐service #order服务的名称
custom‐sensitive‐headers: true
#如果还有则在继续添加

03 在启动类中增加

@EnableZuulProxy
@EnableEurekaClient

6.熔断器hystrix

01 在需要使用熔断器的项目中的pom增加

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐hystrix</artifactId>
</dependency>

02 在配置文件中新增

#断路器配置
circuitBreaker:
# 请求总数下限
requestVolumeThreshold: 20
# 休眠时间窗
sleepWindowInMilliseconds: 3000
# 错误百分比下限
errorThresholdPercentage: 50
feign:
hystrix:
enabled: true

03 新增熔断类,xxxxFallBack,该类实现客户端接口,如

UserServiceClientFailBack
@Component
public class UserServiceClientFailBack implements UserServiceClient

04 在客户端接口中的@FeignClient注解中新增属性

@FeignClient(value = "user‐service",fallback = UserServiceClientFailBack.class)

7.统一配置中心(gitlab)

01 在git中新增项目,复制项目的路径

02 项目使用配置中心,需要将application.yml更改为
bootstrap.yml(两个文件可以共存,bootstrap里只需要配置注
册中心以及配置中心的配置即可,项目启动时bootstrap优先级
高)
03 pom文件中增加

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐config‐server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐eureka</artifactId>
</dependency>

04 在启动类中增加

@EnableConfigServer
@EnableEurekaClient

05 在配置文件中 bootstrap.yml 新增

server:
port: xxx
eureka:
client:
service‐url:
defaultZone: http://localhost:8080/eureka/
instance:
prefer‐ip‐address: true
spring:
application:
name: dj‐config # 配置中心的名称
cloud:
config:
server:
git:
uri: https://gitlab.com/herogosup/dj‐cloud‐config‐repo.git #gitlib地址
username: #gitlib的用户名 和 密码 如果是public 项目,则不需要写
password:
06 在调用统一配置中心的项目中调用配置,如user服务中调用统
06 在调用统一配置中心的项目中调用配置,如user服务中调用统
一配置中心,如调用配置中心的test-dev.yml
spring:
cloud:
config:
discovery:
enabled: true
serviceId: dj‐config #配置中心的名称
name: test #名称前缀
profile: dev #名称后缀

07 在调用统一配置中心的项目中pom文件增加

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐config</artifactId>
</dependency>

8.SpringCloud-pom需要内容

需要eureka,无论注册中心还是客户端

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐eureka‐server</artifactId>
</dependency>

需要Fegin,服务调用

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐feign</artifactId>
</dependency>

需要配置统一配置中心ConfigServer

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐config</artifactId>
</dependency>

需要zuul,路由

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐zuul</artifactId>
</dependency>

需要熔断器,hystrix

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐hystrix</artifactId>
</dependency>

9. SpringCloud配置需要的内容

服务名
spring:
application:
name: order‐service
注册中心
@EnableEurekaServer
#eureka注册中心配置
eureka:
instance.hostname: localhost #一般为域名
client:
register‐with‐eureka: false
fetch‐registry: false
server:
# 自我保护
enable‐self‐preservation: false
# 扫描失效服务间隔
eviction‐interval‐timer‐in‐ms: 3000
客户端
#连接注册中心
eureka:
client:
service‐url:
defaultZone: http://localhost:8080/eureka/
instance:
prefer‐ip‐address: true #支持域名直接解析ip

10.swagger

01 依赖信息

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox‐swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox‐swagger‐ui</artifactId>
<version>2.8.0</version>
</dependency>

02 配置swagger

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
//是否开启swagger,正式环境一般是需要关闭的,从配置文件中读取
@Value(value = "${swagger.enabled}")
Boolean swaggerEnabled;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
// 是否开启
.enable(swaggerEnabled).select()
// 扫描的路径包
.apis(RequestHandlerSelectors.basePackage("com.dj.cloud.web"))
// 指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any()).build().pathMapping("/");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swaggerdemo")
.description("test")
// 作者信息
.contact(new Contact("Liuyl", "https://www.baidu.cn",
"qqq@163.com"))
.version("1.0.0")
.build();
}
}
03 常用注解
// 配置在类上,针对于这个类的描述
@Api(tags = "订单api")
@ApiOperation:用在方法上,说明方法的作用
value: 表示接口名称
notes: 表示接口详细描述
@ApiImplicitParams:用在方法上包含一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
paramType:参数位置
header 对应注解:@RequestHeader
query 对应注解:@RequestParam
path 对应注解: @PathVariable
body 对应注解: @RequestBody
name:参数名
dataType:参数类型
required:参数是否必须传
value:参数的描述
defaultValue:参数的默认值
paramType:参数放在哪个地方
∙ header ‐‐> 请求参数的获取:@RequestHeader
∙ query ‐‐>请求参数的获取:@RequestParam
∙ path(用于restful接口)‐‐> 请求参数的获取:@PathVariable
∙ body(不常用)
∙ form(不常用

http://localhost:8081/swagger‐ui.html

11. 总结

springboot,springcloud都是微服务框架,Spring Boot 的核心思想
就是约定大于配置,Spring Boot 应用只需要很少的 Spring 配置,采用
Spring Boot 可以大大的简化你的开发模式,所有你想集成的常用框架,它都有
对应的组件支持。他默认支持模板的是html,数据需要通过thymeleaf来渲染。

在和mybatis整合时,sql语句有三种写法,常见的接口和xml,注解式(不
建议使用),注解+类,这三种方式,我们在项目中使用的一个mybatis-plus这
个插件,这个插件没有任何侵入,只需要在对应的层继承相关的东西就能完成单
表的CRUD。

Spring Cloud 是完全基于 Spring Boot 而开发,像springcloud就是跟
dubbo一样的都是实现分布式服务,模块主要分为eureka(注册中心)、熔断
器(hystrix)、配置中心(config)、路由(zuul),我们在项目中使用的模
块,每个模块都需要依赖注册中心,服务模块之间的调用都是通过feign实现
的。
还有一些就是在操作的时候需要注意的:使用fegin调用服务时请求方式默
认都是post,简单数据类型需要加@RequertParm注解才能接收到,fegin使用
@RepsonseBady默认返回的数据格式为xml,在PostMapping里添加属性即
可返回json。
注册中心没啥说的,模块都需要依赖他。
熔断器,其实就是在Feign中来配置的,主要是解决分布式系统交互时超时
处理和容错的类库,使用熔断器预防服务雪崩,我在项目配置的熔断器是20个
请求,错误百分比50%以上,3秒内如果还没有返回则进入到熔断器处理,在熔
断器中记录日志
配置中心就是配置一些公共的配置。其实除了配置中心以及注册中心的配
置在本地,其他几乎都在配置中心也就是gitlab上,但是要注意,如果使用配置
中心,一般情况我们会把配置文件的名称改为boostrap.yml,让项目启动时先
去加载
路由就是同一个端口可以调用他配置信息里不同模块的提供的url。如果没
有他,调用我的人就需要记录我每个服务器的url,很麻烦,这时路由的好处就
出现了,只需要访问我的路由,由我的路由去转发请求,请求我 不同的服务

常用注解:

@springbootapplication、@EnableEurekaServer、
@EnableEurekaClient、@EnableFeignClients、@EnableZuulProxy

swagger2: https://www.cnblogs.com/exmyth/p/7171857.html

 类似资料: