【转载请注明出处】:https://segmentfault.com/a/1190000022909729
公司以前的系统都是基于Dubbo的分布式架构,后来的新业务逐渐采用了SpringCloud微服务架构,在相互的业务调用中都是提供http接口供各自调用,但是总感觉很别扭,现在有了阿里开源的Nacos就可以解决很多问题。最终要实现的效果就是同一个接口,既可以用Dubbo的方式调用,也可以用Feign来调用,覆盖当前的这种场景。
此时服务提供者即注册了 Dubbo 又注册了 Http 服务,服务消费者根据配置方式可以在 Dubbo 与 Http 调用中随意切换。
1、接口nacos-dubbo-cloud-provider-api
接口API做为服务提供者和消费者的共同依赖,将接口不仅暴露为RestAPI,做为Feign的客户端,也按照Dubbo协议注册。
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
接口
@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
依赖
<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的错。
@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
@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
3、服务消费者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调用的。
【转载请注明出处】: https://segmentfault.com/a/1190000022909729
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。