Dubbo集成Nacos
一、Nacos配置安装
可查看:SpringCloud 集成 Nacos 使用小结
二、Provider工程
pom.xml
<?xml version=”1.0” encoding=”UTF-8”?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.0.RELEASE
com.example
dubbo_client
0.0.1-SNAPSHOT
dubbo_client
Demo project for Spring Boot
1.8
2.7.7
2.1.1.RELEASE
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-dependencies-bom
${dubbo.version}
pom
import
org.apache.dubbo
dubbo-spring-boot-starter
2.7.7
org.apache.curator
curator-recipes
2.13.0
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
${nacos.version}
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
${nacos.version}
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
com.example
dubbo_facade
1.0.0
compile
org.springframework.boot
spring-boot-maven-plugin
bootstrap.yml
spring:
application:name: dubbo_server
cloud:
nacos:
server-addr: 127.0.0.1:8848
config:
namespace: 9c6b8156-d045-463d-8fe6-4658ce78d0cc
file-extension: yml
dubbo:
protocol:name: dubbo
port: 20880
registry:
address: nacos://127.0.0.1:8848
application:
name: dubbo_server
consumer:
timeout: 3000
scan:
base-packages: com.example.dubbo.service
DubboServerApplication
package com.example.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableDubbo
@SpringBootApplication
public class DubboServerApplication {public static void main(String[] args) {
SpringApplication.run(DubboServerApplication.class, args);
}
}
package com.example.dubbo.service.impl;
import com.example.dubbo.facade.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;/* @author liuteng */
@Component
@DubboService(timeout = 500, version = “1.0.0”, interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {@Override
public String getUserIdByPhone(String phone) {
return "1000000101001";
}
}
三、Consumer工程
pom.xml
<?xml version=”1.0” encoding=”UTF-8”?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.0.RELEASE
com.example
dubbo_client
0.0.1-SNAPSHOT
dubbo_client
Demo project for Spring Boot
1.8
2.7.7
2.1.1.RELEASE
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-dependencies-bom
${dubbo.version}
pom
import
org.apache.dubbo
dubbo-spring-boot-starter
2.7.7
org.apache.curator
curator-recipes
2.13.0
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
${nacos.version}
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
${nacos.version}
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
com.example
dubbo_facade
1.0.0
compile
org.springframework.boot
spring-boot-maven-plugin
bootstrap.yml
server:
port: 9090spring:
application:name: dubbo_client
cloud:
nacos:
server-addr: 127.0.0.1:8848
config:
namespace: 9c6b8156-d045-463d-8fe6-4658ce78d0cc
file-extension: yml
dubbo:
application:name: dubbo_client
registry:
address: nacos://127.0.0.1:8848
scan:
base-packages: com.example.demo.controller
DubboClientApplication
package com.example.demo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableDubbo
@SpringBootApplication
public class DubboClientApplication {public static void main(String[] args) {
SpringApplication.run(DubboClientApplication.class, args);
}
}
UserController
package com.example.demo.controller;
import com.example.dubbo.facade.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/* @author liuteng */
@RestController
@RequestMapping(value = “user/“)
public class UserController {@DubboReference(version = "1.0.0")
private UserService userService;
@GetMapping(value = "/getUserIdByPhone")
public String getUserIdByPhone(String phone){
return userService.getUserIdByPhone(phone);
}
}
四、Facade工程
UserService
package com.example.dubbo.facade.service;
/* @author liuteng */
public interface UserService {String getUserIdByPhone(String phone);
}
五、工程结构
├─dubbo_client
├─src
│ ├─main
│ ├─java
│ │ └─com
│ │ └─example
│ │ └─demo
│ │ └─controller
│ │ └─UserController.java
│ └─resources
│ │ └─bootstrap.yml
├─dubbo_facade
├─src
│ └─main
│ ├─java
│ │ └─com
│ │ └─example
│ │ └─dubbo
│ │ └─facade
│ │ └─service
│ │ └─UserService.java
└─dubbo_server
├─src
│ └─main
│ ├─java
│ │ └─com
│ │ └─example
│ │ └─dubbo
│ │ └─service
│ │ └─impl
│ │ └─UserServiceImpl.java
│ └─resources
│ │ └─bootstrap.yml
六、Nacos平台
还没有评论,来说两句吧...