Follow Me CSE Series Part 3: Develop Your First CSE Microservice

Overview

Combined with the microservice framework, develop the first microservice HelloWorld application.

Through this example, you will learn:

  • Define a remote service interface
  • provider publishes remote service to registry
  • Consumer automatically discovers remote services and completes service calls

Steps

Step 1 Configure maven

The CSE release is in the maven repository provided by HUAWEI CLOUD. You need to specify the corresponding repository in the maven setting file so that CSE-related dependency packages can be downloaded.

<mirror>
  <id>mirrorId</id>
  <mirrorOf>*</mirrorOf>
  <name>Mirror of central repository.</name>
  <url>http://maven.huaweicse.com/nexus/content/groups/public</url>
</mirror>

Step 2 Introduce POM dependency package

Modify the pom file of the project and introduce the corresponding microservice framework dependency package.

<dependencyManagement> 
  <dependencies> 
    <dependency> 
      <groupId>com.huawei.paas.cse</groupId>  
      <artifactId>cse-dependency</artifactId>  
      <version>2.3.12</version>  
      <type>pom</type>  
      <scope>import</scope> 
    </dependency> 
  </dependencies> 
</dependencyManagement>

<dependencies> 
  <dependency> 
    <groupId>com.huawei.paas.cse</groupId>  
    <artifactId>cse-solution-service-engine</artifactId> 
  </dependency>
</dependencies>

Step 3 Modify the configuration.

1. Configure microservice information

Create the microservice.yaml file in the src/main/resources directory to store the sdk configuration information.

APPLICATION_ID: helloTest

service_description:
  name: helloClient
  version: 0.0.1
cse:
  service:
    registry:
      address: http://127.0.0.1:30100

2. Log file configuration (optional)

Create the config directory under the src/main/resources directory, add the log4j.hello.properties configuration file, and the log configuration file naming rule is log4j.*.properties, as follows.

paas.logs.dir=../logs/ #日志文件目录
paas.logs.file=hello.log #日志文件名
log4j.rootLogger=INFO,paas,stdout

Step 4 Define the service interface: (This interface needs to be packaged separately and shared between the service provider and the consumer)

HelloWorldService.java

Write the business interface according to the contract, as follows:

public interface HelloWorldService{
  String sayHello(String name);
}

Step 5Provider implementation

  • The service provider implements the interface (the implementation is hidden from the service consumer):
@RpcSchema(schemaId="hello")
public class HelloWorldServiceImpl implements HelloWorldService{
  public String sayHello(String name) {
    return "hello " + name;
  }
}
  • Write the Main startup function:
public class Provider{
  public static void main(String[] args) throws Exception {
    Log4jUtils.init(); //日志初始化
    BeanUtils.init(); //Spring初始化
  }
}

Step 6 Consumer implementation

  • service call declaration

Declare the following in the file META-INF/spring/hello.bean.xml:

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:cse="http://www.huawei.com/schema/paas/cse/rpc" xsi:schemaLocation="http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd http://www.huawei.com/schema/paas/cse/rpc classpath:META-INF/spring/spring-paas-cse-rpc.xsd">  
  <cse:rpc-reference id="helloworld" schema-id="helloworld" microservice-name="helloServer"></cse:rpc-reference> 
</beans>
  • service call

Consumer.java

public class Consumer{
   public static void main(String[] args) throws Exception {
     Log4jUtils.init(); #日志初始化
     BeanUtils.init(); # Spring bean初始化
     HelloWorld hello = BeanUtils.getBean("helloworld")#服务调用
     System.out.println(hello.sayHello("world"));
  }
}

Step 7 Service Registration

  • Start Service Center

The installation package of the service center stand-alone version is cse-service-center-standalone.tar.gz, which only supports 64-bit. After decompression, click the start.bat script in the installation package to start the service center (run start. sh).

  • Using HUAWEI CLOUD Online Middleware

Developers only need to register as a HUAWEI CLOUD user and obtain the corresponding AK and SK certification information, and then they can use the online service directly, saving the trouble of local service. You can also monitor your own service status online. To use online services, you only need to configure the connection information to the corresponding domain name, and set AK and SK:

cse:
  service:
    registry:
      address: https://cse.cn-north-1.myhwclouds.com:443
      instance:
        watch: false
  config:
    client:
      serverUri: https://cse.cn-north-1.myhwclouds.com:443
      refreshMode: 1
      refresh_interval: 5000
  monitor:
    client:
      serverUri: https://cse.cn-north-1.myhwclouds.com:443
  credentials:
    accessKey: your access key
    secretKey: your secret key
    akskCustomCipher: default

华为云微服务引擎 CSE ,截止到2018630 限时免费

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325828928&siteId=291194637