今天给大家分享一个华为的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,在浏览器中输入 地址进行访问,如图:
以上就是整个项目的配置了,有问题可以在评论区评论,技术问题可以私信我。