瀏覽代碼

eureka 放置到broker

huangzhiyong 8 年之前
父節點
當前提交
9b05a97fcb

+ 4 - 0
hos-broker/pom.xml

@ -127,6 +127,10 @@
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-restlet</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- camel end -->
    </dependencies>
    <build>

+ 11 - 3
hos-broker/src/main/java/com/yihu/hos/broker/controllers/GatewayController.java

@ -5,12 +5,11 @@ import com.yihu.hos.core.exception.ESBException;
import com.yihu.hos.broker.models.GatewayRequestResult;
import com.yihu.hos.broker.models.GatewayResponseResult;
import com.yihu.hos.broker.services.GatewayService;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -55,4 +54,13 @@ public class GatewayController {
        writer.flush();
        writer.close();
    }
    @RequestMapping(value = "/webservice/host", method = RequestMethod.GET)
    public String wsHost(
            @ApiParam(name = "serviceName", value = "微服务名", required = true)
            @RequestParam String serviceName) throws Exception {
        String uri = gatewayService.serviceUrl(serviceName);
        return uri;
    }
}

+ 23 - 0
hos-broker/src/main/java/com/yihu/hos/broker/services/GatewayService.java

@ -8,11 +8,14 @@ import com.yihu.hos.core.http.HttpClientKit;
import com.yihu.hos.broker.models.GatewayRequestResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
@ -27,6 +30,26 @@ public class GatewayService {
        this.gatewayConfiguration = gatewayConfiguration;
    }
    @Autowired
    private DiscoveryClient discoveryClient;
    /**
     * 通过服务名获取微服务地址
     *
     * @param serviceName 微服务名
     * @return
     */
    public String serviceUrl(String serviceName) {
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        if (instances != null && !instances.isEmpty()) {
            int index = (int) (Math.random() * instances.size());//随机获取其中一个服务
            ServiceInstance serviceInstance = instances.get(index);
            return serviceInstance.getUri().toString();
        } else {
            return null;
        }
    }
    public String getResultData(GatewayRequestResult gatewayRequestResult) throws ESBException {
        String returnData;

+ 5 - 1
hos-broker/src/main/resources/application.yml

@ -33,12 +33,16 @@ spring:
  camel.gateway:
    ip: localhost
    port: 8066
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.1.221:8761/eureka/
log:
  path: D://logs/logback/dev
  level: info
hos:
  filesystem:
    url: http://127.0.0.1:9010/dfs/file
    url: http://172.19.103.57:9010/dfs/file
  arbiter:
    enable: true
    url: http://localhost:10135

+ 4 - 0
hos-camel2/pom.xml

@ -85,6 +85,10 @@
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-ws</artifactId>
        </dependency>
    </dependencies>
</project>

+ 9 - 11
hos-camel2/src/main/java/camel/gateway/processor/GatewayProcessor.java

@ -8,7 +8,6 @@ import com.yihu.hos.core.http.HttpClientKit;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
@ -17,7 +16,6 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -27,9 +25,7 @@ import java.util.stream.Stream;
public class GatewayProcessor implements Processor {
    static Map<String, AppApi> apiMap = new HashMap<>();
    static final String agUrl = "http://192.168.1.221:10000";
    @Autowired
    ParamVerifyBean paramSign;
    static final String brokerUrl = "http://127.0.0.1:8099";
    @Autowired
    private DiscoveryClient discoveryClient;
@ -119,6 +115,7 @@ public class GatewayProcessor implements Processor {
        try {
            String sign = params.get("sign").toString();                     // 簽名
            ParamVerifyBean paramSign = new ParamVerifyBean();
            paramSign.addParam(params);
//            paramSign.genParam();
            //TODO 获取app secret传入验证
@ -275,13 +272,14 @@ public class GatewayProcessor implements Processor {
     * @return
     */
    public String serviceUrl(String serviceName) {
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        if (instances != null && !instances.isEmpty()) {
            ServiceInstance serviceInstance = instances.get(0);
            return serviceInstance.getUri().toString();
        } else {
            return null;
        HTTPResponse response = HttpClientKit.get(brokerUrl + "/esb/webservice/host?serviceName=" + serviceName);
        if (response.getStatusCode() != 200) {
            System.out.println("获取服务地址请求失败!");
            return "";
        }
        return response.getBody();
    }
}

+ 0 - 2
hos-camel2/src/main/java/camel/gateway/processor/ParamVerifyBean.java

@ -1,7 +1,6 @@
package camel.gateway.processor;
import com.yihu.hos.core.encrypt.MD5;
import org.springframework.stereotype.Component;
import java.util.Iterator;
import java.util.Map;
@ -12,7 +11,6 @@ import java.util.TreeMap;
 * @vsrsion 1.0
 * Created at 2017/3/15.
 */
@Component
public class ParamVerifyBean {
    private final String version = "1.0";

+ 2 - 2
src/main/resources/application.yml

@ -55,9 +55,9 @@ spring:
    proxy-target-class: true
hos:
  zbus:
    url: 127.0.0.1:9020
    url: 172.19.103.57:9020
  filesystem:
    url: http://192.168.131.101:9010/dfs/file
    url: http://172.19.103.57:9010/dfs/file
  mysql:
    filePath: e://learn.sql   #租户基础表 sql文件位置
service-gateway: