为什么不能从ui应用程序读取spring boot angularjs网关应用程序?
我正在使用以下链接中的教程了解spring的可伸缩性function。 具体来说, 本教程的第6部分使用网关应用程序来管理对其他服务器上运行的应用程序的访问。
我已经完全按照以下步骤操作,但是当我启动所有三个应用程序然后在我的网络浏览器中输入localhost:8080/ui
,我得到的是“问候”这个词,没有id或hello world,也没有css。
当我在Firefox中打开请求的开发人员工具时,我看到对css和js资源的GET请求获得404错误,指向http://localhost:8080/js/hello.js
而不是指向http://localhost:8080/ui/js/hello.js
,正如本教程的测试部分所示。 如何更改此设置以便在浏览器中显示问候语?
以下是我一步一步完成的工作,按照教程的第六步,首先从第一部分重新创建ui
起点,从第三部分重新创建resource
起点:
创建UI示例启动器应用程序
# mkdir ui # chmod -R 777 ui # cd ui # curl https://start.spring.io/starter.tgz -d style=web -d style=security -d name=ui | tar -xzvf -
Eclipse>文件>导入>现有Maven项目>导航到ui文件夹>完成
在src/main/resources/static
创建index.html
并添加以下内容:
Hello AngularJS [ng\:cloak], [ng-cloak], .ng-cloak { display: none !important; } Greeting
The ID is {{greeting.id}}
The content is {{greeting.content}}
src/main/resources/application.properties
添加到src/main/resources/application.properties
:
security.user.password=some.password server.port: 8081 security.sessions: NEVER // The "security.sessions" setting means that Spring Security will accept cookies as authentication tokens but won't create them unless they already exist.
将以下内容添加到pom.xml
中,以便使用wro4j下载和集成angular和bootstrap等:
${project.basedir}/src/main/resources ${project.build.directory}/generated-resources org.springframework.boot spring-boot-maven-plugin maven-resources-plugin copy-resources validate copy-resources ${basedir}/target/wro src/main/wro true ro.isdc.wro4j wro4j-maven-plugin 1.7.6 generate-resources run ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory ${project.build.directory}/generated-resources/static/css ${project.build.directory}/generated-resources/static/js ${project.build.directory}/wro/wro.xml ${basedir}/src/main/wro/wro.properties ${basedir}/src/main/wro org.webjars jquery 2.1.1 org.webjars angularjs 1.3.8 org.webjars bootstrap 3.2.0
Eclipse>文件>新建>源文件夹>(创建src / main / wro)
将以下内容添加到src/main/wro/wro.properties
(将从less编译css并缩小javascript):
preProcessors=lessCssImport postProcessors=less4j,jsMin
将以下内容添加到src/main/wro/wro.xml
(声明单个组angular-bootstrap,引用css,js和main.less):
webjar:bootstrap/3.2.0/less/bootstrap.less file:${project.basedir}/src/main/wro/main.less webjar:jquery/2.1.1/jquery.min.js webjar:angularjs/1.3.8/angular.min.js
创建src/main/wro/main.less
并src/main/wro/main.less
将其留空。 main.less
可用于自定义bootstrap默认值。
创建src/main/resources/static/js/hello.js
并添加以下内容:
angular.module('hello', []) .controller('home', function($scope, $http) { $http.get('/resource/').success(function(data) { $scope.greeting = data; }) });
将com.example.UiApplication.java
更改为:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @SpringBootApplication @EnableRedisHttpSession public class UiApplication { public static void main(String[] args) { SpringApplication.run(UiApplication.class, args); } }
//现在将以下内容添加到ui app的pom.xml中:
org.springframework.session spring-session org.springframework.boot spring-boot-starter-redis
创建单独的资源服务器
//导航到工作区的根目录
# cd /home/username/someworkspace # mkdir resource # chmod -R 777 resource # cd resource # curl https://start.spring.io/starter.tgz -d style=web -d name=resource -d language=java | tar -xzvf -
Eclipse>导入>现有Maven项目(导航到刚刚创建的资源文件夹)
//将以下内容添加到resource
应用程序中的pom.xml
:
org.springframework.session spring-session org.springframework.boot spring-boot-starter-redis
//将com.example.ResourceApplication.java
更改为以下内容:
import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @EnableRedisHttpSession public class ResourceApplication { @RequestMapping("/resource") public Map home() { Map model = new HashMap(); model.put("id", UUID.randomUUID().toString()); model.put("content", "Hello World"); return model; } public static void main(String[] args) { SpringApplication.run(ResourceApplication.class, args); } }
//将以下内容添加到src/main/resources/application.properties
:
server.port: 9000 security.sessions: NEVER
创建网关应用程序
然后将终端导航到工作空间的根目录
# cd /home/username/someworkspace # mkdir gateway # chmod -R 777 gateway # cd gateway # curl https://cloud-start.spring.io/starter.tgz -d style=web -d style=security -d style=cloud-zuul -d name=gateway -d style=redis | tar -xzvf -
Eclipse>文件>导入>现有Maven项目>(导航到网关目录)
//将GatewayApplication.java
更改为:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @SpringBootApplication @EnableRedisHttpSession @EnableZuulProxy public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
//将src/main/resources/application.properties
更改为:
zuul.routes.ui.url: http://localhost:8081 zuul.routes.resource.url: http://localhost:9000 security.user.password: password security.sessions: ALWAYS
。
启动并测试应用:
# cd /home/username/someworkspace/ui # mvn spring-boot:run
然后打开第二个终端并输入
# cd /home/username/someworkspace/resource # mvn spring-boot:run
然后打开第三个终端并键入:
# cd /home/username/someworkspace/gateway # mvn spring-boot: run
//通过在浏览器中输入localhost:8080 / ui来测试应用程序
请注意,只有“问候”一词出现在浏览器localhost:8080/ui
,并且没有id和内容。 此外,firefox开发人员工具显示404 http://localhost:8080/js/hello.js
等资源的错误, 而不应该是 http://localhost:8080/ui/js/hello.js
但是,当我在浏览器中键入localhost:8081
时,我得到了css样式的“Greeting”,后跟“ID is”和“The content is”,但没有来自资源服务器的动态内容。 localhost:8081
请求的firefox开发人员工具为http://localhost:8081/resource/
提供404。
请注意,要测试对上述内容的任何更改,只需在相应的控制台中键入control C,然后键入kill $(lsof -t -i:8080)
或8081或9000,然后键入mvn spring-boot:run
那么我对上面的代码做了哪些更改来获取通过网关加载的id和问候语?
看起来像你的HTML页面中的问题。 您应该确保链接正确。 Zuul本质上做的是将URL从网关映射到后端。 要使您的示例工作,您必须将角度应用程序中的URL更改为/resource/resource
或将资源应用程序中的控制器更改为/
。 还要确保所有应用程序都具有spring-boot-starter-security作为依赖项,以允许共享sec上下文。 或者完全禁用安全性以首先调试您的问题。