serviceComb学习笔记

##ServiceComb学习笔记

1.下载并安装SDK

 //使用git下载,需要先安装git
 git clone https://github.com/apache/servicecomb-java-chassis.git 
 cd servicecomb-java-chassis
 //将sdk安装的本地仓库
 mvn clean install -DskipTests

2.rest方式例子

1.创建父工程

  • pom文件内容

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.zjbgroupId>
        <artifactId>servicecombtestartifactId>
        <packaging>pompackaging>
        <version>1.0-SNAPSHOTversion>
        <modules>
            <module>service-interfacemodule>
            <module>service-providermodule>
            <module>service-customermodule>
        modules>
        
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>1.5.12.RELEASEversion>
            
            <relativePath/>
        parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
            <java.version>1.8java.version>
        properties>
    
        
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.apache.servicecombgroupId>
                    <artifactId>java-chassis-dependenciesartifactId>
                    <version>1.0.0-m2version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
    project>
    

2.创建接口类

  • pom文件不需要添加任何内容

  • 创建一个名字为RpcService的接口

    package com.zjb.service;
    
    /**
     * @author zhaojianbo
     * @date 2019/7/14 16:50
     */
    public interface RestService {
        String sayHello(String name);
    }
    
  • mvn install 命令将该类安装到本地仓库中,供其他模块使用

3.服务提供者模块

  • pom文件内容

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>servicecombtestartifactId>
            <groupId>com.zjbgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>service-providerartifactId>
    
        <dependencies>
            
            <dependency>
                <groupId>org.hibernategroupId>
                <artifactId>hibernate-validatorartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-restartifactId>
            dependency>
            
            <dependency>
                <groupId>org.apache.servicecombgroupId>
                <artifactId>spring-boot-starter-providerartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>com.zjbgroupId>
                <artifactId>service-interfaceartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
        dependencies>
    project>
    
  • RestServiceImpl类的内容

    package com.zjb.impl;
    
    import com.zjb.service.RestService;
    import org.apache.servicecomb.provider.rest.common.RestSchema;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @author zhaojianbo
     * @date 2019/7/14 16:59
     */
    @RestSchema(schemaId = "provider")
    public class RestServiceImpl implements RestService {
        @Override
        @GetMapping("/hello")
        public String sayRest(String name) {
            return "hello world " + name;
        }
    }
    
  • microservice.yaml文件的内容

    APPLICATION_ID: zjb.servicecomb.io
    service_description:
      name: provider
      version: 0.0.1
    servicecomb:
      handler:
        chain:
          Provider: {}
      rest: #rest通信
        address: 0.0.0.0:9080
      service:
        registry:
          address: http://127.0.0.1:30100 #服务注册中心的地址
          autodiscovery: false #是否可以自动发现
    
  • spring启动类,略

4.服务消费者

  • pom文件内容

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>servicecombtest</artifactId>
            <groupId>com.zjb</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>service-customer</artifactId>
    
        <dependencies>
            <!--校验规则-->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
            </dependency>
            <!--rest的支持-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
            </dependency>
            <!--servicecomb-->
            <dependency>
                <groupId>org.apache.servicecomb</groupId>
                <artifactId>spring-boot-starter-provider</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>com.zjb</groupId>
                <artifactId>service-interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </project>
    
  • RestConsumerServiceImpl类

    package com.zjb.service.impl;
    
    import com.zjb.service.RestService;
    import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author zhaojianbo
     * @date 2019/7/14 17:16
     */
    @Service
    public class RestConsumerServiceImpl implements RestService {
        //创建restTemplate模板
        private final RestTemplate restTemplate = RestTemplateBuilder.create();
    
        @Override
        public String sayRest(String name) {
            //微服务名称与提供者配置文件中的name对应
            String provideName = "provider";
            //cse是协议,类似dubbo://
            String returnResult = restTemplate.getForObject("cse://" + provideName + "/hello?name=" + name, String.class);
            return returnResult;
        }
    }
    
  • RestConsumerController类

    package com.zjb.controller;
    
    import com.zjb.service.RestService;
    import org.apache.servicecomb.provider.rest.common.RestSchema;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @author zhaojianbo
     * @date 2019/7/14 17:22
     */
    @RestSchema(schemaId = "customer")
    public class RestConsumerController {
        @Autowired
        private RestService restService;
        @GetMapping("/hello")
        public String sayRest(String name) {
            return restService.sayRest(name);
        }
    }
    
  • microservice.yaml

    APPLICATION_ID: zjb.servicecomb.io
    service_description:
      name: customer
      version: 0.0.1
    servicecomb:
      handler:
        chain:
          Provider: {}
      rest: #rest通信
        address: 0.0.0.0:9081
      service:
        registry:
          address: http://127.0.0.1:30100 #服务注册中心的地址
          autodiscovery: false #是否可以自动发现
    
  • springBoot启动类,略

5.启动过程

  • 启动注册中心,如果没有则下载ServiceComb Service-Center进到目录中运行service-center.exe(window平台下,使用默认端口,修改则进人conf中修改配置文件)
  • 启动提供者
  • 启动消费者

你可能感兴趣的:(后端,java,ServiceComb)