码云地址: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.py

from django.http import HttpResponse

def 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.py

from django.conf.urls import include, url
from django.contrib import admin
import view

urlpatterns = [
    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

<?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>
	<artifactId>test-sidecar</artifactId>
	<packaging>jar</packaging>
	<version>${test.version}</version>
	<name>sangfor-sidecar</name>
	<description>test-sidecar</description>

	<parent>
		<groupId>com.test</groupId>
		<artifactId>test</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>


	<dependencies>
		<dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-actuator</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-starter-config</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-starter-eureka</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-web</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-netflix-sidecar</artifactId>
	    </dependency>
	</dependencies>
</project>

启动类

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.1

server:
  port: 8766


spring:
  application:
    name: sidecar-server
  profiles:
    active: dev
  cloud:
    config:
      fail-fast: true
      discovery:
        service-id: config-server
        enabled: true
      profile: ${spring.profiles.active}

logging:
  level: debug

sidecar:
  port: 8000
  health-uri: http://localhost:8000/sidecar/
---
spring:
  profiles: dev

eureka:
  instance:
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 20
  client:
    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的部分功能,来实现服务注册、发现、监控等

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐