chenweida před 8 roky
rodič
revize
3d338ddd5d

+ 1 - 1
common-lib-parent-pom/pom.xml

@ -4,7 +4,7 @@
         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>
    <groupId>com.yihu</groupId>
    <groupId>com.yihu.jw</groupId>
    <artifactId>common-lib-parent-pom</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

+ 2 - 2
common/common-swagger/pom.xml

@ -4,13 +4,13 @@
         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>
    <parent>
        <groupId>com.yihu</groupId>
        <groupId>com.yihu.jw</groupId>
        <artifactId>common-lib-parent-pom</artifactId>
        <version>1.0.0</version>
        <relativePath>../../common-lib-parent-pom/pom.xml</relativePath>
    </parent>
    <groupId>com.yihu</groupId>
    <groupId>com.yihu.jw</groupId>
    <artifactId>common-swagger</artifactId>
    <version>1.0.0</version>

+ 2 - 2
svr-configuration/pom.xml

@ -4,14 +4,14 @@
         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>
    <parent>
        <groupId>com.yihu</groupId>
        <groupId>com.yihu.jw</groupId>
        <artifactId>svr-lib-parent-pom</artifactId>
        <version>1.0.0</version>
        <relativePath>../svr-lib-parent-pom/pom.xml</relativePath>
    </parent>
    <groupId>com.yihu</groupId>
    <groupId>com.yihu.jw</groupId>
    <artifactId>svr-configuration</artifactId>
    <version>1.0-SNAPSHOT</version>

+ 2 - 2
svr-discovery/pom.xml

@ -4,13 +4,13 @@
         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>
    <parent>
        <groupId>com.yihu</groupId>
        <groupId>com.yihu.jw</groupId>
        <artifactId>svr-lib-parent-pom</artifactId>
        <version>1.0.0</version>
        <relativePath>../svr-lib-parent-pom/pom.xml</relativePath>
    </parent>
    <groupId>com.yihu</groupId>
    <groupId>com.yihu.jw</groupId>
    <artifactId>svr-discovery</artifactId>
    <version>1.0.0</version>

+ 2 - 2
svr-lib-parent-pom/pom.xml

@ -3,7 +3,7 @@
         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>
    <groupId>com.yihu</groupId>
    <groupId>com.yihu.jw</groupId>
    <artifactId>svr-lib-parent-pom</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
@ -48,7 +48,7 @@
            <!--自己的封装 common -->
            <dependency>
                <groupId>com.yihu</groupId>
                <groupId>com.yihu.jw</groupId>
                <artifactId>common-swagger</artifactId>
                <version>${version.myCommon}</version>
            </dependency>

+ 4 - 4
svr/svr-base/pom.xml

@ -4,21 +4,21 @@
         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>
    <parent>
        <groupId>com.yihu</groupId>
        <groupId>com.yihu.jw</groupId>
        <artifactId>svr-lib-parent-pom</artifactId>
        <version>1.0.0</version>
        <relativePath>../../svr-lib-parent-pom/pom.xml</relativePath>
    </parent>
    <groupId>com.yihu</groupId>
    <groupId>com.yihu.jw</groupId>
    <artifactId>svr-base</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>com.yihu</groupId>
            <groupId>com.yihu.jw</groupId>
            <artifactId>common-swagger</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

+ 10 - 4
web-gateway/pom.xml

@ -4,18 +4,18 @@
         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>
    <parent>
        <groupId>com.yihu</groupId>
        <groupId>com.yihu.jw</groupId>
        <artifactId>svr-lib-parent-pom</artifactId>
        <version>1.0.0</version>
        <relativePath>../svr-lib-parent-pom/pom.xml</relativePath>
    </parent>
    <groupId>com.yihu</groupId>
    <groupId>com.yihu.jw</groupId>
    <artifactId>web-gateway</artifactId>
    <version>1.0-SNAPSHOT</version>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>com.yihu</groupId>
            <groupId>com.yihu.jw</groupId>
            <artifactId>common-swagger</artifactId>
        </dependency>
@ -49,10 +49,16 @@
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>

+ 17 - 0
web-gateway/src/main/java/com/yihu/jw/commnon/Contants.java

@ -0,0 +1,17 @@
package com.yihu.jw.commnon;
/**
 * Created by chenweida on 2017/5/15.
 * 静态类,存储一些常量
 */
public class Contants {
    //微服务的id start
    public static final String svr_base = "svr-base";
    public static final String svr_gateway = "svr-gateway";
    //微服务的id end
    public static final class patient {
        public static final String findByCode = "/patient/findByCode";
    }
}

+ 2 - 2
web-gateway/src/main/java/com/yihu/jw/controller/PatientController.java

@ -26,7 +26,7 @@ public class PatientController {
    @GetMapping(value = "findByCode")
    //配置HystrixProperty 则调用的方法和fallback是同一个线程 否则就不是
    //@HystrixCommand(fallbackMethod = "findByCodeFallback",commandProperties = @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE"))
    //@HystrixCommand(fallbackMethod = "findByCodeFallback" )
//   @HystrixCommand(fallbackMethod = "findByCodeFallback" )
    public String findByCode(
            @ApiParam(name = "code", value = "患者code", required = true) @RequestParam(value = "code", required = true) String code) {
        return patientFegin.findByCode(code);
@ -39,6 +39,6 @@ public class PatientController {
//     * @return
//     */
//    public String findByCodeFallback(String code) {
//        return "";
//        return "启动断路器";
//    }
}

+ 10 - 5
web-gateway/src/main/java/com/yihu/jw/fegin/PatientFegin.java

@ -1,6 +1,10 @@
package com.yihu.jw.fegin;
import com.yihu.jw.commnon.Contants;
import com.yihu.jw.fegin.fallback.PatientFeginFallback;
import com.yihu.jw.fegin.fallbackfactory.PatientFeginFallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -10,13 +14,14 @@ import org.springframework.web.bind.annotation.RequestParam;
 * Created by chenweida on 2017/5/10.
 */
@FeignClient(
        name = "svr-base", // name值是eurika的实例名字
        fallback = PatientFeginFallback.class// fallback是请求超时或者错误的回调函数
        name = Contants.svr_base // name值是eurika的实例名字
       // ,fallback = PatientFeginFallback.class// fallback是请求超时或者错误的回调函数
        ,fallbackFactory  =PatientFeginFallbackFactory.class
//        ,configuration =   //可以配置当个fegin的配置 例如禁用单个feign的hystrix
    )
)
public interface PatientFegin {
    @RequestMapping(value = "/patient/findByCode", method = RequestMethod.GET)
    public String findByCode(@RequestParam(value = "code", required = true) String code);
    @RequestMapping(value = Contants.patient.findByCode, method = RequestMethod.GET)
    String findByCode(@RequestParam(value = "code", required = true) String code);
}

+ 7 - 1
web-gateway/src/main/java/com/yihu/jw/fegin/fallback/PatientFeginFallback.java

@ -1,17 +1,23 @@
package com.yihu.jw.fegin.fallback;
import com.yihu.jw.commnon.Contants;
import com.yihu.jw.fegin.PatientFegin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * Created by chenweida on 2017/5/13.
 */
@Component
//@Component
public class PatientFeginFallback implements PatientFegin {
    private Logger logger = LoggerFactory.getLogger(PatientFeginFallback.class);
    @Override
    public String findByCode(String code) {
        logger.info("进入断路器");
        return "断路器启动";
    }
}

+ 22 - 0
web-gateway/src/main/java/com/yihu/jw/fegin/fallbackfactory/PatientFeginFallbackFactory.java

@ -0,0 +1,22 @@
package com.yihu.jw.fegin.fallbackfactory;
import com.yihu.jw.fegin.PatientFegin;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * Created by chenweida on 2017/5/15.
 */
@Component
public class PatientFeginFallbackFactory implements FallbackFactory<PatientFegin> {
    @Override
    public PatientFegin create(Throwable e) {
        return new PatientFegin() {
            public String findByCode(String code) {
                return "启动断路器"+e.getMessage() ;
            }
        };
    }
}

+ 16 - 13
web-gateway/src/main/resources/application.yml

@ -5,30 +5,33 @@ spring:
##断路器
hystrix:
  metrics:
    polling-interval-ms: 20000 ##熔断间隔时间 默认是2秒 
#hystrix:
#  metrics:
#    enabled: true
#    polling-interval-ms: 20000 ##熔断间隔时间 默认是2秒
#zuul 默认会代理所有的微服务  路径 /{appliction.name}/**
zuul:
  ignored-services: '*'  ##忽略全部的代理  忽略单个微服务   ignored-services: svr-user 多个逗号分割
  ignored-services: '*'  ##忽略全部的代理  忽略单个微服务   ignored-services: svr-base 多个逗号分割
  routes:
    svr-user: /user/**  ##svr-user方向代理到/user下多层级的路径
#    svr-user:    这种方式和  svr-user: /user/**  一样  svr-user可以随便写 唯一即可
#      path: /user/**  path是代理后的路径
#      serviceId: svr-user  serviceId是微服务name
#   svr-user: 这种方式和 上面一样 就是serviceId改成具体的url 但是这种配置方法不能利用eurika的负载均衡
#     path: /user/**
    svr-base: /base/**  ##svr-base方向代理到/base下多层级的路径
#    svr-base:    这种方式和  svr-base: /base/**  一样  svr-base可以随便写 唯一即可
#      path: /base/**  path是代理后的路径
#      serviceId: svr-base  serviceId是微服务name
#   svr-base: 这种方式和 上面一样 就是serviceId改成具体的url 但是这种配置方法不能利用eurika的负载均衡
#     path: /base/**
#     url: http://localhost:10020/
#svr-user: 这边是微服务ID  配置负载均衡
#svr-base: 这边是微服务ID  配置负载均衡
#  ribbon:
#    listOfService: http://localhost:10020/,http://localhost:10021/
---
spring:
  profiles: dev

+ 0 - 2
web-gateway/src/main/resources/bootstrap.yml

@ -1,5 +1,3 @@
##优先读取 boostarap配置 然后在读取application。yml的配置
spring:
  #从发现服务里面取配置服务的信息