Dubbo SpringCloud Nacos
入门案例会基于SpringCloudAlibaba 企业级应用实战:基于Nacos 实现服务发现工程来实现。
public interface IAccountService {
public Object register(String name);
}
Dubbo 服务接口是服务提供方与消费方的远程通讯契约,为了确保契约的一致性,推荐的做法是将 Dubbo 服务接口打包在第二方或者第三方的 artifact(jar)中。上述接口,存放于sca-account-api
工程中。dubbo
服务提供方需要引入sca-account-api
依赖,并实现服务接口,dubbo
服务消费者同样需要引入sca-account-api
依赖,并以接口调用的方式实现远程方法调用。
dubbo
服务提供方是一个独立微服务工程,需要引入sca-account-api
依赖,并实现dubbo
服务接口。本例中sca-account-service
作为dubbo
服务提供方。
cn.tyrone.sca
sca-account-api
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-dubbo
@org.apache.dubbo.config.annotation.Service
public class AccountServiceImpl implements IAccountService {
public Object register(String name) {
return name;
}
}
server.port=7000
spring.application.name=sca-account-service
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.main.allow-bean-definition-overriding=true
management.endpoints.web.exposure.include=*
dubbo.scan.base-packages=cn.tyrone.sca
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost
配置文件说明:
当前 Dubbo Spring Cloud 实现必须配置 dubbo.registry.address = spring-cloud://localhost,下一个版本将其配置变为可选。当前版本:
2.1.1.RELEASE
。
该值在 Dubbo Spring Cloud 加持下被视作 dubbo.application.name,因此,无需再显示地配置 dubbo.application.name
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ScaAccountServiceApplication implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(this.getClass());
public static void main(String[] args) {
SpringApplication.run(ScaAccountServiceApplication.class, args);
}
public void run(String... args) throws Exception {
logger.info("-------------- 账户应用服务提供者-启动成功 --------------");
}
}
dubbo
服务消费方是一个独立微服务工程,需要引入sca-account-api
依赖,并通过接口调用方式调用远程接口。本例中sca-account-consumer
作为dubbo
服务提供方。
cn.tyrone.sca
sca-account-api
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-dubbo
server.port=7001
spring.application.name=sca-account-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.main.allow-bean-definition-overriding=true
dubbo.scan.base-packages=cn.tyrone.sca
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost
dubbo.cloud.subscribed-services=sca-account-service
配置说明:
服务消费方配置增加dubbo.cloud.subscribed-services
属性,属性说明如下
import cn.tyrone.sca.account.service.IAccountService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@RestController
public class AccountConsumerController {
@Reference private IAccountService accountService;
@GetMapping("/register")
public Object register(String name){
Object object = accountService.register(name);
return object;
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class AccountConsumerApplication implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(this.getClass());
public static void main(String[] args) {
SpringApplication.run(AccountConsumerApplication.class, args);
}
public void run(String... args) throws Exception {
logger.info("-------------- 账户应用服务消费者-启动成功 --------------");
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Nacos
服务发现Dubbo
服务提供方和Dubbo
服务消费方。服务启动成功后,可以在Nacos
控制台看到服务列表,如下图:
浏览器输入:http://localhost:7001/register?name=tyrone
。HTTP
的响应是name
的值:tyrone。如下图:
参考链接:https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/README_CN.md
源代码链接:https://github.com/myNameIssls/spring-cloud-alibaba-study/tree/master/sca-dependences/sca-account