jhipster-架构优化技巧总结

经过一段时间的部门动荡,框架的事终于转交给其他部门了。虽然以后不再是我操心的事,但是我要把这些经历记录下来,与朋友们分享。个别jhipster的配置问题不要让新手们花太多时间了。

 

一、关于UAA

建议采用OAuth2的模式。安全性较高,而且jhipster对这一块封装得比较好。通过jhipster拉取源代码的顺序依次是先uua后网关的。最后注册中心+网关+uaa直接自成体系的能跑起来。

二、如何不通过鉴权,不需要登录,直接调用接口返回数据?

也就是接口公开暴露,有些数据采集时需要这种接口。

在SecurityConfiguration中配置即可,例如

三、关于跨服务调用

在不同微服务之间调用接口,以下举例在业务代码中调用uaa,其实其他的微服务之间调用也是一样的。

package myapp._frame;

import myapp._frame.dto.UserDto;
import myapp.client.AuthorizedFeignClient;
import myapp.config.Constants;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@AuthorizedFeignClient(name="uaa") //调用uaa服务。被调用服务的名称
public interface CapolUaaClient {
    @RequestMapping(value ="/api/account",method = RequestMethod.GET)
    UserDto getAccount();

    @RequestMapping(value ="/api/users/{login}",method = RequestMethod.GET)
    ResponseEntity<UserDto> getUser(@PathVariable("login") String login);

    @RequestMapping(value ="/api/users-byid/{id}",method = RequestMethod.GET)
    ResponseEntity<UserDto> getUserById(@PathVariable("id") Long id);
}

三、前后端分离配置

这里有个坑,具体报错不记得了。当时用jhipster搭建前后端分离,后端的接口老是没办法被前端调用。前端是另一个同时在开发,一直纠结怎么编辑header 的Cookie和Set-Cookie,其实理解错了。本身是CORS的问题。

它需要配置的地方还挺简单的。

首先,还是SecurityConfiguration。网关(gateway)的SecurityConfiguration。加一句

http.csrf().disable();

另外 查找 \src\main\resources\config\application-dev.yml,

注意以下代码不要被注释

 cors:
        allowed-origins: "*"
        allowed-methods: "*"
        allowed-headers: "*"
        exposed-headers: "Authorization,Link,X-Total-Count"
        allow-credentials: true
        max-age: 1800

四、微服务之间的调用,经常出现通讯超时。

这个问题能从配置上改进。然后再通过建立多实例,避免并发的时候存在超时的情况。

在网关(gateway)中配置文件\src\main\resources\config\application.yml配置ribbon:

ribbon:
    eureka:
        enabled: true
    eager-load:
        enabled: true
        clients: uaa,myapp1,myapp2#我的微服务名称
    ReadTimeout: 100000
    ConnectTimeout: 100000

配置超时

zuul: # those values must be configured depending on the application specific needs
    sensitive-headers: Cookie,Set-Cookie #see https://github.com/spring-cloud/spring-cloud-netflix/issues/3126
    host:
        max-total-connections: 10000
        max-per-route-connections: 1000
        connect-timeout-millis: 60000
        socket-timeout-millis: 100000
    semaphore:
        max-semaphores: 500
hystrix:
    command:
        default:
            execution:
                timeout: false
                isolation:
                    thread:
                        timeoutInMilliseconds: 60000

五、微服务实现文件上传下载

文件上传下载的文件大小配置呢,其实是被网关限制的。本身应用的微服务并没有影响。

在网关的配置文件\src\main\resources\config\application.yml配置

spring:
    application:
        name: gateway
    servlet:
      multipart:
        enabled: true
        maxFileSize: 1024Mb
        maxRequestSize: 1024Mb

其次是在具体的文件服务中,通过HttpServletRequest拿到前端通过Form表单提交上来的文件即可。

……

以后想起来再补充。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章