# cloud2020 **Repository Path**: JiShiMoWang_admin/cloud2020 ## Basic Information - **Project Name**: cloud2020 - **Description**: spring cloud - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-13 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 根据尚硅谷在 bilibili 的视频学习
在学习的时候根据实际流行和公司规范,不使用lombok 1.cloud-provider-payment8001 支付提供者 由于在consumer-order中调用了provider-payment接口,需要在controller中添加RequestBody, 会导致原先使用postman (POST)http://localhost:8001/payment/create?serial=尚硅谷001, 出现错误:"Required request body is missing" ,在测试时需要将serial 放到body中,以json的格式发送 { "serial":"尚硅谷002" } 代码重构:简单的提取了一些公用的实体类,在商用项目中,重构比这个更复杂。 在一些公司重构是不希望提取更多的公用代码的,如果更改一个微服务的业务,其他的都要更改; 也有的公司是提取公共部分很多,主要是微服务的业务很少有大的变动,这样提取的话, 可以尽可能的不去修改其他微服务的业务。主要是看公司和个人习惯吧 ,我这边是跟着视频学习,按照视频来 eureka server 集群 需要在 C:\Windows\System32\drivers\etc\hosts 中,添加两行 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 测试地址:http://eureka7001.com:7001/ ,http://eureka7002.com:7002/ windows 下查看consul版本 consul --version 启动consul :consul agent -dev ribbon 配置分配算法时,不能将IRule定义在 @ComponentScan 注解下,然后需要在启动类上加上 @RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class) 在使用 HystrixDashboard 的时候需要在被监控的微服务中需要加上下列代码 ```java /** * 此配置是为了服务监控而配置,与服务容错本身无关,SpringCloud 升级后的坑 * ServletRegistrationBean 因为SpringBoot的默认路径不是 /hystrix.stream, * 只要在自己项目里配置上下面的Servlet就可以了 * * @return */ @Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } ``` config 客户端配置时,是 spring.cloud.config.uri 不是 spring.cloud.config.url,在这个地方找了半天才发现 刷新config客户端配置 curl -X POST "http://localhost:3355/actuator/refresh" 总线 刷新时,需要配置rabbitmq,然后配置,修改git配置后,发送命令 curl -X POST "http://localhost:3344/actuator/bus-refresh",然后客户端就刷新配置了 指定刷新的话,需要在 curl -X POST "http://localhost:3344/actuator/bus-refresh/{destination}" 比如刷新 config-client的3355端口的服务; curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355" Stream 同一个组的多个微服务实例,每次只有一个会拿到 分组 解决重复消费和持久化 zipkin 下载地址:https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/ 启动方式:java -jar zipkin-server-2.12.9-exec.jar 前端页面: http://localhost:9411/zipkin/ nacos 安装 1.在 mysql 下新建数据库 nacos 2.找到nacos 下的config 文件下的nacos-mysql.sql 在nacos库中执行 3.找到conf 下的application.properties 配置 spring.datasource.platform=mysql ### Count of DB: db.num=1 ### Connect URL of DB: db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user=nacos db.password=nacos 4.找到bin文件下的 startup.cmd 将集群模式改为单机模式 set MODE="cluster" 改为 MODE="standalone" 设置好之后,启动 startup.cmd ,然后在浏览器输入 http://127.0.0.1:8848/nacos/#/login 登录,默认账号密码都是 nacos nacos 做配置中心时,@RestController+ @reFresh 中使用@Value 获取不到值,一直为空,需要在另外的文件中获取信息(nacos 1.4,nacos 1.1.4) 封装为 一个配置,在需要的时候注入 windows 下配置 nacos 集群,如果采用 nacos 1.1.4 1. 在 mysql 下新建数据库 nacos 2. 找到nacos 下的config 文件下的nacos-mysql.sql 在nacos库中执行 3.找到conf 下的application.properties 新增 spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user=nacos db.password=nacos 4.复制 cluster.conf.example 并改名为 cluster.conf,IP不能设置为127.0.0.1 192.168.43.31:3333 192.168.43.31:4444 192.168.43.31:5555 5.修改 startup.cmd 将 set MODE="standalone" 改为 set MODE="cluster" 6.将配置好的nacos,复制成三份,修改application.properties 中的 server.port ,分别改为 3333,4444,5555 ,即在第4步设置集群的配置的端口 7.修改nginx 1).修改nginx.conf 在server 上增加 upstream cluster{ server 127.0.0.1:3333; server 127.0.0.1:4444; server 127.0.0.1:5555; } 修改listen 端口 listen 1111; 修改 location / { #root html; #index index.html index.htm; proxy_pass http://cluster; } 8.启动三个 nacos和nginx,通过浏览器,访问 http://192.168.43.31:1111/nacos ,如果能进入到nacos登录页面就成功了 sentinel 启动方式 java -jar sentinel-dashboard-1.7.0.jar 登录地址:http://localhost:8080,账户密码:sentinel,sentinel sentinel 持久化到Nacos spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: # nacos 服务注册中心地址 server-addr: localhost:8848 sentinel: transport: # 配置sentinel dashboard 地址 dashboard: localhost:8080 # 默认8719 端口,假如被占用会自动从8719开始+1扫描,直到找到未被占用的端口 port: 8719 datasource: ds1: nacos: server-addr: localhost:8848 dataId: ${spring.application.name} groupId: DEFAULT_GROUP data-type: json rule-type: flow 一定要注意这个datasource 和 transport 是同级,不然会配置规则的时候失败, 在 nacos 中,配置一个 Data ID:cloudalibaba-sentinel-service 的文件,文件格式为json, 内容如下,可以自己更改: [ { "limitApp": "default", "resource": "/rateLimit/byUrl", "grade": 1, "count": 1, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ]