SpringCloud应用集成Nacos和Dubbo
第一步:引入依赖
版本见 => 附录:根pom文件=>版本控制片段 www.cnblogs.com/anhaoyan...
<!-- Spring Boot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
第二步:配置 application.yml 文件
(1)服务提供者
server:
port: 8084
dubbo:
application:
name: nacos-dubbo-provider
protocol:
name: dubbo
port: -1
registry:
address: nacos://127.0.0.1:8848
(2)服务消费者
server:
port: 8085
dubbo:
application:
name: nacos-dubbo-consumer
protocol:
name: dubbo
port: -1
registry:
address: nacos://127.0.0.1:8848
第三步:启动类加@EnableDubbo
注解开启服务注册与发现
(1)服务提供者
package com.anhaoyang.test.nacos.dubbo.provider.start;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 服务提供者启动类
* 该类位于 boot-nacos-dubbo-provider 项目子模块 boot-nacos-dubbo-provider-start中
**/
@SpringBootApplication
@EnableDubbo //开启Dubbo
public class NacosDubboProviderStartApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDubboProviderStartApplication.class, args);
}
}
package com.anhaoyang.test.nacos.dubbo.provider.start;
import com.anhaoyang.test.nacos.dubbo.provider.api.NacosDubboProviderApi;
import org.apache.dubbo.config.annotation.DubboService;
/**
* 测试接口
* 该类位于 boot-nacos-dubbo-provider 项目子模块 boot-nacos-dubbo-provider-start中
**/
@DubboService
public class NacosDubboProviderApiImpl implements NacosDubboProviderApi {
@Override
public String sayHello(String name) {
return "Provider received: " + name;
}
}
package com.anhaoyang.test.nacos.dubbo.provider.api;
/**
* 服务提供者api定义
* 该接口位于 boot-nacos-dubbo-provider 项目子模块 boot-nacos-dubbo-provider-api 中
**/
public interface NacosDubboProviderApi {
String sayHello(String name);
}
(2)服务消费者
package com.anhaoyang.test.nacos.dubbo.consumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo //开启dubbo
public class NacosDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDubboConsumerApplication.class, args);
}
}
package com.anhaoyang.test.nacos.dubbo.consumer;
import com.anhaoyang.test.nacos.dubbo.provider.api.NacosDubboProviderApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 消费者调用测试
**/
@RestController
public class NacosDubboConsumerController {
//需依赖 服务提供者定义的接口,即 boot-nacos-dubbo-provider-api
@DubboReference
private NacosDubboProviderApi nacosDubboProviderService;
@GetMapping("/get")
public String get() {
return nacosDubboProviderService.sayHello("ConsumerBody");
}
}
第四步:验证
(1)启动服务后,Nacos控制台-服务列表
(2)服务消费者-> 访问rpc结果,成功。
标签:dubbo,Dubbo,SpringCloud,org,boot,nacos,Nacos,provider,import From: https://www.cnblogs.com/anhaoyang/p/springcloud-application-integrates-nacos-and-dubbo-1iq