springcloud学习(四) sidecar
码云地址:https://gitee.com/lpxs/lp-springcloud.git
有问题可以多沟通:136358344@qq.com。
springcloud学习(四) sidecar
前言
最近一段一直在研究微服务,并且将公司内部系统微服务化,由于公司内部也有一些业务系统是采用异构语言比如说.net ,python开发,所以也需要将这些系统服务化,让这些系统也能使用Eureka, Ribbon和Config Server。
下面是官方给的描述:
[外链图片转存失败(img-ggrqNTha-1565757294153)(http://i.imgur.com/ilU73lu.png)]
现在结合一个python的django框架来实现springcloud sidecar功能
sidecar实践
1、准备springcloud eureka、config server等微服务环境
2、安装python以及django
安装过程我就不详细描述了
3、生成一个django工程
django-admin startproject HelloWorld$ cd HelloWorld/
$ tree
.
|-- HelloWorld
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
`-- manage.py
4、编写view
HelloWorld/HelloWorld/view.pyfrom django.http import HttpResponsedef hello(request):return HttpResponse("Hello world ! ")def sidecar(request):return HttpResponse("{\"status\":\"UP\"}",content_type="application/json")
这里需要注意的是,返回消息content_type必须为application/json,否则springcloud sidecar会报错
5、增加url
HelloWorld/HelloWorld/urls.pyfrom django.conf.urls import include, url
from django.contrib import admin
import viewurlpatterns = [url(r'^admin/', include(admin.site.urls)),url(r'^$', view.hello),url(r'^sidecar/', view.sidecar),
]
6、启动python
python manage.py runserver 0.0.0.0:8000
7、创建springcloud sidecar工程
pom.xml
4.0.0 test-sidecar jar ${test.version} sangfor-sidecar test-sidecar com.test test 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-netflix-sidecar
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.sidecar.EnableSidecar;@SpringBootApplication
@EnableDiscoveryClient
@EnableSidecar
public class SangforSidecarApplication {public static void main(String[] args) {SpringApplication.run(SangforSidecarApplication.class, args);}
}
配置文件application.yml
info:name: sidecar代理version: 0.0.1server:port: 8766spring:application:name: sidecar-serverprofiles:active: devcloud:config:fail-fast: truediscovery:service-id: config-serverenabled: trueprofile: ${spring.profiles.active}logging:level: debugsidecar:port: 8000health-uri: http://localhost:8000/sidecar/
---
spring:profiles: deveureka:instance:prefer-ip-address: truelease-renewal-interval-in-seconds: 5lease-expiration-duration-in-seconds: 20client:serviceUrl:defaultZone: http://200.200.4.30:8761/eureka/registry-fetch-interval-seconds: 10
注意这里的重点是sidecar.port和sidecar.health-uri两个配置项
sidecar.port 异构系统端口
sidecar.health-uri 异构系统的心跳uri
8、启动sidecar
现在可以看到eureka中出现了sidecar如下:
停掉django服务,再看eureka中的sidecar状态,已经显示down
总结
sidecar就是一个简单代理,类似于service mesh服务中的envoy,不过远没有envoy那么强大,不过对于restful异构系统来说,使用sidecar已经可以使用springcloud的部分功能,来实现服务注册、发现、监控等
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
