Spring Cloud Alibaba large Internet multi scenario best practices online disk download

Download: Spring Cloud Alibaba large Internet multi scenario best practices online disk download

How will the large-scale Internet field develop?

In the field of large-scale Internet, from super portal to super interface, from super APP to super API, super interface should have two meanings: one is "super", that is, it has wide application and large volume. Second, the "interface", that is, a to B middle tier business, does not directly reach consumers; At the same time, "interface" also means a highly standardized business form. For Internet companies with certain strength, they are experiencing a similar process of business focus transfer - from super portal to super interface, from super APP to super API. The idea of "super interface" is to "take a step back", retreat from the application layer nearest to the C-end consumers to the technology layer, and transfer some general capabilities to diversified industries and scenarios. Super interface should have two meanings: one is "super", that is, it has wide application and large volume. Second, the "interface", that is, a to B middle tier business, does not directly reach consumers; At the same time, "interface" also means a highly standardized business form. Spring cloud can play a key role in many Internet best practices.

Introduction to Spring Cloud Alibaba

It provides flow control and fusion degradation functions for microservices. It has the same function as hystrix, which can effectively solve the "avalanche" effect of microservice invocation and provide a stable solution for microservice system. With hytrxi entering the maintenance period and no longer providing new functions, sentinel is a good choice. Typically, hystrix uses a thread pool to isolate service calls, and sentinel uses user threads to isolate interfaces. Compared with the two, hysrxi is a service level isolation, sentinel provides interface level isolation, and sentinel has a finer isolation level. In addition, sentinel directly uses user threads to limit, which reduces the overhead of thread switching compared with the thread pool isolation of hysrx. In addition, Sentinel's dashboard provides online configuration to change the current restriction rules. Also more optimized. According to the official literature, sentinel has the following characteristics: rich application scenarios: in recent 10 years, sentinel has been undertaking the core scenario of Alibaba's double 11 traffic promotion. For example, spike (i.e. burst flow control within the system capacity), message peak clipping and valley filling, and real-time disconnection of downstream unavailable applications. Complete real-time monitoring: sentinel also provides real-time monitoring function. You can see the secondary data of a single computer connected to the application in the console. The cluster running in aggregation is less than 500 units. Extensive open source ecosystem: sentinel provides out of the box integration modules with other open source frameworks / libraries, such as spring cloud, dubbo and grpc. You can quickly access sentinel by simply introducing the corresponding dependencies and performing a simple configuration. Complete spi extension points: sentinel provides easy-to-use and complete spi extension points. You can achieve this by implementing extension points and Quick custom logic. For example, custom rule management, adaptive data source, etc.

Spring Cloud Alibaba multi scenario best practices in large Internet field

How to use sentinel in spring cloud Alibaba

Sentinel, as a part of Alibaba, is very simple to use in the spring cloud project. Now let's explain how to use sentinel in the spring cloud project in the form of a case. This project is based on the previous nacos tutorial case. Add the initial dependency of Sentinel's spring cloud to the pom file of the project. The code is as follows:

<dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-starter-alibaba-sentinel</artifactid>
  <version>0.9.0.release</version>
</dependency>
server:
 port:8762
spring:
 application:
 name:nacos-provider
 cloud:
 nacos:
  discovery:
  server-addr:127.0.0.1:8848
 sentinel:
  transport:
  port:8719
dashboard:localhost:8080
@restcontroller
public class providercontroller {
 @getmapping ("/hi")
 @sentinelresource (value="hi")
 public string hi (@requestparam (value="name", defaultvalue="forezp", required=false) string name) {
  return "hi" + name;
 }
}
@restcontroller
public class providercontroller {
 @getmapping ("/hi")
 @sentinelresource (value="hi")
 public string hi (@requestparam (value="name", defaultvalue="forezp", required=false) string name) {
  return "hi" + name;
 }
}@restcontroller
public class providercontroller {
 @getmapping ("/hi")
 @sentinelresource (value="hi")
 public string hi (@requestparam (value="name", defaultvalue="forezp", required=false) string name) {
  return "hi" + name;
 }
}
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-openfeign</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-alibaba-sentinel</artifactid>
<version>0.9.0.release</version>
</dependency>
In the configuration file, in addition to the sentinel.transport. Dashboard configuration, you also need to add the feign.sentinel.enabled configuration. The code is as follows:

server:
 port:8763
spring:
 application:
 name:nacos-consumer
 cloud:
 nacos:
  discovery:
  server-addr:127.0.0.1:8848
 sentinel:
  transport:
  port:8719
  dashboard:localhost:8080
feign.sentinel.enabled:true
Write a feignclient and call the/hi interface of nacos-provider:

@feignclient ("nacos-provider")
public interface providerclient {
 @getmapping ("/hi")
 string hi (@requestparam (value="name", defaultvalue="forezp", required=false) string name);
}
Write a restcontroller to call providerclient, the code is as follows:

@restcontroller
public class consumercontroller {
 @autowired
 providerclient providerclient;
 @getmapping ("/hi-feign")
 public string hifeign () {
  return providerclient.hi ("feign");
 }
}

Spring Cloud Alibaba multi scenario best practice in large Internet field: implementing Gateway dynamic Gateway based on Nacos

Dynamic routing means that the router can automatically establish its own routing table and adjust it in time according to the changes of the actual situation. The routing table entries on the dynamic router exchange information with each other through the interconnected router, and optimize according to a certain algorithm; The routing information is updated continuously in a certain time interval to adapt to the changing network and obtain the optimal routing effect. The initial operation is that the operation of dynamic routing mechanism depends on two basic functions

  1. Nacos environment preparation does not introduce specific Nacos configurations. You can refer to the official introduction of Alibaba. Here, you can directly start the stand-alone mode through windows and log in to the Nacos Console. Create the namespace dev, and create the dataId of the gateway router under the default group under dev

[{ "id": "consumer-router", "order": 0, "predicates": [{ "args": { "pattern": "/consume/**" }, "name": "Path" }], "uri": "lb://nacos-consumer" },{ "id": "provider-router", "order": 2, "predicates": [{ "args": { "pattern": "/provide/**" }, "name": "Path" }], "uri": "lb://nacos-provider" }]
  1. Connect to the Nacos configuration center. Generally, the configuration of the "configuration center" in the project is usually configured in the boot. propertis (yaml) to ensure that the routing configuration in the project is read from Nacos Config.
# nacosConfiguration Center configuration is recommended to be configured in bootstrap.properties
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.file-extension=properties
 # Configuration center namespace: dev namespace (environment)
spring.cloud.nacos.config.namespace=08ecd1e5-c042-410a-84d5-b0a8fbeed8ea
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

...

...

Added by starmsa on Fri, 18 Feb 2022 17:54:15 +0200