(二期)22、微服务框架spring cloud(一)
- 单一职责原则
- 服务自治原则
- 轻量级通信机制
- 跨语言、平台
- 通信体量轻,如REST
- 微服务粒度
- 主要使用RestTemplate完成rest调用
- 适用场景有局限,服务提供者对我接口不能发生变化,会直接影响到消费者。
- 服务容灾不法保证,容易造成服务雪崩
- 适合较为简单微服务系统
开发工具集合,含有多个项目。
简化了分布式开发。
- 利用Spring boot的开发便利
- 主要是基于对Netfix开源组件的进一步封装
- 分布式/版本化配置
- 服务注册和发现
- 路由
- 服务和服务之间的调用
- 负载均衡
- 断路器
- 分布式消息传递
从上图可以看出Spring Cloud各个组件相互配合,合作支持了一套完整的微服务架构。
- 其中Eureka负责服务的注册与发现,很好将各服务连接起来
- Hystrix 负责监控服务之间的调用情况,连续多次失败进行熔断保护。
- Hystrix dashboard,Turbine 负责监控 Hystrix的熔断情况,并给予图形化的展示
- Spring Cloud Config 提供了统一的配置中心服务
- 当配置文件发生变化的时候,Spring Cloud Bus 负责通知各服务去获取最新的配置信息
- 所有对外的请求和服务,我们都通过Zuul来进行转发,起到API网关的作用
- 监控我们使用Sleuth+Zipkin+springAdmin将所有的请求数据记录下来,方便我们进行后续分析
(心跳检测、健康检查、负载均衡等)
- Eureka Server注册中心
- Eureka Client服务注册
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--actuator用于应用监控管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
#开启Eurka注册中心服务
@EnableEurekaServer
server:
port: 8761
eureka:
client:
# 表示是否将自己注册到Eureka Servcer。
register-with-eureka: false
# 表示是否从Eureka Server获取注册信息
fetchRegistry: false
serviceUrl:
# 设置Eureka Server交互的地址
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: eureka-server-1
# 开启注册服务发现功能
@EnableDiscoveryClient
# 开启Eureka注册中心服务功能
@EnableEurekaServer
自我保护的条件:
那这个阀值是多少呢?
关闭自我保护模式(生产上不建议)
(关闭了自我保护之后,然后需要几分钟才能把失效的客户端节点删掉)
注册中心节点之间相互注册即可实现高可用部署。
假如3个以上也一样,把其他的都注册到自己的server上面,用逗号隔开。客户端需要配置所有的Eureka server地址。
原理
注册中心地位
- eureka
- nginx
- zookeeper
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
spring:
security:
user:
name: admin
password: admin
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭csrf
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
eureka:
client:
# 设置Eureka Server交互的地址
service-url:
defaultZone: http://admin:admin@localhost:8761/eureka/
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
@Bean
//为RestTemplate整合Ribbon的负载均衡能力
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
ORDER:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
@Autowired
private LoadBalancerClient loadBalancerClient;
LoadBalancerInterceptor
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
import com.itmuch.cloud.microserviceconsumermoviefeign.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@GetMapping(value = "/{id}")
User findById(@PathVariable("id") Long id);
}
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
- http://www.itmuch.com/spring-cloud-sum-feign/