Browse Source

完善配置,增加说明

zhouhao 7 years ago
parent
commit
efe8dd7bd5
15 changed files with 207 additions and 19 deletions
  1. 67 0
      hsweb-examples/hsweb-examples-cloud/README.md
  2. 8 0
      hsweb-examples/hsweb-examples-cloud/build-docker.sh
  3. 17 0
      hsweb-examples/hsweb-examples-cloud/docker-compose.yml
  4. 22 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/pom.xml
  5. 4 2
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/resources/application.yml
  6. 21 1
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/pom.xml
  7. 0 11
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/java/org/hswebframework/web/examples/cloud/service/Service01Application.java
  8. 5 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application-docker.yml
  9. 4 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application.yml
  10. 5 1
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/bootstrap.yml
  11. 21 1
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/pom.xml
  12. 2 1
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/java/org/hswebframework/web/examples/cloud/user/UserCenterApplication.java
  13. 5 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/resources/application-docker.yml
  14. 5 1
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/resources/bootstrap.yml
  15. 21 1
      hsweb-examples/hsweb-examples-cloud/pom.xml

+ 67 - 0
hsweb-examples/hsweb-examples-cloud/README.md

@@ -0,0 +1,67 @@
+# spring-cloud 示例
+
+```bash
+----------hsweb-examples-cloud
+--------------hsweb-examples-cloud-gateway          #服务注册,路由.api的入口
+--------------hsweb-examples-cloud-service01        #1号测试服务,可从用户中心获取当前登录用户并进行权限控制
+--------------hsweb-examples-cloud-user-center      #用户中心,用于用户登录授权
+```
+
+# 启动
+
+## 1. main方法启动
+1. 执行`hsweb-examples-cloud-gateway`模块中的 `GateWayApplication`
+2. 执行`hsweb-examples-cloud-service01`模块中的 `Service01Application`
+3. 执行`hsweb-examples-cloud-user-center`模块中的 `UserCenterApplication`
+
+##2. 使用spring-boot插件启动
+
+分别进入3个模块,执行`mvn spring-boot:run`
+
+## 2. docker
+
+1. 执行 `./build-docker.sh` 构建docker镜像
+
+2. 执行 'docker-compose up' 等待服务启动完成
+
+# 访问
+访问: http://localhost:8761/
+
+# 测试
+
+1. 使用 `PostMan` 之类的工具发起POST请求:
+
+    http://localhost:8761/api/user-center/authorize/login?username=admin&password=admin&token_type=jwt
+
+拿到结果如下:
+```json
+{
+    "result": {
+        "userId": "b3d4ee054b8195e8ce2dbecedefbfb49",
+        "token": "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJoc3dlYi1qd3QiLCJpYXQiOjE1MDc3MDU4NzgsInN1YiI6IntcInRva2VuXCI6XCJjYTQ5MjlkZGJlYTY4Y2I4OWYwYTE0YzVjYWE4YTk5OFwiLFwidXNlcklkXCI6XCJiM2Q0ZWUwNTRiODE5NWU4Y2UyZGJlY2VkZWZiZmI0OVwifSIsImV4cCI6MTUwNzcwOTQ3OH0.R09HSDbxZgM6zoW0hDHhKDVP9nmKqilLpv8SHAZoS58"
+    },
+    "status": 200,
+    "timestamp": 1507705878257
+}
+```
+
+2. 得到上一步骤的结果,再次发起GET请求:
+
+   http://localhost:8761/api/service-1/user-info
+
+需要带上请求头: jwt-token:上一步返回json中的token。
+得到返回结果类似:
+```json
+    {
+        "attributes": {},
+        "permissions": [],
+        "roles": [],
+        "user": {
+            "id": "b3d4ee054b8195e8ce2dbecedefbfb49",
+            "name": "super user",
+            "username": "admin"
+        }
+    }
+```
+
+测试成功

+ 8 - 0
hsweb-examples/hsweb-examples-cloud/build-docker.sh

@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+mvn clean package
+cd hsweb-examples-cloud-gateway
+mvn docker:build
+cd ../hsweb-examples-cloud-service01
+mvn docker:build
+cd ../hsweb-examples-cloud-user-center
+mvn docker:build

+ 17 - 0
hsweb-examples/hsweb-examples-cloud/docker-compose.yml

@@ -0,0 +1,17 @@
+version: "2"
+services:
+    gateway:
+      image: hsweb/hsweb-examples-cloud-gateway:3.0-SNAPSHOT
+      ports:
+        - 8761:8761
+      hostname: gateway
+    user-center:
+      image: hsweb/hsweb-examples-cloud-user-center:3.0-SNAPSHOT
+      links:
+          - gateway:gateway
+      hostname: user-center
+    service01:
+      image: hsweb/hsweb-examples-cloud-service01:3.0-SNAPSHOT
+      links:
+          - gateway:gateway
+      hostname: service01

+ 22 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/pom.xml

@@ -11,6 +11,28 @@
 
     <artifactId>hsweb-examples-cloud-gateway</artifactId>
 
+    <build>
+        <finalName>${project.artifactId}</finalName>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>${spring.boot.version}</version>
+                <configuration>
+                    <mainClass>org.hswebframework.web.examples.cloud.gateway.GateWayApplication</mainClass>
+                    <layout>ZIP</layout>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>

+ 4 - 2
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/resources/application.yml

@@ -4,8 +4,8 @@ server:
   port: 8761
 eureka:
   client:
-    register-with-eureka: true
     fetch-registry: true
+    register-with-eureka: true
 zuul:
   prefix: /api
   routes:
@@ -15,7 +15,9 @@ zuul:
     service-1:
       path: /service-1/**
       service-id: service01
-  add-host-header: true
+  sensitive-headers: Cookies
+  host:
+    connect-timeout-millis: 10000
 ribbon:
   eureka:
     enabled: true

+ 21 - 1
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/pom.xml

@@ -10,7 +10,27 @@
     <modelVersion>4.0.0</modelVersion>
 
     <artifactId>hsweb-examples-cloud-service01</artifactId>
-
+    <build>
+        <finalName>${project.artifactId}</finalName>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>${spring.boot.version}</version>
+                <configuration>
+                    <mainClass>org.hswebframework.web.examples.cloud.service.Service01Application</mainClass>
+                    <layout>ZIP</layout>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
     <dependencies>
         <dependency>
             <groupId>com.alibaba</groupId>

+ 0 - 11
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/java/org/hswebframework/web/examples/cloud/service/Service01Application.java

@@ -1,16 +1,11 @@
 package org.hswebframework.web.examples.cloud.service;
 
 
-import feign.Feign;
-import org.hswebframework.web.authorization.cloud.feign.FeignUserTokenManager;
-import org.hswebframework.web.authorization.token.UserTokenManager;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.netflix.feign.EnableFeignClients;
-import org.springframework.cloud.netflix.feign.FeignClient;
 import org.springframework.cloud.netflix.hystrix.EnableHystrix;
-import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 @SpringBootApplication
@@ -20,12 +15,6 @@ import org.springframework.context.annotation.Configuration;
 @EnableFeignClients("org.hswebframework.web.authorization.cloud.feign")
 public class Service01Application {
 
-//    @Bean
-//   public UserTokenManager userTokenManager(){
-//      return Feign.builder().target(FeignUserTokenManager.class,"http://localhost:9000");
-//    }
-
-
     public static void main(String[] args) {
         SpringApplication.run(Service01Application.class, args);
     }

+ 5 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application-docker.yml

@@ -0,0 +1,5 @@
+eureka-host: gateway
+eureka-port: 8761
+spring:
+  datasource:
+     url : jdbc:h2:file:./data/service01

+ 4 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application.yml

@@ -11,6 +11,10 @@ hsweb:
     app:
       name: 权限管理测试
       version: 3.0.0
+    cloud:
+      user-center:
+        name: gateway
+        prefix: /api/user-center/
 server:
     port: 9001
 logging:

+ 5 - 1
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/bootstrap.yml

@@ -6,4 +6,8 @@ spring:
       client:
         simple:
           local:
-            service-id: service01
+            service-id: service01
+eureka:
+  client:
+    service-url:
+        defaultZone: http://${eureka-host:localhost}:${eureka-port:8761}/eureka/

+ 21 - 1
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/pom.xml

@@ -10,7 +10,27 @@
     <modelVersion>4.0.0</modelVersion>
 
     <artifactId>hsweb-examples-cloud-user-center</artifactId>
-
+    <build>
+        <finalName>${project.artifactId}</finalName>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>${spring.boot.version}</version>
+                <configuration>
+                    <mainClass>org.hswebframework.web.examples.cloud.user.UserCenterApplication</mainClass>
+                    <layout>ZIP</layout>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
 
     <dependencies>
         <dependency>

+ 2 - 1
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/java/org/hswebframework/web/examples/cloud/user/UserCenterApplication.java

@@ -1,12 +1,12 @@
 package org.hswebframework.web.examples.cloud.user;
 
-import org.h2.command.Command;
 import org.hswebframework.web.entity.authorization.UserEntity;
 import org.hswebframework.web.service.authorization.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.netflix.hystrix.EnableHystrix;
 import org.springframework.context.annotation.Configuration;
@@ -15,6 +15,7 @@ import org.springframework.context.annotation.Configuration;
 @EnableDiscoveryClient
 @EnableHystrix
 @Configuration
+@EnableCaching
 public class UserCenterApplication implements CommandLineRunner{
     public static void main(String[] args) {
         SpringApplication.run(UserCenterApplication.class, args);

+ 5 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/resources/application-docker.yml

@@ -0,0 +1,5 @@
+eureka-host: gateway
+eureka-port: 8761
+spring:
+  datasource:
+     url : jdbc:h2:file:./data/user-center

+ 5 - 1
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/resources/bootstrap.yml

@@ -6,4 +6,8 @@ spring:
       client:
         simple:
           local:
-            service-id: user-center
+            service-id: user-center
+eureka:
+  client:
+    service-url:
+       defaultZone: http://${eureka-host:localhost}:${eureka-port:8761}/eureka/

+ 21 - 1
hsweb-examples/hsweb-examples-cloud/pom.xml

@@ -16,7 +16,27 @@
         <module>hsweb-examples-cloud-user-center</module>
         <module>hsweb-examples-cloud-service01</module>
     </modules>
-
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>com.spotify</groupId>
+                <artifactId>docker-maven-plugin</artifactId>
+                <version>0.4.13</version>
+                <configuration>
+                    <imageName>hsweb/${project.artifactId}:${project.version}</imageName>
+                    <baseImage>java:8</baseImage>
+                    <entryPoint>["java","-jar","-Dspring.profiles.active=docker","${project.build.finalName}.jar"]</entryPoint>
+                    <resources>
+                        <resource>
+                            <targetPath>/</targetPath>
+                            <directory>${project.build.directory}</directory>
+                            <include>${project.build.finalName}.jar</include>
+                        </resource>
+                    </resources>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
     <dependencyManagement>
         <dependencies>
             <dependency>