赞
踩
001-interface
:表示接口工程
boot-provider
: 表示服务提供者
boot-consumer
: 表示服务消费者
明确接口工程为java工程,需要将该工程打成jar包,供提供者和消费者使用
打包步骤
public interface HelloService {
public String sayHello();
}
<?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.cjw.dubbo</groupId>
<artifactId>001-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
import com.cjw.dubbo.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Service //将服务暴露出去
@Component //将服务注册在容器中
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello() {
return "boot-provider-sayHello";
}
}
@EnableDubbo //开启dubbo注解
@SpringBootApplication
public class BootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootProviderApplication.class, args);
}
}
#端口号
server.port=8082
#注册提供者
dubbo.application.name=boot-provider
#注册服务到zookeeper中
dubbo.registry.address=zookeeper://localhost:2181
#协议端口号为20880
dubbo.protocol.port=20880
#使用协议为dubbo
dubbo.protocol.name=dubbo
<!--dubbo--> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <!--zookeeper--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>apache-curator</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> <!--排除这个slf4j-log4j12--> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <!--引入接口工程--> <dependency> <groupId>com.cjw.dubbo</groupId> <artifactId>001-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
1.HelloDubboController
import com.cjw.dubbo.service.HelloService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloDubboController { @Reference //远程引用指定的服务,他会按照全类名进行匹配,看谁给注册中心注册了这个全类名 private HelloService helloService; @RequestMapping("/hello") @ResponseBody public String getHello() { String sayHello = helloService.sayHello(); System.out.println("sayHello = " + sayHello); return sayHello; } }
@EnableDubbo //开启dubbo注解
@SpringBootApplication
public class BootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootConsumerApplication.class, args);
}
}
#端口号
server.port=8083
#注册服务消费者
dubbo.application.name=boot-consumer
#使用zookeeper 接收服务
dubbo.registry.address=zookeeper://localhost:2181
<!--dubbo--> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <!--zookeeper--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>apache-curator</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> <!--排除这个slf4j-log4j12--> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <!--接口工程--> <dependency> <groupId>com.cjw.dubbo</groupId> <artifactId>001-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
注意. @Reference 表示远程引用指定的服务,他会按照全类名进行匹配,看谁给注册中心注册了这个全类名
@Service //将服务暴露出去 包名为org.apache.dubbo.config.annotation.Service
@EnableDubbo //开启dubbo注解
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。