【轉載請註明出處】:http://www.javashuo.com/article/p-fslfduzb-b.htmlweb
公司之前的系統都是基於Dubbo的分佈式架構,後來的新業務逐漸採用了SpringCloud微服務架構,在相互的業務調用中都是提供http接口供各自調用,可是總感受很彆扭,如今有了阿里開源的Nacos就能夠解決不少問題。最終要實現的效果就是同一個接口,既能夠用Dubbo的方式調用,也能夠用Feign來調用,覆蓋當前的這種場景。
spring
此時服務提供者即註冊了 Dubbo 又註冊了 Http 服務,服務消費者根據配置方式能夠在 Dubbo 與 Http 調用中隨意切換。apache
nacos-dubbo-cloud-provider-api
接口API作爲服務提供者和消費者的共同依賴,將接口不只暴露爲RestAPI,作爲Feign的客戶端,也按照Dubbo協議註冊。
依賴segmentfault
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
接口api
@FeignClient("nacos-dubbo-cloud-provider-service") public interface EchoRestDubboService { @GetMapping("/echoRestDubbo") String echo(@RequestParam String name); }
nacos-dubbo-cloud-provider-service
是服務提供者的應用名。架構
nacos-dubbo-cloud-provider-service
依賴app
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.eyison</groupId> <artifactId>nacos-dubbo-cloud-provider-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId <version>0.9.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>0.9.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency>
EchoRestDubboService
的實現類分佈式
@RestController @Service(version = "1.0.0", protocol = {"dubbo"}) public class EchoRestDubboServiceImpl implements EchoRestDubboService { private final Logger logger = LoggerFactory.getLogger(EchoRestDubboServiceImpl.class); @Override public String echo(String name) { logger.info("echo rest dubbo :{}", name); return "hello " + name; } }
WebSecurity
配置
這個必須得配置,不然Dubbo啓動的時候會報EndPoint的錯。ide
@EnableWebSecurity class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/**"); super.configure(web); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .requestMatchers(EndpointRequest.to("health", "info")).permitAll(); super.configure(http); } }
啓動類NacosDubboCloudProviderApplication
spring-boot
@SpringBootApplication @EnableDiscoveryClient public class NacosDubboCloudProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosDubboCloudProviderApplication.class, args); } }
配置
server: port: 8021 spring: application: name: nacos-dubbo-cloud-provider-service main: allow-bean-definition-overriding: true cloud: nacos: discovery: server-addr: 127.0.0.1:8848 dubbo: scan: base-packages: com.eyison.service protocols: dubbo: name: dubbo port: -1 registry: address: spring-cloud://localhost feign: hystrix: enabled: true
nacos-dubbo-cloud-consumer
依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.eyison</groupId> <artifactId>nacos-dubbo-cloud-provider-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.9.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>0.9.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency>
WebSecurity
配置類同上,這裏再也不重複。
測試類TestController
@RestController public class TestController { @Reference(version = "1.0.0", protocol = "dubbo") private EchoRestDubboService echoDubboService; @Autowired private EchoRestDubboService echoRestService; @GetMapping("/echoDubbo") String echoDubbo(String name) { return echoDubboService.echo(name); } @GetMapping("/echoRest") String echoRest(String name) { return echoRestService.echo(name); } }
啓動類
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class NacosDubboCloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosDubboCloudConsumerApplication.class, args); } }
配置
server: port: 8011 spring: application: name: nacos-dubbo-cloud-consumer main: allow-bean-definition-overriding: true cloud: nacos: discovery: server-addr: 127.0.0.1:8848 dubbo: application: qos: port: 33333 registry: address: spring-cloud://localhost cloud: subscribed-services: nacos-dubbo-cloud-provider-service logging: level: org.apache.dubbo: debug com.netflix: debug
依次啓動服務提供者和服務消費者,而後訪問消費者的兩個接口能夠看到都能成功,並且經過觀察日誌,分別是經過Dubbo的RPC和Feign調用的。
【轉載請註明出處】: http://www.javashuo.com/article/p-fslfduzb-b.html