【轉(zhuǎn)載請(qǐng)注明出處】:http://www.reibang.com/p/9a00838152fe
公司以前的系統(tǒng)都是基于Dubbo的分布式架構(gòu)烟阐,后來(lái)的新業(yè)務(wù)逐漸采用了SpringCloud微服務(wù)架構(gòu)蜒茄,在相互的業(yè)務(wù)調(diào)用中都是提供http接口供各自調(diào)用,但是總感覺(jué)很別扭檀葛,現(xiàn)在有了阿里開(kāi)源的Nacos就可以解決很多問(wèn)題。最終要實(shí)現(xiàn)的效果就是同一個(gè)接口空扎,既可以用Dubbo的方式調(diào)用,也可以用Feign來(lái)調(diào)用盘寡,覆蓋當(dāng)前的這種場(chǎng)景撮慨。
此時(shí)服務(wù)提供者即注冊(cè)了 Dubbo 又注冊(cè)了 Http 服務(wù),服務(wù)消費(fèi)者根據(jù)配置方式可以在 Dubbo 與 Http 調(diào)用中隨意切換菇曲。
1抚吠、接口nacos-dubbo-cloud-provider-api
接口API做為服務(wù)提供者和消費(fèi)者的共同依賴弟胀,將接口不僅暴露為RestAPI,做為Feign的客戶端萧朝,也按照Dubbo協(xié)議注冊(cè)夏哭。
依賴
<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
是服務(wù)提供者的應(yīng)用名。
2何址、服務(wù)提供者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
的實(shí)現(xiàn)類
@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
配置
這個(gè)必須得配置用爪,否則Dubbo啟動(dòng)的時(shí)候會(huì)報(bào)EndPoint的錯(cuò)胁镐。
@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);
}
}
啟動(dòng)類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盯漂、服務(wù)消費(fèi)者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
配置類同上就缆,這里不再重復(fù)。
測(cè)試類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);
}
}
啟動(dòng)類
@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
依次啟動(dòng)服務(wù)提供者和服務(wù)消費(fèi)者阿弃,然后訪問(wèn)消費(fèi)者的兩個(gè)接口可以看到都能成功渣淳,而且通過(guò)觀察日志,分別是通過(guò)Dubbo的RPC和Feign調(diào)用的入愧。
【轉(zhuǎn)載請(qǐng)注明出處】: http://www.reibang.com/p/9a00838152fe