SpringCloud
约 792 字大约 3 分钟
1. springcloud常用组件
1.1. 服务发现与注册ZooKeeper、Eureka、consoul
//只需要使用 @EnableEurekaServer 注解就可以让应用变为 Eureka Server
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
eureka:
instance:
hostname: localhost
client:
# eureka.client.fetch-registry: 表示是否从 Eureka Server 获取注册信息,默认为true。如果这是一个单点的 Eureka Server,不需要同步其他节点的数据,设为false
fetch-registry: false
# eureka.client.register-with-eureka: 表示是否将自己注册到 Eureka Server, 默认为true。由于当前应用就是 Eureka Server, 因此设为 false
register-with-eureka: false
# 设置 Eureka Server 所在的地址,查询服务和注册服务都需要依赖这个地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- Eureka client
//只需要使用 @EnableEurekaClient 注解就可以让应用变为 Eureka Client
@EnableEurekaClient
@SpringBootApplication
public class HomepageCourseApplication {
public static void main(String[] args) {
SpringApplication.run(HomepageCourseApplication.class, args);
}
}
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 服务注册与发现consul
默认端口8500
启动命令consul agent -client 0.0.0.0 -dev -config-dir=config-dev
mysql.json
{
"service":
{
"id": "mysql",
"name": "mysql",
"tags": ["mysql"],
"address": "127.0.0.1",
"port": 3306,
"checks":
[
{
"id": "mysql",
"name": "mysql",
"tcp": "127.0.0.1:3306",
"interval": "60s",
"timeout": "2s"
}
]
}
}
java注册
@EnableDiscoveryClient
@RestController
spring.cloud.consul.enabled=true
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.serviceName=xlsys-hscl-distributed-server
spring.cloud.consul.discovery.tags=xlsys,xlsys-server,xlsys-hscl-distributed-server
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.instance-id=server2
spring.cloud.consul.discovery.healthCheckPath=/xlsys-check
spring.cloud.consul.discovery.healthCheckInterval=2s
1.2. 服务间调用Feign与断路器Hystrix
@EnableFeignClients
@EnableCircuitBreaker
@EnableEurekaClient
@SpringBootApplication
public class HomepageUserApplication {
public static void main(String[] args) {
SpringApplication.run(HomepageUserApplication.class, args);
}
}
与spring:application:name: eureka-client-homepage-course对应CourseClientHystrix熔断降级策略
@FeignClient(value = "eureka-client-homepage-course", fallback = CourseClientHystrix.class)
public interface CourseClient {
@RequestMapping(value = "/homepage-course/get/course", method = RequestMethod.GET)
CourseInfo getCourseInfo(Long id);
@RequestMapping(value = "/homepage-course/get/courses", method = RequestMethod.POST)
List<CourseInfo> getCourseInfos(@RequestBody CourseInfosRequest request);
}
@Component
public class CourseClientHystrix implements CourseClient {
@Override
public CourseInfo getCourseInfo(Long id) {
return CourseInfo.invalid();
}
@Override
public List<CourseInfo> getCourseInfos(CourseInfosRequest request) {
return Collections.emptyList();
}
}
feign:
hystrix:
enabled: true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=180000
<!-- 引入 Feign, 可以以声明的方式调用微服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 引入服务容错 Hystrix 的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
1.3. 网关Zuul、gatyway
//SpringCloudApplication = @SpringBootApplication + @EnableDiscoveryClient + @EnableCircuitBreaker
@EnableZuulProxy
@SpringCloudApplication
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
}
server:
port: 9000
zuul:
prefix: /imooc
routes:
course:
path: /homepage-course/** ==>server.servlet.context-path:/homepage-course
serviceId: eureka-client-homepage-course ==>spring.application.name:eureka-client-homepage-course
strip-prefix: false
user:
path: /homepage-user/**
serviceId: eureka-client-homepage-user
strip-prefix: false
访问网关:http://网关服务ip:网关服务的端口号/imooc/....
不访问网关:http://普通服务ip:普通服务的端口号/....
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
- 网关gateway
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
spring.cloud.gateway.httpclient.ssl.useInsecureTrustManager=true
spring.cloud.gateway.httpclient.ssl.handshake-timeout-millis=10000
spring.cloud.gateway.httpclient.ssl.close-notify-flush-timeout-millis=3000
spring.cloud.gateway.httpclient.ssl.close-notify-read-timeout-millis=0
1.4. 分布式配置consul
spring.cloud.consul.config.enabled=true
spring.cloud.consul.config.format=PROPERTIES
spring.cloud.consul.config.prefix=config
spring.cloud.consul.config.data-key=data
@EnableFeignClients //启用OpenFeign
服务间调用
@FeignClient("server2")
public interface OpenFeginDemo {
@GetMapping("/testserver2")
public String test();
}
1.5. 负载均衡Ribbon
ribbon.ConnectTimeout=30000
ribbon.ReadTimeout=60000
ribbon.SocketTimeout=60000