spring microservice learning 1

1. Microservice model

Microservice model
Core microservice development model
Microservice routing pattern
Microservice client elastic mode
Microservice security model
Microservice logging and tracking mode
Microservice construction and deployment model

Core microservice development model
Service granularity
letter of agreement
Interface design
Service configuration management
Event handling between services

Microservice routing pattern
Service discovery
Service routing

Microservice client elastic mode
Client load balancing
Circuit breaker mode
Backup mode
Bulkhead mode

Microservice security model
verification
Authorization
Credential management and propagation

Microservice logging and tracking mode
Log correlation
Log aggregation
Microservice tracking

Microservice construction and deployment model
Build and deploy pipeline
Infrastructure as code
Immutable server
Phoenix server

2. Use Spring Cloud to build
microservices 1)
Spring Boot Spring Boot simplifies the core tasks of building REST-based microservices
2) Spring Cloud Config
Spring Cloud Config handles the management of application configuration data through centralized services, so application configuration data (Especially the environment-specific configuration data) is completely separated from the deployed microservices. It ensures that no matter how many microservice instances are started, these microservice instances always have the same configuration.
Can be integrated:

Git-Git 个开惊版本控制系统
Consul-Consu 种开源的服务发现工具
Eureka-Eureka 个开源的 Netflix 项目

3)Spring Cloud 服务发现
可以从客户端消费的服务中抽象出部署服务器的物理位置( IP 或服务器名称) 。
4)Spring Cloud与 Netflix Hystrix和 Netflix Ribbon
使用 Netflix Hystrix 库,可以快速实现服务客户端弹性模式,如断路器模式和舱壁模式。
5)Spring Cloud与Netflix Zuul
Spring Cloud 使用 Netflix Zuul 项目为微服务应用程序提供服务路由功能 。
6) Spring Cloud Stream
是一种可让将轻量级消息处理集成到微服务中的支持技术,将微服务与消息代理进行整合,如 RabbitMQ Kafka。
7)Spring Cloud Sleuth
允许将唯一跟踪标识符集成到应用程序所使用的 HTTP 调用和消息通道(RabbitMQ Apache Kafka )之中。
可以结合日志聚合技术工具(如 Papetrail )和跟踪工具(如Zipkin )
8)Spring Cloud Security
是一个验证和授权框架,可以控制哪些人可以访问服务,以及他们可以用服务做什么 。
9)代码供应
Travis CI和 Docker 这两样工具,前者可以作为构建工具,而后者可以构建包含微服务的服务器镜像。

3、Spring Cloud示例

package demo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; 
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

@SpringBootApplication 
@RestController 
@RequestMapping(value=” hello”) 
@EnableCircuitBreaker	// 使服务能够使用Hystrix和Ribbon库
@EnableEurekaClient  // 告诉服务,它应该使用 Eureka服务发现代理注册自身,并且服务调用是使用服务发现来“查找”远程服务的位置的
public class Application {
    
    		
	public static void main(String[] args) {
    
     
			SpringApplication.run(Application .class, args);
	}

	@HystrixCommand(threadPoolKey = "helloThreadPool")	// 包装器使用 Hystrix 断路器调用helloRemoteServiceCall方法
	public String helloRemoteServiceCall(String firstName, String lastName) {
    
    
		ResponseEntity<String> restExchange = restTemplate.exchange(
		// 使用 一个装饰好的 RestTemplate类来获取一个“逻辑”服务 ID,Eureka 在幕后查找服务的物理位置。
			"http://logical-service-id/name/{firstName)/{lastName}", HttpMethod.GET, null, String.class, firstName, lastName);
		return restExchange.getBody();
	}
	
    @RequestMapping(value="/{firstName)/{lastName)", method = RequestMethod.GET)
	public String hello(@PathVariable("firstName") String firstName, @PathVariableable ("lastName") String lastName) {
    
    
		return helloRemoteServiceCall(firstName , lastName);
	}
}

@EnableCircuitBreaker@EnableEurekaClient注解。

@EnableCircuitBreaker注解告 Spring 微服务,将要在应用程序使用 Netflix Hystrix库。
@EnableEurekaClient 注解告诉微服务使用 Eureka 服务发现代理去注册它自己,并将要在代码中使用服务发现去查询远程 REST 服务端点。(服务器地址和端口号是在配置文件中配置。)

@HystrixCommand注解:

1、在任何时候调用 helloRemoteServiceCall该方法都不会被直接调用,这个调用会被委派给由 Hystrix 管理的线程池。
如果调用时间太长(默认为 1s ), Hystrix 将介入并中断调用这是断路器模式的实现。
2、创建一个由 Hystrix 管理的名为 helloThreadPool 的线程池。 所有对 helloRemoteServiceCall,方法的调用只会发生在此线程池中,
并且将与正在进行的任何其他远程服务调用隔离。

RestTernplate 类将与 Eureka 服务进行通信,并查找一个或多个“name”服务实例的实际位置。作为服务的消费者,不需要知道服务的位置。

Guess you like

Origin blog.csdn.net/tongwudi5093/article/details/114399159