經過一段時間的部門動盪,框架的事終於轉交給其他部門了。雖然以後不再是我操心的事,但是我要把這些經歷記錄下來,與朋友們分享。個別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表單提交上來的文件即可。
……
以後想起來再補充。