Apace dubbo学习笔记(一)
1.创建父工程,其pom.xml为
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<my.version>2.7.6my.version>
<api.version>1.0api.version>
<web.version>2.3.3.RELEASEweb.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>${web.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${my.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<type>pomtype>
<version>${my.version}version>
dependency>
<dependency>
<groupId>com.scaffold.learngroupId>
<artifactId>my-apiartifactId>
<version>${api.version}version>
dependency>
dependencies>
dependencyManagement>
2.创建my-api项目。
public class Entity implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Entity{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建rpc接口
public interface ShowApi {
Entity show(String name, int age);
}
创建服务提供者
主要的pom.xml为
org.springframework.boot
spring-boot-starter-web
2.3.3.RELEASE
org.apache.dubbo
dubbo-spring-boot-starter
org.apache.dubbo
dubbo-dependencies-zookeeper
pom
com.scaffold.learn
my-api
服务提供者实现接口
import com.scaffold.learn.api.ShowApi;
import com.scaffold.learn.api.entity.Entity;
import org.apache.dubbo.config.annotation.Service;
@Service
public class ShowProvider implements ShowApi {
@Override
public Entity show(String name, int age) {
System.out.println("request\tname="+name+"\tage="+age);
Entity entity=new Entity();
entity.setName("dubbo-"+name);
entity.setAge(age+1);
return entity;
}
}
启动函数
@SpringBootApplication
public class ProviderService {
public static void main(String[] args) {
SpringApplication.run(ProviderService.class,args);
}
}
相关配置文件
server.port=8333
dubbo.scan.base-packages=com.scaffold.learn.provider
dubbo.application.name=dubbo-provider
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:2181
3.服务消费者pom.xml文件
org.springframework.boot
spring-boot-starter-web
2.3.3.RELEASE
org.apache.dubbo
dubbo-spring-boot-starter
org.apache.dubbo
dubbo-dependencies-zookeeper
pom
com.scaffold.learn
my-api
对外接口:
@RestController
public class Api {
@Reference
private ShowApi showApi;
@GetMapping(value = "/test")
public String testDubbo(){
Entity entity = showApi.show("tianjingle",1);
System.out.println(entity.toString());
return entity.toString();
}
}
启动函数
@SpringBootApplication
public class ConsumerService {
public static void main(String[] args) {
SpringApplication.run(ConsumerService.class,args);
}
}
配置文件
server.port=8334
spring.application.name=consumer
#dubbo.scan.base-packages=com.scaffold.learn.consumer
embedded.zookeeper.port=2181
## Dubbo Registry
dubbo.registry.address= zookeeper://127.0.0.1:${embedded.zookeeper.port}
4.启动zookeeper
5.启动服务提供者和消费者
在浏览器访问服务消费者接口
http://localhost:8334/test
评论