目录
3、补充:springcloud常用配置【application.yml】
官网:Spring Cloud
辅助学习视频:【SpringCloud】分布式微服务框架_哔哩哔哩_bilibili
SpringCloud版本与SpringBoot版本对应关系
1、Eureka服务注册与发现
Eureka草图
创建项目,首先打开IDEA,创建一个maven项目,我名字是cloudparent
先添加依赖推荐看官网,因为要与时俱进
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>clound-parent</groupId>
<artifactId>clound-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.12.RELEASE</version>
</parent>
<properties>
<spring.cloud-version>Hoxton.SR8</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>
</project>
创建子模块,名字写错了,改成clound-server-eureka
添加对应依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 8866
eureka:
client:
fetch-registry: false #拉取服务
register-with-eureka: false #注册自己
service-url:
defaultZone: http://127.0.0.1:8866/eureka/ #集群互相注册
instance:
prefer-ip-address: true
spring:
application:
name: server-eureka
创建子模块clound-client-product
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
测试!!!启动两个服务,访问8866
思维导图
在商品服务添加个controller文件,就是按id获取商品的
@GetMapping("getProductById")
public String getProductById(Integer productId){
System.out.println("服务提供者根据ID查询商品:"+productId);
return "这是你要的商品"+productId;
}
问题引入!!!如果商品服务的controller层,如何在订单服务中使用,这就需要Feign
2、Feign的使用
在原来的项目基础上再创建一个order子模块
依赖如下,要导入openfeign
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 8082
spring:
application:
name: client-order
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8866/eureka/
order子模块想获得product子模块中的商品信息,就要使用远程调用
这里要创建远程调用的方法,在oder子模块创建remote/ProductRemote.java 基本上就是把product那个方法复制过来,变成接口类型
package com.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name="client-product")
public interface ProductRemote {
@GetMapping("/getProductById")
public String getProductById(@RequestParam Integer productId);
}
再创建order子模块的controller
package com.controller;
import com.remote.ProductRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@Autowired
private ProductRemote productRemote;
@GetMapping("/getOrderDetail")
public Object getOrederDetail() {
Integer productId = 10066;
String product = productRemote.getProductById(productId);
System.out.println("服务小消费者,获取远程服务提供的商品信息");
return product;
}
}
最后一步在启动函数上加
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
public class OderApplication {
public static void main(String[] args){
SpringApplication.run(OderApplication.class);
}
}
测试!!!
4、补充:springcloud常用配置【application.yml】
服务端口号:
server:
port: 8081
Spring配置:
spring:
datasource: #配置数据源四要素
url: jdbc:mysql://localhost:3306/travel_heima
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
application: #服务名称
name: consumer-service
MyBatis配置:
mybatis:
type-aliases-package: cn.xxx.pojo
configuration:
map-underscore-to-camel-case: true #开启驼峰匹配
Eureka服务配置
eureka:
client:
fetch-registry: false #拉取服务
register-with-eureka: false #注册自己
service-url:
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka #集群互相注册
instance:
ip-address: 127.0.0.1
instance-id: ${eureka.instance.ip-address}:${server.port}
zuul网关配置:
zuul:
prefix: /api #前缀
routes:
user-service:
path: /user/** #访问路径
serviceId: user-service #服务id
strip-prefix: false #是否开启前缀
ignored-services:
- user-service
- consumer-feign
Ribbon负载均衡配置:
ConnectTimeout: 500 # ribbon链接超时时长
ReadTimeout: 2000 # ribbon读取超时时长
MaxAutoRetries: 0 # 当前服务重试次数
MaxAutoRetriesNextServer: 1 # 切换服务重试次数
OkToRetryOnAllOperations: false # 是否对所有的请求方式都重试,只对get请求重试
Hystrix配置:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000 # 熔断超时时长:6000ms
4、erueka集群配置
模拟集群,启动三次同样的进程,但是每次进程的yml文件不一样,勾选这个可以重复启动相同的进程,不会覆盖原来的
application.yml配置
server:
port: 8863
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8862/eureka/,http://127.0.0.1:8861/eureka/ #集群互相注册
instance:
hostname: 127.0.0.1
spring:
application:
name: server-eureka
server:
port: 8861
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8862/eureka/,http://127.0.0.1:8863/eureka/ #集群互相注册
instance:
hostname: 127.0.0.1
spring:
application:
name: server-eureka
server:
port: 8862
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8861/eureka/,http://127.0.0.1:8861/eureka/ #集群互相注册
instance:
hostname: 127.0.0.1
spring:
application:
name: server-eureka
后续【SpringCloud】分布式微服务框架_哔哩哔哩_bilibili
我看到第7节了,目前我需要去学习Django了,这个后期再继续深入学