非Java语言(php)接入SpringCloud微服务框架

微服务很火,实验室要求大部分语言接入微服务,比如:NET、JAVA、前端、PHP。微服务本来就是JAVA的,作为一个phper,只有想办法接入微服务。
网上可以参考的东西不多,但还是找到了可利用的东西。
先实现php注册到Spring Cloud的注册中心:http://eureka.springcloud.cn/
结果如图:

在这里插入图片描述

把php应用部署到openresty + PHP(FPM)

注册工具SpringCloudOpenresty
https://github.com/tangaiyun/SpringCloudOpenrestyPHP

前提:安装openresty和php 7.0和FPM

使用的系统:ubuntu14.04

安装openresty

源码包下载:http://openresty.org/cn/download.html

找个目录放源码:cd /root
wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
相关依赖包的安装
apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential
解压源码,安装openresty
tar -xzvf openresty-1.13.6.2.tar.gz
cd openresty-1.13.6.2
./configure
make && make install

验证安装成功
开启openresty
/usr/local/openresty/nginx/sbin/nginx
curl 127.0.0.1
返回一段html代码

在这里插入图片描述

安装php7.0

前期准备

# 更新 apt-get
sudo apt-get update

# 解决有可能存在的语言问题
sudo apt-get install -y language-pack-en-base

安装完成之后,运行:
locale-gen en_US.UTF-8
安装Php7.0,本教程采用ppa方式安装php7.0,

# 添加php的仓库
sudo apt-get install software-properties-common
#> 中途按下回车
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

安装完成之后,运行sudo apt-get update更新安装包,把刚才添加的包拉取下来。 运行apt-cache search php7.0搜索php7.0开头的包检验是否安装成功,输出如下:
在这里插入图片描述

安装php7.0:
sudo apt-get -y install php7.0
安装成功后运行php -v查看是否安装成功,成功的话会输出类似如下信息:
在这里插入图片描述

安装 fpm

sudo apt-get install php7.0-fpm

使用serviceRegistry工具

下载serviceRegistry到root目录下
https://github.com/tangaiyun/SpringCloudOpenrestyPHP
git clone https://github.com/tangaiyun/SpringCloudOpenrestyPHP.git
对于Spring cloud:
1.将serviceRegistry/consul/lualib中的所有文件(包含目录)复制到你的openresty lualib目录中/usr/local/openresty/lualib
cp -r /root/SpringCloudOpenrestyPHP/serviceRegistry/eureka/lualib/* /usr/local/openresty/lualib
2.更改你的nginx.conf文件和serviceRegistry/consul/conf/nginx.conf一样,请注意块
cp -r /root/SpringCloudOpenrestyPHP/serviceRegistry/eureka/conf/* /usr/local/openresty/nginx/conf

init_worker_by_lua_block {
        if 0 == ngx.worker.id() then 
            local workservice = require 'eureka.workerservice'
            local filePath = '/usr/local/openresty/nginx/conf/serviceInstance.json'
            eurekaHost = '10.100.23.69'
            eurekaPort = 8761
	    local file1 = io.input(filePath)
        
...

可将“filePath”更改为您自己的服务配置位置。
eurekaHost填写注册中心ip或者域名。比如Spring Cloud的注册中心eureka.springcloud.cn的ip地址:114.215.45.167
eurekaPort填写注册中心的端口。比如Spring Cloud的注册中心端口:80
3.将serviceInstance.json更改为你的环境,例如:
/usr/local/openresty/nginx/conf/serviceInstance.json中的ip改为自己环境的ip,比如192.168.211.22
4.将serviceRegistry/eureka/html中的文件复制到你的Web根目录。
cp -r /root/SpringCloudOpenrestyPHP/serviceRegistry/eureka/html/* /usr/local/openresty/nginx/html

命令

Openresty nginx服务命令:

/usr/local/openresty/nginx/sbin/nginx #启动
/usr/local/openresty/nginx/sbin/nginx -s stop #停止
/usr/local/openresty/nginx/sbin/nginx -s reload #重启

启动php7.0-fpm服务
service php7.0-fpm start
开启openresty以及php7.0-fpm,可以发现在注册中心已经注册成功。
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值