Spring Boot 2.0-based application integration and use of IoT CSE practice

Abstract This paper describes the steps based on Spring Boot IoT 2.0 application integration and use of CSE, benefits and problems. Use CSE, can quickly make a prototype system with micro-management, and operation and maintenance services, accelerate the speed of the line of business, reduce operating risk services.

By application of a IoT Spring Boot 2.0 show the integrated and used CSE. IoT applications originally developed using Spring Boot 2.0, CSE integration step by a small amount. This article explains what the future brings CSE integrates new features to the application, issue and some of the changes occurring in the middle of the transformation process may encounter. Through this example, we will be a Spring Boot rapid prototype system 2.0, transformed into micro-services have the versatility and reliability of operation and maintenance capabilities, speed up the construction speed of business systems. This document is refreshed according to CSE JAVA SDK 2.3.52 version.

Integration Steps

The basic integrated three-step process.

  1. Increased dependence

For introducing CSE spring boot 2 provided Package dependency management and CSE in its dependency management dependencyManagement inside. Note that the dependency management spring boot 2 to the front, because the CSE is the default dependency management of spring boot 1. If the business need to introduce special spring boot or spring cloud version, this mechanism can also be spring boot or spring cloud of dependencyManagement introduced. [Here] (http://servicecomb.apache.org/cn/docs/maven_dependency_management/) has introduced a dependencyManagement article, use dependencyManagement? Tripartite pieces to solve the conflict, is a very useful technique, proper understanding of its principles can help developers quickly locate and resolve common pieces of tripartite conflict. [Here] (https://huaweicse.github.io/cse-java-chassis-doc/using-cse-in-spring-boot/spring-boot-2.html) illustrates integration CSE spring boot 1, spring boot principles and components 2.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.huawei.paas.cse</groupId>
            <artifactId>cse-dependency-spring-boot2</artifactId>
            <version>${paas.cse.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

CSE then introduced to the support assembly spring boot 2 provided in the package.

<dependencies>
    <dependency>
        <groupId>com.huawei.paas.cse</groupId>
        <artifactId>cse-solution-service-engine</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.servicecomb</groupId>
        <artifactId>spring-boot2-starter-servlet</artifactId>
    </dependency>
</dependencies>

2. Modify operating parameters 

By @EnableServiceComb label in Spring Boot enabled in 2 and loading CSE. In Spring Boot 2, developers will be published as a service by DispatcherServlet REST interface, in Spring Boot 2 default implementations, REST interfaces DispatcherServlet release, after integration CSE, CSE provided by the service RestServlet publish. After the integration of the two Servlet can coexist, their context can be set to a different prefix to avoid conflicts. With particular reference to Step 3.

@EnableServiceComb
public   class Application {
    public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
    }
}

3. Publish the service interface

And Spring Boot 2, classes CSE also scans @RestContoller labels, and publish these classes the REST interface. While the interface information registered to the service center. You can specify the service center address in application.yml and provides a simple micro-information service. Which service_description.name can use the name of the original project, cse.rest.address using server.port tomcat listening port of publishing. Wherein server.servlet.path = / mvc and servicecomb.rest.servlet.urlPattern = / cse / * are the DispatcherServlet RestServlet prefix Spring Boot and the CSE.

APPLICATION_ID:   houseapp
service_description:
  name: ${spring.application.name}
  version: 0.0.1
 
cse:
  service:
    registry:
      address: http://localhost:30100
      instance:
        watch: false
  rest:
    address: 0.0.0.0:9091
  
  servlet:
    urlPattern: /cse/*
    
server:
  servlet: 
    path: /mvc

Integrated results

In the example above, a very simple operation will be converted into the original Spring Boot 2.0 application when the CSE. But conversion is not an end in itself, by following some changes to illustrate the benefits brought to the business system after conversion and the possible loss of function and workload to facilitate the user to evaluate whether it is worthwhile to do so integration.

Run-time changes

Run-time changes are a direct source of gains and losses. In the transformation of the above, the client code and server code did not change (or little change), the client still use RestTemplate call server-side code, the server still provides a REST interface, you can use tools such as access to post man. Run-time change does not modify the micro-services interface to external behavior, this is the basis for transformation.

Before the transformation, the following diagram when running Spring Boot 2. In the client, a variety of processing MessageConverters Spring RestTemplate request message to provide a response message decoding and encoding, on the server side can use various functions provided by the Spring MVC user verification parameters.

image.png

After the transformation, the following diagram is running. CSE client and server provides a uniform consistent processing chain, and the default implementation of the [load balancing] (https://docs.servicecomb.io/java-chassis/zh_CN/references-handlers/loadbalance.html), [ Fault-tolerant fuse] (https://docs.servicecomb.io/java-chassis/zh_CN/build-provider/configuration/downgrade-strategy.html), [flow control] (https://docs.servicecomb.io/java- chassis / zh_CN / build-provider / configuration / ratelimite-strategy.html) functions.

image.png

Run-time feature a core function of the rapid completion of the application of micro service transformation. A basic feature of the service is a multi-micro example, communicate over the network interface. Therefore, to solve the problem of service discovery, and communication reliability is not micro-services of a basic protection. By integrating CSE, to help users quickly build these capabilities, reducing the need to learn and develop other Spring Boot, cost Spring Cloud components.

CSE itself also provides a wealth of development and expansion of capacity, from [design] (https://bbs.huaweicloud.com/blogs/1fc9427c088611e89fc57ca23e93a89f) and [Guide] (https://docs.servicecomb.io/java-chassis / zh_CN /) for more information on the CSE.

Changes in development mode

Use Spring Boot 2, developers can use RestTemplate or using Feign end interface to access the service. CSE also supports two ways RestTemplate and RPC. Compared to Feign, CSE's RPC more simple, no need to define and use REST client labels.

@RpcReference(microserviceName   = "hello", schemaId = "hello")

private   Hello hello;

System.out.println(hello.sayHi("Java   Chassis"));

This approach applies to the use of access between the CSE framework for the development of micro-services. If you visit a third party, CSE provides a similar Feign mechanism, and provides powerful than Feign load balancing management and governance capabilities (Consumer processing chain). Related examples with reference to the code ServiceComb [Example] (https://github.com/apache/incubator-servicecomb-java-chassis/blob/master/integration-tests/it-consumer/src/main/java/org/apache/ servicecomb / it / testcase / thirdparty / Test3rdPartyInvocation.java).

By integrating CSE, developers can easily use in your code at any time REST and RPC, very flexible, saves time writing code.

Surrounding tool change

The above discussion is limited to micro service itself. In order to ensure business can continue to function efficiently run operation and maintenance, but also the need to provide services to micro a powerful operating environment, to achieve control of micro health services, real-time adjustment of the micro-service functions (flow control, release the gray, fuse fault tolerance, etc.) . Here you can quickly implement solutions by means of commercial or open source solutions to build their own.

l Business Solutions

Huawei [Micro Services Engine] (https://console.huaweicloud.com/cse/) provides one-stop service micro-management functions. The transformation of deploying applications to micro-services engine, you can achieve the micro-service catalog, a variety of functional interface management, dynamic governance.

Micro-service engine core components include service center, distribution center and management center. In addition to the deployment of business to the cloud, developers can use these services locally, just to have the available network connection, Huawei cloud microphysical service engine registration and obtain AK / SK identifiable information. [Buy System] (https://github.com/huaweicse/HouseApp/tree/spring-boot-2.0-with-zipkin) provides an example of using a local Huawei cloud services. Developers only need to configure the AK in application.yml / SK Information and Service Center Address information.

l Open Source Solutions

[Center] (  https://github.com/apache/incubator-servicecomb-service-center ). Registration Service Center provides discovery services, but also provides a front-end services, and directory service for viewing interface testing.

[Configuration Center] (  https://github.com/ctripcorp/apollo ). This is the configuration center Ctrip development. CSE supported by Netflix Archaius extension, using a variety of configuration services.

[Call chain Zipkin] (  https://github.com/openzipkin/zipkin ). This standard zipkin call chain services. CSE in support docking with zipkin call chain by Hanlder extension.

[Call chain skywalking] (  https://github.com/apache/incubator-skywalking/tree/master/apm-sniffer/apm-sdk-plugin/servicecomb-plugin ).

From [Tutorial] (  https://docs.servicecomb.io/java-chassis/zh_CN/ understand) how to integrate and use these open source components.

Spring Boot CSE itself as a Servlet 2.0 run, thus Spring Boot, most components Spring Cloud provided, the user can choose. Spring Cloud scene will choose eureka and other services as service discovery, CSE needed to use the service as a service center found. In the service center selection, has yet to have a unified standard, supporting the SDK can only use the recommended service discovery tool.

Load balancing and isolation show

After the show here through a transformation example, load balancing and isolation. This [example] (https://bbs.huaweicloud.com/blogs/72a312f09c8911e89fc57ca23e93a89f) demonstrated in multi-instance deployments, rolling upgrades zero interruption.

[Purchase system] (  https://github.com/huaweicse/HouseApp/tree/spring-boot-2.0-with-zipkin ) achieved a complete spring cloud applications can use this system to experience.

Use zikpin call chain traceability show

Demonstrated here quickly build call chain traceability in the system. After transformation, the only reference to the need to introduce the call chain jar package and increase the processing chain configuration and zipkin server address in application.yml and installs zipkin server, you can use the call chain. [Development Step] (  https://docs.servicecomb.io/java-chassis/zh_CN/general-development/microservice-invocation-chain.html ).

[Purchase system] (  https://github.com/huaweicse/HouseApp/tree/spring-boot-2.0-with-zipkin ) realized the call chain.

Open metrics monitoring

CSE provides a powerful [metrics Function] (https://docs.servicecomb.io/java-chassis/zh_CN/general-development/metrics.html), it can help developers analyze performance issues. After transformation, the only reference to the need to introduce metrics related jar package, and increase switch enables metrics in application.yml, can be used. metrics data can be obtained in two ways.

l through the interface query

地址: http://localhost:9091/metrics

输出参考:(截取了执行时间和平均调用次数,统计周期为60s)

"servicecomb.invocation(operation=huawei-cloud-ioccity-app-basemgmt.EquipmentInfoController.getEquipmentList,role=PRODUCER,stage=execution,statistic=totalTime,status=200,transport=rest)":0.011417577750000001,

"servicecomb.invocation(operation=huawei-cloud-ioccity-app-basemgmt.EquipmentInfoController.getEquipmentList,role=PRODUCER,stage=total,statistic=count,status=200,transport=rest)":0.016666666666666666

l output to the log file

配置项: cse.metrics.publisher.defaultLog.enabled=true

输出参考:(截取了性能部分)

producer:

    tps     latency(ms)   max-latency(ms) queue(ms) max-queue(ms) execute(ms) max-execute(ms) operation

    rest.200:

    0       0.000       727.756         0.000     23.542        0.000       704.214           huawei-cloud-ioccity-app-basemgmt.EquipmentInfoController.getEquipmentList  

common problem

In the new framework for business transformation, that they will encounter some problems. [Principle] (https://bbs.huaweicloud.com/blogs/ba7b62178cb811e89fc57ca23e93a89f) introduced the reform process may encounter problems, as well as some general patterns, you can read this article in order to assess the workload of transformation. Here are a few common problems encountered in the transformation of IoT systems and solutions.

Tripartite conflict pieces

When introducing a new framework or components, three pieces of conflict is a very common problem. Premise to solve the conflict tripartite pieces are compatible between versions. After many possible causes of conflict, to solve such problems is no fixed ideas, but mastered the reason maven dependency management skills and understanding of conflicts, often analyzing and solving such problems will be easier. Suggested Reading [maven management skills] (  http://servicecomb.apache.org/cn/docs/maven_dependency_management/ ).

SpringMVC data type support

CSE support SpringMVC label definitions REST interface, under normal circumstances, the application for transformation SpringMVC CSE will be very simple. But running CSE and SpringMVC different design goals are different, so there is still some local differences. The main difference between this point is defined using CSE REST interface, and annotation data types supported with respect SpringMVC fewer detailed description [Reference] (  https://docs.servicecomb.io/java-chassis/zh_CN/using- The chassis-in--Java Spring-Boot / the diff-Spring-mvc.html ).

For example, some of the following interfaces are defined in REST CSE is not in use or limited use.

import   org.springframework.data.domain.Page;

@GetMapping(params   = { "pageNumber", "pageSize" })

public ResponseEntity<Page<T>>   listByPagination(@RequestParam int pageNumber, @RequestParam int   pageSize)

The interface defined at the time, Page is an interface. CSE is not supported on a generic interface definition, comprising interface, abstract class and the like. CSE made the reason for this limitation is because the CSE to follow open API specification, there is a corresponding interface definition must be able to describe the swagger. If you use the interface and abstract class, so it can not be described by this interface swagger.

import   org.springframework.data.domain.Page;

@GetMapping(params   = { "pageNumber", "pageSize" })

public ResponseEntity<Page<T>>   listByPagination(@RequestParam int pageNumber, @RequestParam int   pageSize)

The interface defined at the time, Page is an interface. CSE is not supported on a generic interface definition, comprising interface, abstract class and the like. CSE made the reason for this limitation is because the CSE to follow open API specification, there is a corresponding interface definition must be able to describe the swagger. If you use the interface and abstract class, so it can not be described by this interface swagger.

CSE data type support [Description] (  https://docs.servicecomb.io/java-chassis/zh_CN/build-provider/interface-constraints.html )

l Solutions

Business code should be used reasonably hierarchical design, so that you can ensure that the code can be very flexible migration in different frameworks. If you use the Spring development, hierarchical design can follow some of the recommendations of Spring. Business logic implemented in @Service, the local interface is implemented in the framework @Controller. When (such as Servlet, Spring MVC, CSE, etc.) publish services in a different frame, simply adjust @Controller code @Service not need to modify the code. In the above example the interface.

@Service

@Service

public class MyService {

  public Page<T> findAll(Pageable   pageable)

}

Published as Spring MVC's Endpoint:

@RestController

public class   SpringMVCEndpoint {

  @Autowired

private MyService service;

 

  @PostMapping(value = "/search")

public ResponseEntity<Page<T>> findAll(@RequestParam   int pageNumber, @RequestParam int pageSize) {

    return   new ResponseEntity<Page<T>>(service.findAll(pageable),   HttpStatus.OK);

}

}

Publish as Servlet:

public class   ServletEndpoint extends HttpServlet {

  @Autowired

private MyService service;

 

void doPost(HttpServletRequest req, HttpServletResponse resp)   {

  //   extract request

service.findAll(request, HttpStatus.OK);

// send response

}

}

CSE published as an interface:

Class PageModel<T>   {

  private int totalPages;

  private int totalElements;

  private int number;

  private int size;

  private int numberOfElements;

  private List<T> content;

  private boolean first;

  private boolean last;

}

 

Class Record   {

  private long id;

}

 

@RestSchema(schemaId=   "CSEEndpoint")

@RequestMapping(value   = "/cseEndpoint")

public class   CSEEndpoint {

  @Autowired

private MyService service;

 

  @PostMapping(value = "/search")

public ResponseEntity<PageModel<Record>> findAll(@RequestParam   int pageNumber, @RequestParam int pageSize) {

  Page<Record>   pages = service.findAll(example);

  //   convert pages to PageModel

    PageModel<Page> result = ;

    return   new ResponseEntity<List<T>>(result, HttpStatus.OK);

}

}

Spring MVC is dependent on the specific mechanism of business logic

In our reform step two, removed DispatcherServletAutoConfiguration, namely Spring MVC REST provides related functionality has been removed. Spring MVC provides some interfaces need to rely on this feature, after the removal, which can cause some functions can not be used. such as:

import   org.springframework.web.context.request.RequestContextHolder;

import   org.springframework.web.context.request.ServletRequestAttributes;

@Before("webLog()")

public   void doBefore(JoinPoint joinPoint) throws Throwable {

    // 接收到请求,记录请求内容

    ServletRequestAttributes attributes =   (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

    HttpServletRequest request =   attributes.getRequest();

 

    // 记录下请求内容

log.info("URL : " +   request.getRequestURL().toString());

}

The above Servlet code acquisition request by RequestContextHolder, and to get some information from the request header to logging. Spring MVC REST RequestContextHolder depends on setting request context, after rectification, to obtain attributes is empty, resulting in NPE thrown exception.

l Solutions

This depends on the particular functions provided by the platform to perform this transformation, we need to combine the use of the platform to see if there are alternatives. This code is intended to achieve audit logs, or call chain logic, CSE provides Handler to obtain this information, and has achieved a chain of calls and other functions, it can be used directly, or achieve similar functionality through custom Handler, HttpFilter mechanisms .

Source: Huawei cloud community  Author: liubao68

 

 

 

 

Guess you like

Origin blog.csdn.net/devcloud/article/details/94631572