公司以前的系统都是基于Dubbo的分布式架构,后来的新业务逐渐采用了SpringCloud微服务架构,在相互的业务调用中都是提供http接口供各自调用,但是总感觉很别扭,现在有了阿里开源的Nacos就可以解决很多问题。最终要实现的效果就是同一个接口,既可以用Dubbo的方式调用,也可以用Feign来调用,覆盖当前的这种场景。
![a623f0d24f4b49ceb0aeb805e0d9cc9f.png](https://i-blog.csdnimg.cn/blog_migrate/7fc5f541020bc58625a19cd57a9eade8.jpeg)
此时服务提供者即注册了 Dubbo 又注册了 Http 服务,服务消费者根据配置方式可以在 Dubbo 与 Http 调用中随意切换。
1、接口nacos-dubbo-cloud-provider-api
接口API做为服务提供者和消费者的共同依赖,将接口不仅暴露为RestAPI,做为Feign的客户端,也按照Dubbo协议注册。依赖
org.springframework.cloud spring-cloud-starter-openfeign
接口
@FeignClient("nacos-dubbo-cloud-provider-service")public interface EchoRestDubboService { @GetMapping("/echoRestDubbo") String echo(@RequestParam String name);}
nacos-dubbo-cloud-provider-service是服务提供者的应用名。
2、服务提供者nacos-dubbo-cloud-provider-service
依赖
org.springframework.boot spring-boot-starter-webcom.eyison nacos-dubbo-cloud-provider-api 1.0-SNAPSHOTorg.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery0.9.0.RELEASEorg.springframework.cloud spring-cloud-starter-dubbo 0.9.0.RELEASEorg.springframework.boot spring-boot-starter-actuatororg.springframework.cloud spring-cloud-starter-security
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的错。
@EnableWebSecurityclass 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
@SpringBootApplication@EnableDiscoveryClientpublic class NacosDubboCloudProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosDubboCloudProviderApplication.class, args); }}
配置
server: port: 8021spring: application: name: nacos-dubbo-cloud-provider-service main: allow-bean-definition-overriding: true cloud: nacos: discovery: server-addr: 127.0.0.1:8848dubbo: scan: base-packages: com.eyison.service protocols: dubbo: name: dubbo port: -1 registry: address: spring-cloud://localhostfeign: hystrix: enabled: true
3、服务消费者nacos-dubbo-cloud-consumer
依赖
org.springframework.cloud spring-cloud-starter-openfeignorg.springframework.boot spring-boot-starter-webcom.eyison nacos-dubbo-cloud-provider-api 1.0-SNAPSHOTorg.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 0.9.0.RELEASEorg.springframework.cloud spring-cloud-starter-dubbo 0.9.0.RELEASEorg.springframework.boot spring-boot-starter-actuatororg.springframework.cloud spring-cloud-starter-security
WebSecurity配置类同上,这里不再重复。
测试类TestController
@RestControllerpublic 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@EnableFeignClientspublic class NacosDubboCloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosDubboCloudConsumerApplication.class, args); }}
配置
server: port: 8011spring: application: name: nacos-dubbo-cloud-consumer main: allow-bean-definition-overriding: true cloud: nacos: discovery: server-addr: 127.0.0.1:8848dubbo: application: qos: port: 33333 registry: address: spring-cloud://localhost cloud: subscribed-services: nacos-dubbo-cloud-provider-servicelogging: level: org.apache.dubbo: debug com.netflix: debug
依次启动服务提供者和服务消费者,然后访问消费者的两个接口可以看到都能成功,而且通过观察日志,分别是通过Dubbo的RPC和Feign调用的。
![34e07ad667770f0e506288f0ac74b19e.png](https://i-blog.csdnimg.cn/blog_migrate/d893b97e69cb6664112cbf13895c7bb7.jpeg)