项目作者: dev-otitarenko

项目描述 :
Simple microservices project with docker, angular, auth and resource servers, openfeign
高级语言: Java
项目地址: git://github.com/dev-otitarenko/microservices-sample-auth-angular.git


Description

Sample of using microservices in docker containers. Used libraries: OAuth, Security, OpenFeign, Lombok.
All services are logging in ELK stack.

Calling authorized service’s methods from other service in your application

  1. @Component
  2. public class AuthRequestInterceptor implements RequestInterceptor {
  3. @Override
  4. public void apply(RequestTemplate requestTemplate) {
  5. Authentication _auth = SecurityContextHolder.getContext().getAuthentication();
  6. if (_auth != null) {
  7. if (_auth.getDetails() instanceof OAuth2AuthenticationDetails) {
  8. String _token = ((OAuth2AuthenticationDetails) _auth.getDetails()).getTokenValue();
  9. requestTemplate.header("Authorization", String.format("Bearer %s", _token));
  10. }
  11. }
  12. }
  13. }

Avoiding using gateway (default.nginx.conf)

  1. location / {
  2. try_files $uri $uri/ /index.html?$args;
  3. }
  4. location /api/oauth {
  5. proxy_set_header X-Real-IP $remote_addr;
  6. proxy_set_header X-Forwarded-Host $host;
  7. proxy_set_header X-Forwarded-Server $host;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. proxy_pass_header X-XSRF-TOKEN;
  10. proxy_pass http://auth-server:8811/oauth;
  11. }
  12. location /api/svc1 {
  13. proxy_set_header X-Real-IP $remote_addr;
  14. proxy_set_header X-Forwarded-Host $host;
  15. proxy_set_header X-Forwarded-Server $host;
  16. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  17. proxy_pass_header X-XSRF-TOKEN;
  18. proxy_pass http://service1:8812/svc1;
  19. }

Installing ELK

Before running your application you should start Elastic Stack tools on your machine.
The best way (simply way) to do that is through Docker.
Firstly, let’s create the network:

  1. $ docker network create app_net

Secondly, let’s run ElasticSearch:

  1. $ docker run -d --name elasticsearch --net app_net -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:latest

In next step, let’s run Logbash:

  1. $ docker run -d --name logstash --net app_net -p 5000:5000 -v ~/logstash.conf:/usr/share/logstash/pipeline/logstash.conf docker.elastic.co/logstash/logstash:latest
  1. input {
  2. tcp {
  3. port => 5000
  4. codec => json
  5. }
  6. }
  7. output {
  8. elasticsearch {
  9. hosts => ["http://elasticsearch:9200"]
  10. index => "micro-%{appname}"
  11. }
  12. }

Finally, let’s run Kibana:

  1. 1
  2. $ docker run -d --name kibana --net app_net -e "ELASTICSEARCH_URL=http://elasticsearch:9200" -p 5601:5601 docker.elastic.co/kibana/kibana:latest

Alternative way of installing ELK

[https://github.com/deviantony/docker-elk#how-to-configure-elasticsearch]

Running db

  1. docker-compose -f ./docker-compose-db.yml up -d --build

or

  1. sh ./build-db.sh

Running the application

  1. mvn clean package
  2. docker-compose -f ./docker-compose.yml up -d --build

or

  1. sh ./build-dev.sh

Application Endpoints

Service Port Endpoint
AuthService 8811 http://localhost:8811/oauth/
Service1 8812 http://localhost:8812/svc1/
Service2 8813 http://localhost:8813/svc2/