spring cloud sidecar 代理python的webapi服務

系統結構

系統方案

1.eurake服務中心的實現---略

2.python服務的特殊實現

python服務需要特別實現一個health的方法,並且返回指定的json格式如下

{
  "status":"UP"
}

3.基於sidecar的代理程序實現

  1. pom文件的引用配置
  2. <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>

     

  3. application.properties文件配置
  4. 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/
    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
    ribbon.ConnectTimeout=5000
    ribbon.ReadTimeout=5000
  5. 入口程序類如下
  6. 
    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程序的實現

  1. pom引用
  2. <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>

     

  3. application.properties的配置
  4. 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

     

  5. 標準fegin格式接口實現
  6. 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一致,便於管理】();
    
    
    }

     

  7. 服務調用,正常的fegin接口調用
  8.  @Autowired
        private 接口名 service;

     

系統啓動

  1. 啓動python服務
  2. 啓動sidecar程序【前提eureka服務中心已經在運行】
  3. 啓動調用服務的java程序

注意

python的webapi請求格式

http://域名/路徑/方法名/

需要以/結尾,如果沒有/的話python的框架會發出http的308狀態,自動跳轉,對於java的fegin客戶端來說這是不可接受的,所以在編寫feginclient接口的方法時,一定注意要以/結尾設置RequestMapping的value值。

java的webapi請求格式

http://域名/路徑/方法名

是沒有/結尾的,如果按照java的習慣設置python服務的feginclient接口裏RequestMapping的value值會引起問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章