官网:http://zookeeper.apache.org/,
修改配置文件:zoo.cfg lg4j.properties
启动zookeeper:
服务提供端
Service 接口:
public interface UserService {
public String getUserInfo(String name);
}
Service 接口实现:
@Service //必须是dubbo下的Sservice
public class UserServiceImpl implements UserService {
@Override
public String getUserInfo(String name) {
return "我是服务提供者:your name is " + name;
}
}
application.properties文件配置
server.port=8004
spring.dubbo.application.name=dubbo-user
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.huayu.user.dubbo
消费者:
Service接口:
package com.huayu.user.dubbo;
public interface UserService {
public String getUserInfo(String name);
}
package com.huayu.user.dubbo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class SubUserService {
@Reference
private UserService mUserService;
public String getUser(String name) {
return mUserService.getUserInfo(name);
}
}
application.properties文件配置
server.port=8004
## Dubbo 服务消费者配置
spring.dubbo.application.name=dubbo-sub
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.huayu.user.dubbo
消费者调用:
package com.huayu.subscription.controller;
import com.huayu.user.dubbo.SubUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SubHelloController {
@Autowired
private SubUserService mSubUserService;
@GetMapping(value = "hello")
public String hello(String name) {
return "远程调用:" + mSubUserService.getUser(name);
}
@GetMapping(value = "hello1")
public String haha() {
return "我是消费者";
}
}
打包war文件部署到tomcat
官网地址:https://github.com/apache/incubator-dubbo
重新打包 dubbo-admin 项目 为war文件 放入 :F:\apache-tomcat-8.5.31\webapps 启动tomcat 修改自己的端口号 现实如下网站 说明部署正确
下载地址:http://nginx.org/en/download.html 选择自己需要的版本
配置文件:F:\nginx-1.15.8\conf\nginx.conf
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /UserServer/ {
proxy_pass http://127.0.0.1:8004/UserServer/;
}
location /SubServer/ {
proxy_pass http://127.0.0.1:8004/SubServer/;
}
最终实现:http://localhost/SubServer/hello?name=zhangsan ----> 远程调用:我是服务提供者:your name is zhangsan
简单demo:https://github.com/wudyy/zookeeper-dubbo 适合初学者了解基本zookeeper+dubbo流程