spring-cloud-invoker-parent
介紹
一個簡單輕量的rpc框架,不使用任何註冊中心。
目前spring-cloud-starter-invoker使用http方式引用htppInvoker模塊實現service暴露,從而實現遠程調用功能。
並基於openFeign實現loadbalancer基本原理,獲取註冊中心“服務模塊”IP地址。同時也支持項目中,沒有註冊中心場景下的項目。
軟件架構
maven
- pom.xml 添加如下代碼
<!-- https://mvnrepository.com/artifact/com.i360day/spring-cloud-starter-invoker --> <dependency> <groupId>com.i360day</groupId> <artifactId>spring-cloud-starter-invoker</artifactId> <version>2.0.0</version> </dependency>
使用說明1、 啓動類
@SpringBootApplication
@EnableDiscoveryClient
@EnableRemoteClients
@ComponentScan(basePackages={"com.xxx"})
public class TestBootstrap {
public static void main(String[] args) {
final ConfigurableApplicationContext run = SpringApplication.run(TestBootstrap.class);
}
}
2、 暴露service
@RemoteService
public class TestService implements TestServiceFacade {
public String test(){
return "ok";
}
}
3、 添加接口與熔斷,並指向模塊調用註冊中心地址進行遠程訪問
3.1) openFeign下使用,如果沒有項目名(projectPath)則爲空
@RemoteClient(name = "cx-test",fallback = TestServiceFacadeHystrix.class, projectPath = "/projectName")
public interface TestServiceFacade {
public String test();
}
public class TestServiceFacadeHystrix implements TestServiceFacade{
public String test(){
return "ok";
}
}
3.2) 非openFeign下使用,如果沒有項目名(projectPath)則爲空
@RemoteClient(address = "127.0.0.1:8080",fallback = TestServiceFacadeHystrix.class, projectPath = "/projectName")
public interface TestServiceFacade {
public String test();
}
public class TestServiceFacadeHystrix implements TestServiceFacade{
public String test(){
return "ok";
}
}
4、 客服端使用接口
@RestController
@RequestMapping("/test")
public class ChatController {
@Autowired
private TestServiceFacade testServiceFacade;
@RequestMapping("/test")
public String test(){
return testServiceFacade.test();
}
}