ServiceComb_day01

ServiceComb
1.ServiceCombo介紹:
	ServiceCombo是Apache下的一款微服務框架,它的前身是華爲雲服務引擎(Cloud Service Engine);
ServiceCombo在微服務開發上比起SpringCloud更勝一籌(ServiceCombo提供了分佈式事務解決方案);官
網:http://servicecomb.incubator.apache.org/
2.ServiceCombo設計思想:
	ServiceCombo主要由編程模型,運行模型和通信模型組成;由這三者的不同組合來形成完整的ServiceCombo
3.ServiceCombo快速開發腳手架:
	http://start.servicecomb.io/
4.ServiceComb運行模型:(直接使用腳手架生成)
* 負載均衡(LoadBalance): ServiceComb內置基於Ribbon的負載均衡
* 限流(FlowControl): ServiceComb基於Zuul網關實現限流
* 熔斷機制(CircuitBroker): 使用熔斷機制解決雪崩效應

在這裏插入圖片描述

ServiceCombo入門Demo–rest通信模型
1.編寫公共接口
public interface RestService{
	String satHello(String name);
}

2.編寫Provider
2.1編寫引導類,在引導類上加@EnableServiceComb註解
@EnableServiceComb		//開啓對ServiceCom的支持
@SpringBootApplication
public class ProviderApplication{
    public static void main(String[] args){
        SpringApplication.run(ProviderApplication.class);
    }
}
2.2編寫提供者實現類
//將這個類發佈到cse註冊中心並掃描到spring容器(@Component),schemaId不重複即可
@RestSchema(schemaId = "restServiceImpl")
@RequestMapping("/say")
public class RestServiceImpl implements RestService{
	@GetMapping("/hello")	 //consumer訪問provider也是基於rest風格,因此需要請求映射註解
    public String satHello(String name){
		return "Hello " + name;
    }
}
2.3編寫microservice.yaml,固定名稱
APPLICATION_ID: start.servicecomb.io	#應用名稱任意,但是provider和consumer必須保持一致
service_description:
  name: myProvider	#微服務名稱,供consumer引用
  version: 0.0.1
servicecomb:
  handler:
    chain:
      Provider: {}
  rest:
    address: 0.0.0.0:8888	#consumer訪問provider引用的IP和端口
  service:
    registry:
      address: http://127.0.0.1:30100	#cse註冊中心的IP和端口
      autodiscovery: false

3.編寫consumer
3.1編寫引導類
@EnableServiceComb		//開啓對ServiceCom的支持
@SpringBootApplication
public class ConsumerApplication{
    public static void main(String[] args){
        SpringApplication.run(ConsumerApplication.class);
    } 
    @Bean
    public RestTemplate restTemplate(){
    	//org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder
    	return RestTemplateBuilder.create();
    }
}
3.2編寫provider調用者,注意跟真正Provider的區別
@Service
public class RestServiceInvoker implements RestService{
	@Autowired
	private RestTemplate restTemplate;
	
	public String satHello(String name){
		String providerName = "myProvider";	 //跟service_description.name對應
		//url: cse協議 + 提供者名稱 + 提供者的rest訪問方式
		String url = "cse://" + providerName + "/say/hello?name=" + name;
    }
}
3.3編寫controller
@RestSchema( schemaId = "restController")
@RequestMapping("/test")
public class RestController{
	@Autowired
	private RestServiceInvoker restServiceInvoker;
    
    @ResponseBody	//可以省略
    @GetMapping("/testSay")
    public String sayHello(String name){
        return restServiceInvoker.satHello(name);
    }
}
3.4編寫microservice.yaml
APPLICATION_ID: start.servicecomb.io	
service_description:
  name: myConsumer	#微服務名稱
  version: 0.0.1
servicecomb:
  handler:
    chain:
      Provider: {}
  rest:
    address: 0.0.0.0:9999	#consumer供瀏覽器訪問的IP端口
  service:
    registry:
      address: http://127.0.0.1:30100	#cse註冊中心的IP和端口
      autodiscovery: false
          
測試:
	先打開cse註冊中心,雙擊service-center.exe(在官網下載);
	啓動provider,consumer(順序無所謂,consumer是在真正調用provider時纔去註冊中心發現服務)
    localhost:9999/test/testSay,在這種模式下,service也可以直接被瀏覽器訪問  

注意:
	1.controller類上也必須使用@RestSchema註解
	2.RestTemplate必須使用ServiceComb提供的實現   
pom.xml
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <!--1.實現pom文件導入-->
    <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>
	<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與web整合-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--service-interface-->
        <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>

注意:在install接口工程時,需要先將spring-boot-maven-plugin註釋掉,否則install時需要依賴引導類,而接口工程是沒有引導類的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章