今天給大家分享一個華爲的ServiceComb框架寫的微服務,過多的話不說了,直接上乾貨部分。
ServiceComb需要有cse的註冊中心,這裏介紹如何使用docker快速的搭建一個。
docker pull servicecomb/service-center
docker run -d -p 30100:30100 servicecomb/service-center:latest
搭建成功後會在瀏覽器訪問會有以上的輸出,表明註冊中心搭建成功。項目目錄如下:
先來看看service-interfaces的部分
其中RpcService如下:
package com.yuxuan.service;
/**
*
*/
public interface RpcService {
public String sayRpc(String name);
/**
* 轉成大寫字母
* @param name
* @return
*/
public String strToUpper(String name);
}
interfaces項目中只有這一個接口類,這個項目主要用來放置公共的部分。
下面我們來看provider項目,服務的提供者:
RpcProviderApp類如下:
package com.yuxuan;
import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableServiceComb
public class RpcProviderApp {
public static void main(String[] args) {
SpringApplication.run(RpcProviderApp.class, args);
}
}
RpcProviderServiceImpl如下:
package com.yuxuan.provider;
import com.yuxuan.service.RpcService;
import org.apache.servicecomb.provider.pojo.RpcSchema;
@RpcSchema(schemaId = "helloRpcSrv")
public class RpcProviderServiceImpl implements RpcService {
@Override
public String sayRpc(String name) {
return "name " + name;
}
@Override
public String strToUpper(String name) {
return name == null ? "" : name.toUpperCase();
}
}
application.properties 如下:
server.port=9981
microservice.yaml如下:
APPLICATION_ID: start.servicecomb.io
service_description:
name: service-prvider-rpc
version: 0.0.1
servicecomb:
handler:
chain:
Provider: {}
rest:
address: 0.0.0.0:9080
service:
registry:
address: http://127.0.0.1:30100
autodiscovery: false #是否自動配置
pom.xml配置:
<?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>service-comb-t</artifactId>
<groupId>com.yuxuan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-pojo</artifactId>
</dependency>
<!--服務接口-->
<dependency>
<groupId>com.yuxuan</groupId>
<artifactId>service-interfaces</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
下面我們來看 consumer 消費者項目:
RestConsumerApp如下:
package com.yuxuan;
import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableServiceComb
public class RestConsumerApp {
public static void main(String[] args) {
SpringApplication.run(RestConsumerApp.class, args);
}
}
RestConsumerServiceImpl如下:
package com.yuxuan.service.impl;
import com.yuxuan.service.RpcService;
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.springframework.stereotype.Component;
/**
* rpc服務消費放
*/
@Component
public class RestConsumerServiceImpl implements RpcService {
//從註冊中心找
@RpcReference(schemaId = "helloRpcSrv",microserviceName = "service-prvider-rpc")
private RpcService rpcService;
@Override
public String sayRpc(String name) {
return rpcService.sayRpc(name);
}
@Override
public String strToUpper(String name) {
return rpcService.strToUpper(name);
}
}
RpcController如下:
package com.yuxuan.controller;
import com.yuxuan.service.RpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 對外發布的恐嚇類
*/
@RestController
public class RpcController {
@Autowired
private RpcService rpcService;
@RequestMapping(value = "rpc",method = RequestMethod.GET)
public void rpcInvoke(){
System.out.println(rpcService.sayRpc("rpc servicecomb "));
}
@RequestMapping(value = "upper",method = RequestMethod.GET)
public String strToUpper(String str){
return rpcService.strToUpper(str);
}
}
application.properties
server.port=9980
microservice.yaml
APPLICATION_ID: start.servicecomb.io
service_description:
name: rpc-servicecomb-consumer-rpc
version: 0.0.1
servicecomb:
handler:
chain:
Provider: {}
rest:
address: 0.0.0.0:9081
service:
registry:
address: http://127.0.0.1:30100
autodiscovery: false #是否自動配置
pom.xl
<?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>service-comb-t</artifactId>
<groupId>com.yuxuan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>server-consumer</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<!--RPC通信模型-->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<!--RPC編程模型-->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-pojo</artifactId>
</dependency>
<!--服務接口-->
<dependency>
<groupId>com.yuxuan</groupId>
<artifactId>service-interfaces</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
至此consumer項目完了,下面我們看看父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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yuxuan</groupId>
<artifactId>service-comb-t</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>service-interfaces</module>
<module>service-provider</module>
<module>server-consumer</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies-springboot2</artifactId>
<version>1.2.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- <version>2.1.2.RELEASE</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <goals>-->
<!-- <goal>repackage</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <outputDirectory>target/bin</outputDirectory>-->
<!-- <classifier>exec</classifier>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-jar-plugin</artifactId>-->
<!-- <version>2.6</version>-->
<!-- <configuration>-->
<!-- <archive>-->
<!-- <manifestEntries>-->
<!-- <Class-Path>.</Class-Path>-->
<!-- </manifestEntries>-->
<!-- </archive>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>
</project>
接下來先運行 provider,在運行consumer,在瀏覽器中輸入 地址進行訪問,如圖:
以上就是整個項目的配置了,有問題可以在評論區評論,技術問題可以私信我。