ServiceCombo入门Demo–rpc通信模型
1.编写公共接口
public interface RpcService{
String satHello(String name);
}
2.编写Provider
2.1编写引导类,在引导类上加@EnableServiceComb注解
@EnableServiceComb
@SpringBootApplication
public class ProviderApplication{
public static void main(String[] args){
SpringApplication.run(ProviderApplication.class);
}
}
2.2编写提供者实现类
@RpcSchema( schemaId = "rpcProviderServiceImpl")
public class RpcProviderServiceImpl implements RpcService{
public String satHello(String name){
return "hello world " + name;
}
}
2.3编写microservice.yaml,固定名称
APPLICATION_ID: start.servicecomb.io
service_description:
name: service-provider-rpc
version: 0.0.1
properties:
allowCrossApp: true #允许跨域
servicecomb:
handler:
chain:
Provider: {}
highway:
address: 0.0.0.0:9090 #rpc通信的端口不能重复,注意:不是tomcat访问端口
service:
registry:
address: http://127.0.0.1:30100
autodiscovery: false
3.编写consumer
3.1编写引导类
@EnableServiceComb
@SpringBootApplication
public class ConsumerApplication{
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class);
}
}
3.2编写provider调用者
@Service
public class RpcConsumerServiceImpl RpcService{
@RpcReference(
microserviceName = "start.servicecomb.io:service-provider-rpc",
schemaId = "rpcProviderServiceImpl" )
private RpcProviderServiceImpl rpcProviderServiceImpl;
public String satHello(String name){
return RpcReference.satHello(name);
}
}
3.3编写controller
@RestController
@RequestMapping("/test")
public class RestController{
@Autowired
private RpcConsumerServiceImpl rpcConsumerServiceImpl;
@ResponseBody
@GetMapping("/testSay")
public String sayHello(String name){
return rpcConsumerServiceImpl.sayHello(name);
}
}
3.4编写microservice.yaml
APPLICATION_ID: start.servicecomb.io
service_description:
name: service-consumer-rpc
version: 0.0.1
properties:
allowCrossApp: true
servicecomb:
handler:
chain:
Provider: {}
highway:
address: 0.0.0.0:9091
service:
registry:
address: http://127.0.0.1:30100
autodiscovery: false
3.5编写application.yaml
server:
port: 8080 #指定tomcat端口
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies</artifactId>
<version>1.0.0-m2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<version>5.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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.baidu</groupId>
<artifactId>service-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
微服务上云
1.云计算的三种模式:
* IaaS:Infrastructure-as-a-Service(基础设施即服务),也叫Hardware-as-a-Service,将计算机硬件资源打包对外提供服务,比如云主机、云存储等。
* PaaS:Platform-as-a-Service(平台即服务),也叫中间件服务,比如:MySQL数据库服务、ElasticSearch搜索服
务等。
* SaaS:Software-as-a-Service(软件即服务) ,提供具体应用软件服务,比如:CRM系统,电商平台等。
2.ServiceComb微服务绑定CSE云服务
第一步:在父工程pom.xml中导入依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.huawei.paas.cse</groupId>
<artifactId>cse-dependency</artifactId>
<version>2.3.20</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
第二步:在服务提供方(Provider)导入依赖:
<dependency>
<groupId>com.huawei.paas.cse</groupId>
<artifactId>cse-solution-service-engine</artifactId>
</dependency>
第三步:修改所有的microservice.yaml
APPLICATION_ID: helloworld
service_description:
name: helloworld-provider
version: 1.0.0
cse:
service:
registry:
address: https://cse.cn-north-1.myhuaweicloud.com #华为云CSE注册中心,固定值
instance:
watch: false
credentials:
accessKey: TMOWBPO1WDEB5XDZFFH5 #凭证,访问秘钥key
secretKey: drmZ4K33REEZ2AAM0oDMhsbWA1CYkttpSfFJzMv #凭证,访问秘钥value
akskCustomCipher: default
rest:
address: 127.0.0.1:8080 #浏览器访问IP端口
3.SpringCloud微服务绑定到CSE云服务
第一步:在父工程pom.xml中导入依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.huawei.paas.cse</groupId>
<artifactId>cse-dependency</artifactId>
<version>2.3.20</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
第二步:删除原SpringCloud项目中的eureka依赖,并导入ServiceComb框架的依赖
<!-- 导入Eureka服务的依赖 -->
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> -->
<!-- 导入ServiceComb服务的依赖 -->
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
第三步:删除原SpringCloud项目中的application.yaml中的关于eureka注册中心以及ribbon的配置,并重新配置microservice.yaml
APPLICATION_ID: xc-edu-cloud-test1.0
service_description:
name: xc-service-learning
version: 1.0.0
properties:
allowCrossApp: false
cse:
service:
registry:
address: https://cse.cn-north-1.myhuaweicloud.com
instance:
watch: false
config:
client:
serverUri: https://cse.cn-north-1.myhuaweicloud.com
refreshMode: 1 #默认为定时拉取
refresh_interval: 30 #客户端主动从配置中心拉取配置的周期 单位秒
handler:
chain:
Provider:
default: bizkeeper-provider,qps-flowcontrol-provider
Consumer:
default: bizkeeper-consumer,loadbalance,qps-flowcontrol-consumer
credentials:
accessKey: TMOWBPO1WDEB5XDZFFH5
secretKey: drmZ4K33REEZ2AAM0oDMhsbWA1CYkttpSfFJzMv
akskCustomCipher: default
rest:
address: 127.0.0.1:40600
第五步:将原SpringCloud项目中的所有的Feign的远程调用Api换成ServiceComb提供的Api;如;
@RestSchema(schemaId="testApi")....
第六步:删除引导类上的@EnableDiscoveryClient,@EnableFeignClients;重新添加@EnableServiceComb
第七步:使用ServiceComb提供的Egde Service替代SpringCloud微服务中的Zuul;自定义一个类实现HtpServletFilter,使用SPI(Service Provider Interfaces)的方式启动过滤器,在resources目录下创建一个META-INF目录,创建文件并以过滤器的全类名作为文件名,文件中的内容也是过滤器的全类名
# 路由配置
http:
dispatcher:
edge:
default:
enabled: true
prefix: api #请求url的前缀
withVersion: false
prefixSegmentCount: 2
url:
enabled: true
mappings:
xc-service-search:
prefixSegmentCount: 2
path: "/openapi/search/.*"
withVersion: false
microserviceName: xc-service-search
# versionRule: 1.0.0-2.0.0
xc-service-portalview:
prefixSegmentCount: 2
path: "/openapi/portalview/.*"
withVersion: false
microserviceName: xc-service-portalview
# versionRule: 1.0.0-2.0.0
第八步:上传微服务镜像到云容器引擎CCE(Cloud Container Engine)