linux中搭建nginx做负载均衡的dubbo+zookeep项目
最近在工作中要求用到dubbo+zookeeper搭建项目,本人也刚毕业学习不久,在网上看了一些文章后,总结了一下如何简单的搭建这样的项目,也是第一次写有什么写的不好的地方,欢迎批评和交流。
1、环境准备
因为在互联网,访问量大的情况下,可能会出现服务器崩溃的情况,所以通常需要做负载均衡,这里我们使用nginx做负载均衡。在整个流程中,我们为了更好的看到项目,使用了两台服务器,192.xxx.xxx.131和192.xxx.xxx.132,后续我只会说131和132。其中131上只配置了nginx,132上配置了jdk1.8,zookeeper。
下载nginx(网上有教程,去百度一下),然后修改nginx中配置文件(安装目录下conf下)输入vi nginx.conf,修改如下:
在server{}外增加:
upstream www.tomcat.com{
server 192.xxx.xxx.132:8881;
(如果需要负载均衡,可以配置多个ip)
server ip:8881;
}
在server{}内增加:
location /dz/get/{ (注意:/dz/get/访问路径只是我们再controller中requestMapper写的路径)
proxy_pass http://www.tomcat.com;(www.tomcat.com是我们再upstream 中配置的名称,可以自己随意定义,但是要一致)
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
可以看到如果我们访问 /dz/get/,会根据proxy_pass最终找到192.xxx.xxx.132:8881(8881是我controller所在项目的端口号)
关闭linux 131和132防火墙(systemctl stop firewalld只是暂时关闭,重启好就不生效了)(或者开通端口,注意项目中的端口号也好开通)。(最好开通端口号)。
下载和安装jdk,我在这里就不详细说了,度娘都有。
下载zookeeper,重命名(mv 旧文件名 新文件名)zookeeper中zoo_simple.cfg为zoo.cfg文件(修改dataDir和dataDirLog,需要自己新建文件夹),启动zkServer.sh,通过zkCli.sh测试是否启动成功,配置是否生效(./zkServer.sh restart,sh /zkServer.sh status)。
此时我们的环境就配置好了。
2、项目搭建
1、服务消费者(consumer)
User.java作实体类
package com.lala.pojo;
import lombok.Data;
/**
* @author lala
* @title
* @data 2019/10/28 16:41
*/
@Data
public class User {
private String name;
private Integer age;
private String work;
private String tele;
}
ConsumerController.java
package com.lala.controller;
import com.lala.pojo.User;
import com.lala.service.IConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author lala
* @title
* @data 2019/10/28 17:44
*/
@RestController
@RequestMapping(value = "/dz")
public class UserController {
@Autowired
private IConsumerService consumerService;
@RequestMapping(value = "/get")
public User getUser(){
User user = consumerService.getUser();
return user;
}
}
IConsumerService .java
package com.lala.service;
import com.lala.pojo.User;
/**
* @author lala
* @title
* @data 2019/10/28 16:44
*/
public interface IConsumerService {
public User getUser();
}
ConsumerServiceImpl .java
package com.lala.service.Impl;
import com.lala.pojo.User;
import com.lala.service.IConsumerService;
import com.lala.service.IProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author lala
* @title
* @data 2019/10/28 16:44
*/
@Service("consumerService")
public class ConsumerServiceImpl implements IConsumerService {
@Autowired
private IProviderService providerService;
public User getUser(){
User user = providerService.getUser();
return user;
}
}
IProviderService .java(服务提供者的接口,用来远程调用)
package com.lala.service;
import com.lala.pojo.User;
/**
* @author lala
* @title
* @data 2019/10/28 16:44
*/
public interface IProviderService {
public User getUser();
}
Server .java启动类
package com.lala;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:dubbo.xml")
//@EnableScheduling
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class,args);
}
}
application.xml资源文件(resource目录下)
#port
server.port=8881
dubbo.xml资源文件(resource目录下)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-lazy-init="false" >
<dubbo:application name="provider" owner="lala" organization="own" />
<!--扫描哪个包下的所有文件-->
<dubbo:annotation package="com.lala" />
<!--132这个地址修改为zookeeper启动的ip地址,2181是端口号-->
<dubbo:registry protocol="zookeeper" address="192.168.239.132:2181" />
<dubbo:protocol name="dubbo" port="28881"/>
<!--<dubbo:monitor protocol="registry"/>-->
<dubbo:consumer check="false" />
<!--provider-->
<dubbo:service interface="com.lala.service.IProviderService" ref="providerService"/>
</beans>
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.lala</groupId>
<artifactId>provider</artifactId>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、服务提供者(provider)
User.java作实体类
package com.lala.pojo;
import lombok.Data;
/**
* @author lala
* @title
* @data 2019/10/28 16:41
*/
@Data
public class User {
private String name;
private Integer age;
private String work;
private String tele;
}
IProviderService.java
package com.lala.service;
import com.lala.pojo.User;
/**
* @author lala
* @title
* @data 2019/10/28 16:44
*/
public interface IProviderService {
public User getUser();
}
ProviderServiceImpl.java
package com.lala.service.Impl;
import com.lala.pojo.User;
import com.lala.service.IProviderService;
import org.springframework.stereotype.Service;
/**
* @author lala
* @title
* @data 2019/10/28 16:44
*/
@Service("providerService")
public class ProviderServiceImpl implements IProviderService {
public User getUser(){
User user = new User();
user.setName("xxx");
user.setAge(23);
user.setWork("IT");
user.setTele("1818888888");
return user;
}
}
Server .java启动类
package com.lala;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:dubbo.xml")
//@EnableScheduling
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class,args);
}
}
application.xml资源文件(resource目录下)
#port端口号
server.port=8880
dubbo.xml资源文件(resource目录下)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-lazy-init="false" >
<dubbo:application name="provider" owner="lala" organization="own" />
<dubbo:annotation package="com.lala" />
<!--dev-->
<dubbo:registry protocol="zookeeper" address="192.168.239.132:2181" />
<dubbo:protocol name="dubbo" port="28881"/>
<!--<dubbo:monitor protocol="registry"/>-->
<dubbo:consumer check="false" />
<!--provider-->
<dubbo:service interface="com.lala.service.IProviderService" ref="providerService"/>
</beans>
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.lala</groupId>
<artifactId>provider</artifactId>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
此时将我们提供者(provider)和消费者(consumer)微服务成两个jar包,通过xftp上传到服务器132上,通过命令nohup java -jar provider-1.0.jar >> nohup-provider 2>&1(consumer类似)运行服务器上,此时在我们本地通过http://192.168.239.131:80/dz/get访问nginx中我们配置的地址,将会跳转132到消费者中,消费者会通过dubbo找到对应zookeeper中获取服务,调用服务提供者,返回结果。