Category - 微服务(IT)

跨域 TYK MicroService 微服务    2017-07-19 21:39:10    3
浏览器端报错:

"NetworkError: 403 Forbidden - http://192.168.10.147:8080/mytest/helloworld"
helloworld
已阻止跨源请求:同源策略禁止读取位于 http://192.168.10.147:8080/mytest/helloworld 的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')。

tomcat报错:

192.168.10.147 - - [02/Mar/2016:19:13:41 +0800] "OPTIONS /testjersey/rest/helloworld HTTP/1.1" 403 107
192.168.10.147 - - [02/Mar/2016:19:14:50 +0800] "GET /testjersey/rest/helloworld HTTP/1.1" 200 23


解析:

关于跨域,我使用的是如下地址的方案:http://blog.csdn.net/gsying1474/article/details/49664883
但是在配置方面,在web.xml中,部分配置如下
<init-param>
    <param-name>cors.supportedHeaders</param-name>
    <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>
</init-param>

而tyk中使用了Authorization在头部信息中,

解决办法:
    在init-param配置中,“cors.supportedHeaders”配置添加Authorization即可
 
 

 

by 刘迎光@萤火虫工作室 
OpenBI交流群:495266201 
MicroService 微服务交流群:217722918 
mail: liuyg#liuyingguang.cn 
博主首页(==防止爬虫==):http://blog.liuyingguang.cn

jersey java 拦截器 微服务 MicroService    2017-07-19 21:37:32    6
1、web.xml配置
    <servlet>
        <servlet-name>Rest_Servlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>cn.firewarm.testRestShiro.rest</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>cn.firewarm.testRestShiro.filter.TestFilter</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

2、TestFilter代码
public class TestFilter implements ContainerRequestFilter {
    public ContainerRequest filter(ContainerRequest request) {
        if(判断条件){
            return request;
        }else{
            Response response = Response.ok("error").status(401).type(MediaType.APPLICATION_JSON).build();  
            throw new WebApplicationException(response); // Throw new UnAuthor
jetty jersey 微服务 MicroService    2017-07-19 21:36:34    6

材料:

    Docker
    java+jersey 的demo:https://git.coding.net/firewarm/jetty-jersey.git
    jenkins:详细信息参考文章:http://blog.csdn.net/gsying1474/article/details/51126522

创建一个maven风格的项目,源码管理选择git,填上如上demo的地址,然后添加“post-build step”


填写参数,这里主要我暂时不需要push到registry上,所以选择跳过



都填写完毕后,构建即可,构建完成后,运行docker
# docker run -d -p 8090:8080 test-jetty

待启动完成后,访问地址:http://192.168.10.136:8090/jetty-jersey/status
返回RUNNING则表示构建部署成功了

 

by 刘迎光@萤火虫工作室 
OpenBI交流群:495266201 
MicroService 微服务交流群:217722918 
mail: liuyg#liuyingguang.cn 
博主首页(==防止爬虫==):http://blog.liuyingguang.cn

docker 微服务 MicroService    2017-07-19 21:30:08    12
本项目是将restful项目打包成可执行的war包,在docker中执行

环境介绍:

    docker    1.10.3
    jetty    8
    jersey    1.19

关键配置:
1、pom.xml配置
<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <systemProperties>
                    </systemProperties>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </pl
GateWay restful TYK    2017-07-19 20:58:01    4

源文件:https://github.com/TykTechnologies/tyk-swagger-definitions/blob/master/tyk_gateway_api.yml


 

swagger: '2.0'

info:
  version: "1.9"
  title: Gateway REST API

paths:
  /tyk/keys/:
    get:
      description: |
        Gets a list of *key* IDs (will only work with non-hashed installations)
      parameters:
        -
          name: api_id
          in: query
          description: Back-end to target
          required: true
          type: string
          format: string
        -
          name: x-tyk-authorization
          in: header
          description: tyk gateway shared secret
          required: true
          type: string
          format: string

      responses:
        200:
          description: Successful response
          schema:
            type: object
            properties:
              keys: 
                type: array
                items: 
                  type: string
                  
  /tyk/keys/create:
    post:
      description: |
        Create a n
2/4