Spring cloud Alibaba, the only domestic open source project of spring community, graduated

Time:2019-11-20

Alimei’s Guide:More than a year ago, a big event happened in the Java industry recently. Alibaba opened source spring cloud Alibaba and launched the first preview version. Spring cloud itself is a set of microservice specifications, not a framework that can be used as soon as it is used. The open source of spring cloud Alibaba provides developers with the implementation of this set of specifications. At the same time, the complete microservice components, Chinese documents and localized open source services provided by spring cloud Alibaba improve the speed of developers’ access to microservices and reduce the difficulty of subsequent operation and maintenance.

After more than one year’s incubation, spring cloud Alibaba, as the only domestic open source project of spring community, officially graduated from the spring cloud incubator, and released three new versions of spring cloud Edgware, Finchley and Greenwich.
Spring cloud Alibaba is the first and only domestic open source project of spring community.

Spring cloud Alibaba, the only domestic open source project of spring community, graduated

(refer to spring blog for official graduation announcement: https://spring.io/blog/2019/07/24/simplifying-the-spring-cloud-release-train)

After graduation, spring cloud Alibaba moved from the incubator warehouse to GitHub Alibaba warehouse. Click here for the new warehouse address The new Maven coordinates are as follows:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.1.0.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

Compatibility table of spring cloud Alibaba versions:

Spring cloud Alibaba, the only domestic open source project of spring community, graduated

Introduction to spring cloud Alibaba

Spring cloud is a framework built by the spring community based on spring boot for rapid construction of distributed systems. It mainly includes the following features:

Spring cloud Alibaba, the only domestic open source project of spring community, graduated

Spring cloud Alibaba is the integration of Alibaba open source middleware and spring cloud system:

Spring cloud Alibaba, the only domestic open source project of spring community, graduated

As a new implementation under the spring cloud system, spring cloud Alibaba has more functions than official components or other third-party implementations such as Netflix, consul, zookeeper, etc

Spring cloud Alibaba, the only domestic open source project of spring community, graduated

Spring cloud Alibaba features

Overall framework

Relying on spring cloud Alibaba, you can connect spring cloud applications to Alibaba microservice solutions by adding some annotations and a few configurations, and quickly build distributed application systems through Alibaba middleware.

Spring cloud Alibaba, the only domestic open source project of spring community, graduated

Service registration & Configuration Management

Service registration and configuration management are two essential basic functions in microservice applications.

Based on Nacos, spring cloud Alibaba provides spring cloud Alibaba starter Nacos Discovery & spring cloud Alibaba starter Nacos config to realize service registration and configuration management.

Rely on @ enablediscoveryclient to register services, and clients compatible with resttemplate & openfeign to call services.

Openfeign client:

@FeignClient(name = "echo-service")
public interface EchoService {
  @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
  String echo(@PathVariable("str") String str);
}

Resttemplate client:

@LoadBalanced
@Bean
public RestTemplate restTemplate1() {
  return new RestTemplate();
}
....
restTemplate.getForObject("http://echo-service/echo/hello-spring-cloud-alibaba",
        String.class);

Configuration management directly constructs dataid & group in a conventional way to read configuration settings from Nacos to spring environment.

Flow control downgrade

Flow control degradation is the magic weapon of microservice stability, which can be used to solve the serious consequences of various unstable scenarios.

At the level of flow restriction: we control the flow according to the processing capacity of the system, so as to ensure the high throughput of the system without destroying the system.

At the degradation level: temporarily cut off the service call when the service is unstable, and wait for a period of time to try again. On the one hand, it can prevent the unstable service from being “worse”, on the other hand, it can protect the caller of the service from being dragged down.

Spring cloud Alibaba provides spring cloud Alibaba starter sentinel based on sentinel to adapt all clients and gateways in spring system, including webservlet, Webflux, resttemplate, openfeign, Netflix zuul and spring cloud gateway.

You just need to introduce a starter to take effect on these components. Only define rules for flow control degradation:

Current limiting rules:

Spring cloud Alibaba, the only domestic open source project of spring community, graduated

Degradation rules:

Spring cloud Alibaba, the only domestic open source project of spring community, graduated

Service invocation

Spring cloud’s default service invocation relies on openfeign or resttemplate to use rest for invocation.

Spring cloud Alibaba Dubbo only needs to use @ dubbotransported annotation to seamlessly switch the underlying rest protocol to Dubbo RPC Protocol for RPC calls. This move gives spring cloud a new RPC option for client calls:

@Bean
@LoadBalanced
@DubboTransported
public RestTemplate restTemplate() {
  return new RestTemplate();
}

@FeignClient("dubbo-provider")
@DubboTransported(protocol = "dubbo")
public interface DubboFeignRestService {
  @GetMapping(value = "/param")
  String param(@RequestParam("param") String param);

  @PostMapping("/saveB")
  String saveB(@RequestParam("a") int a, @RequestParam("b") String b);
}

Distributed message & message bus

Based on spring cloud stream, it provides a new implementation of binder: spring cloud stream rocketmq binder. It also adds a new implementation of spring cloud bus message bus, spring cloud bus rocketmq.

Spring cloud stream encapsulates the message programming model in a unified way. It uses the same set of code to send / receive messages, and shields the implementation details of the underlying message middleware. Let’s focus on the business logic. If you want to replace the underlying message middleware, you can directly introduce the new implementation of binder, and the code level needs little modification. This is a process of receiving messages by using spring cloud stream rocketmq & spring cloud function, converting them to uppercase & adding new messages with Alibaba prefix, and sending them to new topics:

spring.cloud.stream.bindings.input.destination=test-topic
spring.cloud.stream.bindings.input.group=test-group1

spring.cloud.stream.bindings.output.destination=test-topic-uppercase

spring.cloud.stream.function.definition=uppercase|addprefix
@SpringBootApplication
@EnableBinding(Processor.class)
public class FunctionApplication {

  public static void main(String[] args) {
    new SpringApplicationBuilder(FunctionApplication.class)
        .web(WebApplicationType.NONE).run(args);
  }

    @Bean
    public Function<String, String> uppercase() {
      return x -> x.toUpperCase();
    }

    @Bean
    public Function<String, String> addprefix() {
        return x -> "alibaba-" + x;
    }

}

Distributed transaction

Seata is Alibaba’s open-source distributed transaction middleware, which solves the distributed transaction problems faced in the microservice scenario in an efficient and invasive way.
At the spring cloud level, we have completed the access of Seata at the spring cloud level by passing the transaction context in the microservice:

  • Support automatic restore of Seata transaction context in spring MVC environment
  • Support the automatic passing of the sea TA transaction context when the service caller calls through resttemplate or feignclient
  • Support feignclient, hystrix, sentinel simultaneous scenarios
    This is a distributed transaction code that uses spring cloud Alibaba sea TA to complete storage and order services for data storage:
@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
@RequestMapping(value = "/seata/rest", method = RequestMethod.GET, produces = "application/json")
public String rest() {

  //Call the storage service. Database operations involved
  String result = restTemplate.getForObject(
      "http://127.0.0.1:18082/storage/" + COMMODITY_CODE + "/" + ORDER_COUNT,
      String.class);

  if (!SUCCESS.equals(result)) {
    throw new RuntimeException();
  }

  String url = "http://127.0.0.1:18083/order";
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

  MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
  map.add("userId", USER_ID);
  map.add("commodityCode", COMMODITY_CODE);
  map.add("orderCount", ORDER_COUNT + "");

  HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(
      map, headers);

  //Call the order service. Database operations involved
  ResponseEntity<String> response = restTemplate.postForEntity(url, request,
      String.class);

  result = response.getBody();

  if (!SUCCESS.equals(result)) {
    throw new RuntimeException();
  }

  return SUCCESS;
}

Roadmap

Spring boot admin is an open source community project for managing and monitoring spring boot applications. But it doesn’t do deep integration with spring cloud. We hope to make a spring cloud admin, which can provide the following functions:

  • Increase service governance console and integrate microservice control capability
  • Service query and management
  • configuration management
  • Current limiting degradation, etc
  • Project management / monitoring
  • Refer to spring cloud azure playground http://azure-spring-cloud.azure websites.net/, create spring cloud Alibaba playground, and put some best practices, video tutorials, automatic project generation and other functions on it
  • Add spring cloud Alibaba best practice project
  • According to various features of spring cloud Alibaba, develop corresponding practical demo
  • Replace the spring cloud service and call the client openfeign & ribbon. Develop more general service calling client, replace spring cloud service — call use client openfeign & Ribbon

If you are interested in these contents, please leave a message at GitHub issue.

Follow-up

Spring cloud Alibaba graduation does not mean the end, but a new beginning. Then we will have a series of actions:

  • The dependency of spring cloud Alibaba will be provided on the spring initializr page
  • Spring cloud Alibaba introduction is provided on the official introduction page of spring cloud
  • Provide the domestic official website of spring cloud Alibaba


Author: middleware brother

Read the original text

This is the original content of yunqi community, which can not be reproduced without permission.

Recommended Today

JavaScript (E5, 6) regular learning summary learning, can see can not see!

1. overview Regular expression is a method to express text pattern (string structure). There are two ways to create: One is to use literals, with slashes for start and end. var regex = /xyz/ The other is to use the regexp constructor. var regex = new RegExp(‘xyz’); The main difference between them is that the […]