---恢复内容开始---
一. dubbo-admin管理控制台
二、
https://blog.csdn.net/qq_25215821/article/details/100806580
这里有配置服务提供者。
1. 新建一个maven项目:hello-dubbo-service-user-api
pom.xml文件如下:
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.funtl</groupId> <artifactId>hello-dubbo-service-user-api</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> </project>
创建一个接口UserService.java :
package com.funtl.hello.dubbo.service.user.provider.api; public interface UserService { String sayHi(); }
2. 再创建一个服务提供者项目 hello.dubbo.service.user.provider
pom.xml文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.funtl</groupId> <artifactId>hello-dubbo-service-user-provider</artifactId> <version>1.0.0-SNAPSHOT</version> <name>hello-dubbo-service-user-provider</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>com.funtl</groupId> <artifactId>hello-dubbo-service-user-api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
这里依赖了上一个项目的jar包,利用了maven的依赖机制,所以先要把上一个项目 mvn clean install到本地仓库。
然后创建一个类实现刚才创建的接口
UserServiceImpl.java 注意:包名和上一个接口项目保持一致,这样打包会打在一起,方便。
package com.funtl.hello.dubbo.service.user.provider.api.impl; import com.alibaba.dubbo.config.annotation.Service; import com.funtl.hello.dubbo.service.user.provider.api.UserService; @Service(version = "${user.service.version}") public class UserServiceImpl implements UserService{ @Override public String sayHi() { return "hello dubbo..."; } }
最后需要在项目程序入口类里面增加代码
package com.funtl.hello.dubbo.service.user.provider; import com.alibaba.dubbo.container.Main; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloDubboServiceUserProviderApplication { public static void main(String[] args) { SpringApplication.run(HelloDubboServiceUserProviderApplication.class, args); Main.main(args); } }
application.yml配置如下:(yml格式注意空格)
spring: application: name: hello-dubbo-service-user-provider user: service: version: 1.0.0 dubbo: scan: basePackages: com.funtl.hello.dubbo.service.user.provider.api application: id: hello-dubbo-service-user-provider name: hello-dubbo-service-user-provider qos-port: 22222 qos-enable: true protocol: id: dubbo name: dubbo port: 12345 status: server registry: id: zookeeper address: zookeeper://10.1.10.19:2181?backup=10.1.10.19:2182,10.1.10.19:2183 management: endpoint: dubbo: enabled: true dubbo-shutdown: enabled: true dubbo-configs: enabled: true dubbo-services: enabled: true dubbo-references: enabled: true dubbo-properties: enabled: true health: dubbo: status: defaults: memory extras: load,threadpool
最后启动项目刷新http://localhost:8080
上图显示服务已经注册到zookeeper上了。服务提供者完成。
三、服务消费者
创建一个springboot web项目 hello.dubbo.service.user.consumer
pom.xml:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.funtl</groupId> <artifactId>hello-dubbo-service-user-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hello-dubbo-service-user-consumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>com.funtl</groupId> <artifactId>hello-dubbo-service-user-api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建UserController 类:
package com.funtl.hello.dubbo.service.user.consumer.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.funtl.hello.dubbo.service.user.provider.api.UserService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Reference(version="${user.service.version}") private UserService userService; @RequestMapping(value = "hi",method = RequestMethod.GET) public String sayHi(){ return userService.sayHi(); } }
application.yml:
spring: application: name: hello-dubbo-service-user-consumer user: service: version: 1.0.0 dubbo: scan: basePackages: com.funtl.hello.dubbo.service.user.consumer.controller application: id: hello-dubbo-service-user-consumer name: hello-dubbo-service-user-consumer qos-port: 22223 qos-enable: true protocol: id: dubbo name: dubbo port: 12345 status: server registry: id: zookeeper address: zookeeper://10.1.10.19:2181?backup=10.1.10.19:2182,10.1.10.19:2183 management: endpoint: dubbo: enabled: true dubbo-shutdown: enabled: true dubbo-configs: enabled: true dubbo-services: enabled: true dubbo-references: enabled: true dubbo-properties: enabled: true health: dubbo: status: defaults: memory extras: load,threadpool server: port: 9090
浏览器访问:http://localhost:9090/hi,显示如下:
完成调用!!!
dubbo-admin显示有消费者:
完成@!!
谢谢参考。