1.背景介绍
ServiceComb作为Apache开源组织下的一款微服务框架,其前身为华为云的微服务引擎CSE(Cloud Service Engine)云服务,它意味着为国内一款微服务在华为和Apache组织的共同努力,随着微服务市场的火爆,一定会让越来越多的开发者所喜欢。
2.首要原则
全国首款进入Apache的开源微服务项目,中立、开放、标准、无商业。Lock-in开源与商业代码同源,具备零成本平滑迁移商用的能力,社区长足发展有保障。
3.技术方案
解决方案级,多语言、多通信协议、标准服务契约、事务最终一致性开源开放,拥抱SpringBoot、SpringCloud、ServiceMesh等主流生态低门栏准入,业务入侵度低,架构松耦合。
4.官方网站介绍
华为将ServiceComb贡献给了Apache基金组织后,我们就可以通过Apache的官方网站提供的资料来学习ServiceComb。
英文:http://servicecomb.apache.org/
中文:http://servicecomb.apache.org/cn
5.开发环境准备
前台访问地址:http://127.0.0.1:30103,后台地址是:http://127.0.0.1:30100
JDK8
Maven3.6
IDEA
服务注册中心CSE:Cloud Service Engine云服务引擎
好处:可以轻松将微服务发布到云平台–华为云PaaS平台,用ServiceComb开发微服务,再借用华为云PaaS平台实现微服务治理
服务提供者:提供微服务功能
服务注册中心:注册微服务,实现微服务的管理
服务消费者:调用微服务
1.服务提供者向CSE注册中心注册一个微服务
2.服务消费者就可以向注册中心服务发现
3.如果在注册中心找到了要调用的微服务就可以发送一个微服务的实例给服务消费者
4.心跳:通过心跳的消息实现微服务状态检测
6.入门配置分析
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependencies>
<!-- hibernate校验规则 -->
<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>
<!-- springboot-junit整合进行单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<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>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<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>1.5.12.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<outputDirectory>target/bin</outputDirectory>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
microservice.yaml
APPLICATION_ID: start.servicecomb.io
service_description:
name: HelloServiceComb
version: 1.0.0
servicecomb:
handler:
chain:
Provider: { }
rest:
address: 0.0.0.0:9000
service:
registry:
address: http://127.0.0.1:30100
autodiscovery: false
7.Rest编程开发ServiceComb
7.3interface
//微服务定义接口
public interface RestService {
String sayRest(String name);
}
7.3provider
RestServiceImpl
@RequestMapping("/hello")
public class RestServiceImpl implements RestService {
@Override
@GetMapping("hello")
public String sayRest(String name){
return "Hello World"+name;
}
}
RestSpringBootApplication
@SpringBootApplication
@EnableServiceComb//向注册中心注册
public class RestSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(RestSpringBootApplication.class,args);
}
}
7.3consumer
RestConsumerController
@RestSchema(schemaId = "test")
@RequestMapping("/test")
public class RestConsumerController {
@Autowired
private RestService restService;
@GetMapping("/consumer")
public String sayRest(String name){
return restService.sayRest(name);
}
}
RestConsumerServiceImpl
public class RestConsumerServiceImpl implements RestService {
//RestTemplate模板
private final RestTemplate restTemplate = RestTemplateBuilder.create();//使用ServiceComb提供的类
@Override
public String sayRest(String name) {
String provideName = "provider";
String returnValue = restTemplate.getForObject("cse://" + provideName + "/hello/hello?name="+name, String.class);
return returnValue;
}
}
ConsumerApplication
@SpringBootApplication
@EnableServiceComb
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
8.总结
告辞!!!