1、下载 zookeeper http://archive.apache.org/dist/zookeeper
解压缩到 /usr/local
重命名 conf/zoo_sample.cfg --> conf/zoo.cfg
2、下载 tomcat https://tomcat.apache.org/download-80.cgi
Tomcat linux 配置
解压缩到 /usr/local
添加环境变量 vi /etc/profile
export TOMCAT_HOME=/usr/local/apache-tomcat-8.5.28
配置 Tomcat 管理界面
vi conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="tomcat" password="tangqun" roles="manager-script, manager-jmx, manager-gui"/>
配置 Tomcat 管理界面 远程IP 访问
vi conf/Catalina/localhost/manager.xml
<Context privileged="true" antiResourceLocking="false" docBase="${catalina.home}/webapps/manager">
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>
3、下载 dubbo https://github.com/alibaba/dubbo/releases/dubbo-dubbo-2.5.8.tar.gz
用IDEA打开, 找到 dubbo-admin 打war包
将新生成的 dubbo-admin-2.5.8.war 上传到 /usr/local/apache-tomcat-8.5.28/webapps/ 下面
启动 tomcat , 访问 http://yourhost:8080/http://106.75.11.56:8080/dubbo-admin-2.5.8/
账号: root 密码:root
4、代码 生产者 消费者
通用 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.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.8</version> </dependency> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>1.0.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.8</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
provider
package com.example.demo.service.rpc; public interface HelloService { String say(); }
package com.example.demo.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.example.demo.service.rpc.HelloService; import org.springframework.stereotype.Component; @Service(interfaceClass = HelloService.class) @Component public class HelloServiceImpl implements HelloService { public String say() { return "hello word"; } }
package com.example.demo; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
spring.dubbo.appname=demo-dubbo-provider spring.dubbo.registry=zookeeper://106.75.11.56:2181 spring.dubbo.protocol=dubbo
consumer
package com.example.demo.service.rpc; public interface HelloService { String say(); }
package com.example.demo.service.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.example.demo.service.rpc.HelloService; import org.springframework.stereotype.Service; @Service public class RefService { @Reference private HelloService helloService; public String say() { return helloService.say(); } }
package com.example.demo; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
server.port=8088 spring.dubbo.appname=demo-dubbo-consume spring.dubbo.registry=zookeeper://106.75.11.56:2181 spring.dubbo.protocol=dubbo
package com.example.demo.controller; import com.example.demo.service.impl.RefService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping("/hello") @Controller public class HelloController { @Autowired private RefService refService; @RequestMapping("/say") @ResponseBody public String say() { return refService.say(); } }
注意:生产者 和 消费者 接口包名一致,通常做法都是将接口发布成一个独立的jar包。
欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。