系统结构
系统方案
1.eurake服务中心的实现---略
2.python服务的特殊实现
python服务需要特别实现一个health的方法,并且返回指定的json格式如下
{
"status":"UP"
}
3.基于sidecar的代理程序实现
- pom文件的引用配置
-
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring-boot.version>2.1.5.RELEASE</spring-boot.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-netflix-sidecar --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-sidecar</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <scope>import</scope> <type>pom</type> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- application.properties文件配置
-
server.port=端口号 #配置Tomcat编码,默认为UTF-8 server.tomcat.uri-encoding=UTF-8 spring.application.name=程序名 #python的webapi的端口 sidecar.port=python服务的端口 #python的webapi的hostname sidecar.hostname=ip地址 #python的webapi的ip,多网卡的服务器需要指明ip sidecar.ip-address=ip地址 sidecar.health-uri=http://ip地址:${sidecar.port}/health # 启动服务注册 eureka.client.register-with-eureka = true #启动服务发现 eureka.client.fetch-registry = true eureka.client.service-url.defaultZone = http://注册服务中心地址/eureka/ eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000 ribbon.ConnectTimeout=5000 ribbon.ReadTimeout=5000
- 入口程序类如下
-
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.sidecar.EnableSidecar; @EnableSidecar @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4.调用python服务的java程序的实现
- pom引用
-
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> ......................省略常见配置 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- application.properties的配置
-
server.port=端口 #配置Tomcat编码,默认为UTF-8 server.tomcat.uri-encoding=UTF-8 # 启动服务注册 eureka.client.register-with-eureka = false #启动服务发现 eureka.client.fetch-registry = true eureka.client.service-url.defaultZone = http://注册中心地址/eureka/ ribbon.okhttp.enabled=true ribbon.restclient.enabled=true
- 标准fegin格式接口实现
-
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(value ="sidecar代理程序在配置文件中spring.application.name属性的值") public interface 接口名{ /** * RequestMapping中的路径需要是端口后的路径 * @return */ @RequestMapping(value = "/路径/方法名/",method = {RequestMethod.POST}) String 方法名【最好和Python一致,便于管理】(); }
- 服务调用,正常的fegin接口调用
-
@Autowired private 接口名 service;
系统启动
- 启动python服务
- 启动sidecar程序【前提eureka服务中心已经在运行】
- 启动调用服务的java程序
注意
python的webapi请求格式
http://域名/路径/方法名/
需要以/结尾,如果没有/的话python的框架会发出http的308状态,自动跳转,对于java的fegin客户端来说这是不可接受的,所以在编写feginclient接口的方法时,一定注意要以/结尾设置RequestMapping的value值。
java的webapi请求格式
http://域名/路径/方法名
是没有/结尾的,如果按照java的习惯设置python服务的feginclient接口里RequestMapping的value值会引起问题。