소스 검색

优化代码

wuxw 1 년 전
부모
커밋
47b9f7a270

+ 1 - 0
changeBootEnvLinux.sh

@@ -11,6 +11,7 @@ cp -r service-report/pom-boot.xml service-report/pom.xml
 cp -r service-store/pom-boot.xml service-store/pom.xml
 cp -r service-user/pom-boot.xml service-user/pom.xml
 cp -r service-scm/pom-boot.xml service-scm/pom.xml
+cp -r service-api/pom-boot.xml service-api/pom.xml
 
 
 

+ 8 - 0
changeBootEnvWindow.bat

@@ -96,3 +96,11 @@ set a=%%i
 echo !a!>>$)
 
 move $ service-scm\pom.xml
+
+for /f "delims=" %%i in (service-api\pom-boot.xml) do (
+
+set a=%%i
+
+echo !a!>>$)
+
+move $ service-api\pom.xml

+ 1 - 0
changeCloudEnvLinux.sh

@@ -11,6 +11,7 @@ cp -r service-report/pom-cloud.xml service-report/pom.xml
 cp -r service-store/pom-cloud.xml service-store/pom.xml
 cp -r service-user/pom-cloud.xml service-user/pom.xml
 cp -r service-scm/pom-cloud.xml service-scm/pom.xml
+cp -r service-api/pom-cloud.xml service-api/pom.xml
 
 
 

+ 8 - 0
changeCloudEnvWindow.bat

@@ -96,3 +96,11 @@ set a=%%i
 echo !a!>>$)
 
 move $ service-scm\pom.xml
+
+for /f "delims=" %%i in (service-api\pom-cloud.xml) do (
+
+set a=%%i
+
+echo !a!>>$)
+
+move $ service-api\pom.xml

+ 3 - 0
java110-core/src/main/java/com/java110/core/context/Environment.java

@@ -39,6 +39,9 @@ public class Environment {
 
     private static String systemStartWay = "CLOUD"; // 环境是spring boot cloud
 
+    public static final String BOOT_PATH = "http://127.0.0.1:8008";
+
+
     /**
      * 环境变量
      *

+ 63 - 0
service-api/pom-boot.xml

@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
+    <parent>
+        <artifactId>MicroCommunity</artifactId>
+        <groupId>com.java110</groupId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>service-api</artifactId>
+
+    <name>service-api</name>
+    <!-- FIXME change it to the project's website -->
+    <url>http://www.example.com</url>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.java110</groupId>
+            <artifactId>java110-service</artifactId>
+            <exclusions>
+                <exclusion>
+                    <artifactId>java110-db</artifactId>
+                    <groupId>com.java110</groupId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <groupId>com.java110</groupId>
+            <artifactId>java110-interface</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.11</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.vaadin.external.google</groupId>
+            <artifactId>android-json</artifactId>
+            <version>0.0.20131108.vaadin1</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
+        </dependency>
+
+    </dependencies>
+
+
+</project>

+ 120 - 0
service-api/pom-cloud.xml

@@ -0,0 +1,120 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
+	<parent>
+		<artifactId>MicroCommunity</artifactId>
+		<groupId>com.java110</groupId>
+		<version>1.0-SNAPSHOT</version>
+	</parent>
+	<modelVersion>4.0.0</modelVersion>
+
+	<artifactId>service-api</artifactId>
+
+	<name>service-api</name>
+	<!-- FIXME change it to the project's website -->
+	<url>http://www.example.com</url>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<maven.compiler.source>1.8</maven.compiler.source>
+		<maven.compiler.target>1.8</maven.compiler.target>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>com.java110</groupId>
+			<artifactId>java110-service</artifactId>
+			<exclusions>
+				<exclusion>
+					<artifactId>java110-db</artifactId>
+					<groupId>com.java110</groupId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+
+		<dependency>
+			<groupId>com.java110</groupId>
+			<artifactId>java110-interface</artifactId>
+			<scope>provided</scope>
+		</dependency>
+
+
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>com.vaadin.external.google</groupId>
+			<artifactId>android-json</artifactId>
+			<version>0.0.20131108.vaadin1</version>
+			<scope>compile</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-websocket</artifactId>
+		</dependency>
+
+	</dependencies>
+
+	<build>
+		<finalName>service-api</finalName>
+
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-dependency-plugin</artifactId>
+				<version>2.10</version>
+				<executions>
+					<execution>
+						<id>unpack</id>
+						<phase>generate-resources</phase>
+						<goals>
+							<goal>unpack</goal>
+						</goals>
+						<configuration>
+							<artifactItems>
+								<artifactItem>
+									<groupId>com.java110</groupId>
+									<artifactId>java110-interface</artifactId>
+									<version>${microcommunity.version}</version>
+									<type>jar</type>
+									<overWrite>true</overWrite>
+									<outputDirectory>${project.build.directory}/classes</outputDirectory>
+								</artifactItem>
+							</artifactItems>
+						</configuration>
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+				<configuration>
+					<mainClass>com.java110.api.ApiApplicationStart</mainClass>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-javadoc-plugin</artifactId>
+				<version>2.10.4</version>
+				<configuration>
+					<aggregate>true</aggregate>
+					<reportOutputDirectory>../javadocs</reportOutputDirectory>
+					<destDir>security-javadoc</destDir>
+					<javadocExecutable>${java.home}bin/javadoc</javadocExecutable>
+					<tags>
+						<tag>
+							<name>Description</name>
+							<placement>a</placement>
+							<head>功能描述:</head>
+						</tag>
+					</tags>
+				</configuration>
+			</plugin>
+		</plugins>
+
+	</build>
+</project>

+ 0 - 57
service-api/pom.xml

@@ -59,62 +59,5 @@
 
     </dependencies>
 
-    <build>
-        <finalName>service-api</finalName>
 
-        <plugins>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-dependency-plugin</artifactId>
-                <version>2.10</version>
-                <executions>
-                    <execution>
-                        <id>unpack</id>
-                        <phase>generate-resources</phase>
-                        <goals>
-                            <goal>unpack</goal>
-                        </goals>
-                        <configuration>
-                            <artifactItems>
-                                <artifactItem>
-                                    <groupId>com.java110</groupId>
-                                    <artifactId>java110-interface</artifactId>
-                                    <version>${microcommunity.version}</version>
-                                    <type>jar</type>
-                                    <overWrite>true</overWrite>
-                                    <outputDirectory>${project.build.directory}/classes</outputDirectory>
-                                </artifactItem>
-                            </artifactItems>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-            <plugin>
-                <groupId>org.springframework.boot</groupId>
-                <artifactId>spring-boot-maven-plugin</artifactId>
-                <configuration>
-                    <mainClass>com.java110.api.ApiApplicationStart</mainClass>
-                </configuration>
-            </plugin>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-javadoc-plugin</artifactId>
-                <version>2.10.4</version>
-                <configuration>
-                    <aggregate>true</aggregate>
-                    <reportOutputDirectory>../javadocs</reportOutputDirectory>
-                    <destDir>security-javadoc</destDir>
-                    <javadocExecutable>${java.home}bin/javadoc</javadocExecutable>
-                    <tags>
-                        <tag>
-                            <name>Description</name>
-                            <placement>a</placement>
-                            <head>功能描述:</head>
-                        </tag>
-                    </tags>
-                </configuration>
-            </plugin>
-        </plugins>
-
-    </build>
 </project>

+ 2 - 0
service-api/src/main/java/com/java110/api/ApiApplicationStart.java

@@ -17,6 +17,7 @@ package com.java110.api;
 
 import com.java110.core.annotation.Java110ListenerDiscovery;
 import com.java110.core.client.OutRestTemplate;
+import com.java110.core.context.Environment;
 import com.java110.core.trace.Java110FeignClientInterceptor;
 import com.java110.core.trace.Java110RestTemplateInterceptor;
 import com.java110.core.client.RestTemplate;
@@ -197,6 +198,7 @@ public class ApiApplicationStart {
             ApplicationContext context = SpringApplication.run(ApiApplicationStart.class, args);
             //服务启动加载
             ServiceStartInit.initSystemConfig(context);
+            Environment.setSystemStartWay(Environment.SPRING_CLOUD);
             //服务启动完成
             ServiceStartInit.printStartSuccessInfo();
         } catch (Throwable e) {

+ 37 - 11
service-api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java

@@ -7,6 +7,7 @@ import com.java110.api.smo.IApiServiceSMO;
 import com.java110.core.client.RestTemplate;
 import com.java110.core.context.ApiDataFlow;
 import com.java110.core.context.DataFlow;
+import com.java110.core.context.Environment;
 import com.java110.core.event.service.api.ServiceDataFlowEventPublishing;
 import com.java110.core.factory.AuthenticationFactory;
 import com.java110.core.factory.DataFlowFactory;
@@ -26,6 +27,7 @@ import com.java110.utils.constant.KafkaConstant;
 import com.java110.utils.constant.MappingConstant;
 import com.java110.utils.constant.ResponseConstant;
 import com.java110.utils.exception.*;
+import com.java110.utils.factory.ApplicationContextFactory;
 import com.java110.utils.kafka.KafkaFactory;
 import com.java110.utils.log.LoggerEngine;
 import com.java110.utils.util.BootReplaceUtil;
@@ -424,6 +426,12 @@ public class ApiServiceSMOImpl extends LoggerEngine implements IApiServiceSMO {
 
         ResponseEntity responseEntity = null;
         //配置c_service 时请注意 如果是以out 开头的调用外部的地址
+        RestTemplate restTemplate ;
+        if (Environment.isStartBootWay()) {
+            restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
+        } else {
+            restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+        }
 
         try {
             if (CommonConstant.HTTP_METHOD_GET.equals(service.getMethod())) {
@@ -439,9 +447,9 @@ public class ApiServiceSMOImpl extends LoggerEngine implements IApiServiceSMO {
 
                 requestUrl = BootReplaceUtil.replaceServiceName(requestUrl);
 
-                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
             } else if (CommonConstant.HTTP_METHOD_PUT.equals(service.getMethod())) {
-                responseEntity = outRestTemplate.exchange(service.getUrl(), HttpMethod.PUT, httpEntity, String.class);
+                responseEntity = restTemplate.exchange(service.getUrl(), HttpMethod.PUT, httpEntity, String.class);
             } else if (CommonConstant.HTTP_METHOD_DELETE.equals(service.getMethod())) {
                 String requestUrl = dataFlow.getRequestHeaders().get("REQUEST_URL");
                 if (!StringUtil.isNullOrNone(requestUrl)) {
@@ -452,11 +460,11 @@ public class ApiServiceSMOImpl extends LoggerEngine implements IApiServiceSMO {
                         requestUrl = service.getUrl() + "?" + param;
                     }
                 }
-                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.DELETE, httpEntity, String.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.DELETE, httpEntity, String.class);
             } else {
 
                 String requestUrl = BootReplaceUtil.replaceServiceName(service.getUrl());
-                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
             }
         } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
             responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
@@ -481,7 +489,16 @@ public class ApiServiceSMOImpl extends LoggerEngine implements IApiServiceSMO {
         String serviceCode = appService.getServiceCode();
         serviceCode = serviceCode.startsWith("/") ? serviceCode : ("/" + serviceCode);
 
-        String requestUrl = "http://127.0.0.1:8008" + serviceCode;
+        //String requestUrl = "http://127.0.0.1:8008" + serviceCode;
+        String requestUrl = serviceCode;
+        RestTemplate restTemplate ;
+        if (Environment.isStartBootWay()) {
+            requestUrl = Environment.BOOT_PATH + requestUrl;
+            restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
+        } else {
+            requestUrl = appService.getUrl() + requestUrl;
+            restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+        }
 
         ResponseEntity responseEntity = null;
         if (!StringUtil.isNullOrNone(orgRequestUrl)) {
@@ -490,13 +507,13 @@ public class ApiServiceSMOImpl extends LoggerEngine implements IApiServiceSMO {
         }
         try {
             if (CommonConstant.HTTP_METHOD_GET.equals(appService.getMethod())) {
-                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
             } else if (CommonConstant.HTTP_METHOD_PUT.equals(appService.getMethod())) {
-                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.PUT, httpEntity, String.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.PUT, httpEntity, String.class);
             } else if (CommonConstant.HTTP_METHOD_DELETE.equals(appService.getMethod())) {
-                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.DELETE, httpEntity, String.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.DELETE, httpEntity, String.class);
             } else {
-                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
             }
             HttpHeaders headers = responseEntity.getHeaders();
             String oId = "-1";
@@ -584,7 +601,8 @@ public class ApiServiceSMOImpl extends LoggerEngine implements IApiServiceSMO {
         serviceCode = serviceCode.startsWith("/") ? serviceCode : ("/" + serviceCode);
 
         //todo 组装调用微服务的地址
-        String requestUrl = "http://127.0.0.1:8008/cmd" + serviceCode;
+        //String requestUrl = "http://127.0.0.1:8008/cmd" + serviceCode;
+        String requestUrl = "/cmd" + serviceCode;
         //
         ResponseEntity responseEntity = null;
         //todo url 带了地址这里 拼接
@@ -595,7 +613,15 @@ public class ApiServiceSMOImpl extends LoggerEngine implements IApiServiceSMO {
         try {
             //todo http的方式调用微服务,相应的java类可以到相应微服务下的cmd下根据serviceCode 的寻找
             //todo 这里会调用到 java110-service 模块下的 CmdApi 类,这个类各个微服务都会集成
-            responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
+            if (Environment.isStartBootWay()) {
+                requestUrl = Environment.BOOT_PATH + requestUrl;
+                restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
+            } else {
+                requestUrl = appService.getUrl() + requestUrl;
+                restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+                responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
+            }
             HttpHeaders headers = responseEntity.getHeaders();
             String oId = "-1";
             if (headers.containsKey(OrderDto.O_ID)) {

+ 1 - 1
service-api/src/main/resources/application-dev.yml

@@ -27,7 +27,7 @@ spring:
     name: api-service
   redis:
     database: 0
-    host: dev.redis.java110.com
+    host: 127.0.0.1
     port: 6379
     password: hc
     pool: