在本套课程中,使用docker容器化技术进行部署和开发,如果对docker不熟悉的同学可以采用传统的部署方式,效果是一样的。
之所以采用docker部署的形式,是考虑到微服务节点的部署,以及后续的集群扩展的便捷性。
环境:ubuntu-16.04.3 + docker 17.03.2-ce
参考资料中的《VMware Workstation 中安装 Ubuntu16.04 虚拟机.docx》文档进行安装,统一环境
虚拟机安装后必须记住
用户密码:itcast/itcast123 root/root123
ip地址 192.168.58.136
《VMware Workstation 中安装 Ubuntu16.04 虚拟机.docx》下载
链接:https://pan.baidu.com/s/1n_yrwwLl6s20mjG6Nv9Hxw?pwd=3hnt
提取码:3hnt
–来自百度网盘超级会员V3的分享
ubuntu.iso下载
链接:https://pan.baidu.com/s/1Tba9ChJhT7l22s7nnYrfyg?pwd=k3nv
提取码:k3nv
–来自百度网盘超级会员V3的分享
jdk包下载
链接:https://pan.baidu.com/s/1SAxoFoTfSA0osLS2-ryHZA?pwd=l74t
提取码:l74t
–来自百度网盘超级会员V3的分享
#拉取zk镜像
docker pull zookeeper:3.5
#创建容器
docker create --name zk -p 2181:2181 zookeeper:3.5
#启动容器
docker start zk
zooinspector下载
链接:https://pan.baidu.com/s/1UhNA-5qeM-jvJ2z1gAlczQ?pwd=42ak
提取码:42ak
–来自百度网盘超级会员V3的分享
打开zooinspector,根文件夹下的build文件夹下的startZK.bat进行测试(地址ipaddress:2181启动有点慢,需要耐心等待)
保存虚拟机快照
groupid:cn.itcast.dubbo
artifactid:itcast-dubbo
右击“itcast-dubbo”–>“new”–>“Module”
artifactid:itcast-dubbo-service
在itcast-dubbo工程中编写pom.xml
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.0.RELEASEversion>
parent>
<groupId>cn.itcast.dubbogroupId>
<artifactId>itcast-dubboartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>itcast-dubbo-servicemodule>
<module>itcast-dubbo-consumermodule>
modules>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>0.2.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.4version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
itcast-dubbo-service依赖
<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">
<parent>
<artifactId>itcast-dubboartifactId>
<groupId>cn.itcast.dubbogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>itcast-dubbo-serviceartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.13version>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
dependencies>
project>
package cn.itcast.dubbo.pojo;
// 使用dubbo要求传输的对象必须实现序列化接口
public class User implements java.io.Serializable {
private static final long serialVersionUID = -7341603933521593227L;
private Long id;
private String username;
private String password;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package cn.itcast.dubbo.service;
import cn.itcast.dubbo.pojo.User;
import java.util.List;
public interface UserService {
/**
* 查询所有的用户数据
*
* @return
*/
List<User> queryAll();
}
package cn.itcast.dubbo.service.impl;
import java.util.ArrayList;
import java.util.List;
import cn.itcast.dubbo.pojo.User;
import cn.itcast.dubbo.service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
@Service(version = "${dubbo.service.version}") //声明这是一个dubbo服务
public class UserServiceImpl implements UserService {
/**
* 实现查询,这里做模拟实现,不做具体的数据库查询
*/
public List<User> queryAll() {
List<User> list = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setAge(10 + i);
user.setId(Long.valueOf(i + 1));
user.setPassword("123456");
user.setUsername("username_" + i);
list.add(user);
}
System.out.println("---------Service 3------------");
return list;
}
}
# Spring boot application
spring.application.name = itcast-dubbo-service
server.port = 9090
# Service version
dubbo.service.version = 1.0.0
dubbo.scan.basePackages = cn.itcast.dubbo.service
dubbo.application.name = dubbo-provider-demo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20882
dubbo.registry.address = zookeeper://192.168.58.136:2181
dubbo.registry.client = zkclient
package cn.itcast.dubbo;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class DubboProvider {
public static void main(String[] args) {
new SpringApplicationBuilder(DubboProvider.class)
.web(WebApplicationType.NONE) // 非 Web 应用
.run(args);
}
}
artifactid:itcast-dubbo-consumer
<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">
<parent>
<artifactId>itcast-dubboartifactId>
<groupId>cn.itcast.dubbogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>itcast-dubbo-consumerartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.13version>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>cn.itcast.dubbogroupId>
<artifactId>itcast-dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
package cn.itcast.dubbo;
import cn.itcast.dubbo.pojo.User;
import cn.itcast.dubbo.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@Reference(version = "1.0.0", loadbalance = "roundrobin")
private UserService userService;
@Test
public void testQueryAll() {
for (int i = 0; i < 100; i++) {
System.out.println("开始调用远程服务 >>>>>" + i);
List<User> users = this.userService.queryAll();
for (User user : users) {
System.out.println(user);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
# Spring boot application
spring.application.name = itcast-dubbo-consumer
server.port = 9091
dubbo.application.name = dubbo-consumer-demo
dubbo.registry.address = zookeeper://192.168.58.136:2181
dubbo.registry.client = zkclient
测试结果,发现可以获取到数据,说明,已经成功调用了service中提供的接口。
不太好用的工具,略
只要价格DUBBO属性loadbalance = “roundrobin"就可以了
记得在启动dubbo-consumer的时候,启动配置上勾上"allow parallel run”(允许多个实例)
链接:https://pan.baidu.com/s/1wp6jNffq6ZZW5mLgd5_60w?pwd=xh3p
提取码:xh3p
–来自百度网盘超级会员V3的分享