springboot接入dubbo-nacos注册中心
项目结构:
pro-parent
parent.pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <artifactId>pro-parent</artifactId> <groupId>removeif.io.java</groupId> <version>1.0-SNAPSHOT</version> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <modules> <module>service-api</module> <module>service-provider</module> <module>service-consumer</module> </modules> </project>
|
暴露服务接口api
service-api.pom
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>pro-parent</artifactId> <groupId>removeif.io.java</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service-api</artifactId> </project>
|
service-api.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package io.github.removeif.springboot.api;
import io.github.removeif.springboot.dto.UserDTO;
public interface UserRpcService {
UserDTO get(Integer id);
}
package io.github.removeif.springboot.dto;
import java.io.Serializable;
public class UserDTO implements Serializable {
private Integer id;
private String name;
private Integer gender;
public Integer getId() { return id; }
public UserDTO setId(Integer id) { this.id = id; return this; }
public String getName() { return name; }
public UserDTO setName(String name) { this.name = name; return this; }
public Integer getGender() { return gender; }
public UserDTO setGender(Integer gender) { this.gender = gender; return this; } }
|
服务提供方service-provider
service-provider.pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>service-provider</artifactId> <version>1.0-SNAPSHOT</version>
<dependencies> <dependency> <artifactId>service-api</artifactId> <groupId>removeif.io.java</groupId> <version>1.0-SNAPSHOT</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.3.RELEASE</version> </dependency>
</dependencies>
</project>
|
service-provider.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| spring: application: name: user-service-provider
dubbo: application: name: user-service-provider registry: address: nacos://127.0.0.1:8848 group: dev parameters[namespace]: dev protocol: port: -1 name: dubbo provider: timeout: 1000 UserRpcService: version: 1.0.0 scan: base-packages: io.github.removeif.springboot.service
|
group以及namespace对应nacos中结构:
service-provider.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package io.github.removeif.springboot.service;
import io.github.removeif.springboot.api.UserRpcService; import io.github.removeif.springboot.dto.UserDTO; import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "${dubbo.provider.UserRpcService.version}") public class UserRpcServiceImpl implements UserRpcService {
@Override public UserDTO get(Integer id) { return new UserDTO().setId(id) .setName("没有昵称:" + id) .setGender(id % 2 + 1); }
}
package io.github.removeif.springboot;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @EnableDubbo public class ProviderApplication {
public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }
}
|
消费方service-consumer
service-consumer.pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>service-consumer</artifactId> <version>1.0-SNAPSHOT</version>
<dependencies> <dependency> <artifactId>service-api</artifactId> <groupId>removeif.io.java</groupId> <version>1.0-SNAPSHOT</version> </dependency>
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.3.RELEASE</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.2.3.RELEASE</version> </dependency>
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.3.RELEASE</version> </dependency> </dependencies>
</project>
|
service-consumer.ymal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| spring: application: name: user-service-consumer
dubbo: application: name: user-service-consumer registry: address: nacos://127.0.0.1:8848 group: dev parameters[namespace]: dev consumer: timeout: 1000 UserRpcService: version: 1.0.0 protocol: port: -1 name: dubbo
|
service-consumer.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package io.github.removeif.springboot;
import io.github.removeif.springboot.api.UserRpcService; import io.github.removeif.springboot.dto.UserDTO; import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; 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.context.ConfigurableApplicationContext; import org.springframework.stereotype.Component;
@SpringBootApplication @EnableDubbo public class ConsumerApplication {
public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args); }
@Component public class UserRpcServiceTest implements CommandLineRunner {
private final Logger logger = LoggerFactory.getLogger(getClass());
@DubboReference(version = "${dubbo.consumer.UserRpcService.version}") private UserRpcService userRpcService;
@Override public void run(String... args) throws Exception { UserDTO user = userRpcService.get(1); logger.info("[run][发起一次 Dubbo RPC 请求,获得用户为({})", user); } } }
|
启动provider,在启动consumer后可以看到控制台
1 2 3 4 5
| 2020-12-19 11:36:49.219 INFO 16272 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP user-service-consumer 192.168.88.1:20881 register finished 2020-12-19 11:36:49.228 INFO 16272 --- [ main] s.ConsumerApplication$UserRpcServiceTest : [run][发起一次 Dubbo RPC 请求,获得用户为(io.github.removeif.springboot.dto.UserDTO@79696332) 2020-12-19 11:36:49.231 INFO 16272 --- [ main] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8848] [subscribe] user-service-consumer+DEFAULT_GROUP 2020-12-19 11:36:49.231 INFO 16272 --- [ main] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8848] [add-listener] ok, tenant=, dataId=user-service-consumer, group=DEFAULT_GROUP, cnt=1
|