Skip to content

šŸ“•Curso referente Ć  MicrosserviƧos Java com Spring Boot e Spring Cloud - Sistema composto por vĆ”rios microsserviƧos que comunicam entre si de forma transparente, escalĆ”vel e com balanceamento de carga

Notifications You must be signed in to change notification settings

GiselleBarbosa/java-ms-course-devsuperior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Ā 

History

53 Commits
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 

Repository files navigation

Curso MicrosserviƧos Java com Spring Boot e Spring Cloud

Note

VersƵes: Java 11 e Spring Boot 2.3.4

ReferĆŖncia: https://www.udemy.com/user/nelio-alves

Checklist baixar e executar projeto pronto


Fase 1: ComunicaĆ§Ć£o simples, Feign, Ribbon

1.1 Criar projeto hr-worker

1.2 Implementar projeto hr-worker

Script SQL

INSERT INTO tb_worker (name, daily_Income) VALUES ('Bob', 200.0);
INSERT INTO tb_worker (name, daily_Income) VALUES ('Maria', 300.0);
INSERT INTO tb_worker (name, daily_Income) VALUES ('Alex', 250.0);

application.properties

spring.application.name=hr-worker
server.port=8001

# Database configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

1.3 Criar projeto hr-payroll

application.properties

spring.application.name=hr-payroll
server.port=8101

1.4 Implementar projeto hr-payroll (mock)

1.5 RestTemplate

1.6 Feign

1.7 Ribbon load balancing

Run configuration

-Dserver.port=8002

Fase 2: Eureka, Hystrix, Zuul

2.1 Criar projeto hr-eureka-server

2.2 Configurar hr-eureka-server

Porta padrĆ£o: 8761

Acessar o dashboard no navegador: http://localhost:8761

2.3 Configurar clientes Eureka

Eliminar o Ribbon de hr-payroll:

  • DependĆŖncia Maven
  • Annotation no programa principal
  • ConfiguraĆ§Ć£o em application.properties

AtenĆ§Ć£o: aguardar um pouco depois de subir os microsserviƧos

2.4 Random port para hr-worker

server.port=${PORT:0}

eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}

AtenĆ§Ć£o: deletar as configuraƧƵes mĆŗltiplas de execuĆ§Ć£o de hr-worker

2.5 TolerĆ¢ncia a falhas com Hystrix

2.6 Timeout de Hystrix e Ribbon

AtenĆ§Ć£o: testar antes sem a annotation do Hystrix

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
ribbon.ConnectTimeout=10000
ribbon.ReadTimeout=20000

2.7 Criar projeto hr-zuul-server

2.8 Configurar hr-zuul-server

Porta padrĆ£o: 8765

2.9 Random port para hr-payroll

2.10 Zuul timeout

Mesmo o timeout de Hystrix e Ribbon configurado em um microsserviƧo, se o Zuul nĆ£o tiver seu timeout configurado, para ele serĆ” um problema de timeout. EntĆ£o precisamos configurar o timeout no Zuul.

Se o timeout estiver configurado somente em Zuul, o Hystrix vai chamar o mƩtodo alternativo no microsserviƧo especƭfico.

Fase 3: ConfiguraĆ§Ć£o centralizada

3.1 Criar projeto hr-config-server

3.2 Configurar projeto hr-config-server

Quando um microsserviƧo Ć© levantado, antes de se registrar no Eureka, ele busca as configuraƧƵes no repositĆ³rio central de configuraƧƵes.

hr-worker.properties

test.config=My config value default profile

hr-worker-test.properties

test.config=My config value test profile

Teste:

http://localhost:8888/hr-worker/default
http://localhost:8888/hr-worker/test

3.3 hr-worker como cliente do servidor de configuraĆ§Ć£o, profiles ativos

No arquivo bootstrap.properties configuramos somente o que for relacionado com o servidor de configuraĆ§Ć£o, e tambĆ©m o profile do projeto.

AtenĆ§Ć£o: as configuraƧƵes do bootstrap.properties tem prioridade sobre as do application.properties

3.4 Actuator para atualizar configuraƧƵes em runtime

AtenĆ§Ć£o: colocar @RefreshScope em toda classe que possua algum acesso Ć s configuraƧƵes

3.5 RepositĆ³rio Git privativo

AtenĆ§Ć£o: reinicie a IDE depois de adicionar as variĆ”veis de ambiente

Fase 4: autenticaĆ§Ć£o e autorizaĆ§Ć£o

4.1 Criar projeto hr-user

4.2 Configurar projeto hr-user

4.3 Entidades User, Role e associaĆ§Ć£o N-N

4.4 Carga inicial do banco de dados

INSERT INTO tb_user (name, email, password) VALUES ('Nina Brown', 'nina@gmail.com', '$2a$10$NYFZ/8WaQ3Qb6FCs.00jce4nxX9w7AkgWVsQCG6oUwTAcZqP9Flqu');
INSERT INTO tb_user (name, email, password) VALUES ('Leia Red', 'leia@gmail.com', '$2a$10$NYFZ/8WaQ3Qb6FCs.00jce4nxX9w7AkgWVsQCG6oUwTAcZqP9Flqu');

INSERT INTO tb_role (role_name) VALUES ('ROLE_OPERATOR');
INSERT INTO tb_role (role_name) VALUES ('ROLE_ADMIN');

INSERT INTO tb_user_role (user_id, role_id) VALUES (1, 1);
INSERT INTO tb_user_role (user_id, role_id) VALUES (2, 1);
INSERT INTO tb_user_role (user_id, role_id) VALUES (2, 2);

4.5 UserRepository, UserResource, Zuul config

4.6 Criar projeto hr-oauth

4.7 Configurar projeto hr-oauth

4.8 UserFeignClient

4.9 Login e geraĆ§Ć£o do Token JWT

Source -> Override -> configure(AuthenticationManagerBuilder)

Source -> Override -> authenticationManager()

Basic authorization = "Basic " + Base64.encode(client-id + ":" + client-secret)

4.10 AutorizaĆ§Ć£o de recursos pelo gateway Zuul

4.11 Deixando o Postman top

VariƔveis:

- api-gateway: http://localhost:8765
- config-host: http://localhost:8888
- client-name: CLIENT-NAME
- client-secret: CLIENT-SECRET
- username: leia@gmail.com
- password: 123456
- token:

Script para atribuir token Ơ variƔvel de ambiente do Postman:

if (responseCode.code >= 200 && responseCode.code < 300) {
    var json = JSON.parse(responseBody);
    postman.setEnvironmentVariable('token', json.access_token);
}

4.12 ConfiguraĆ§Ć£o de seguranƧa para o servidor de configuraĆ§Ć£o

4.13 Configurando CORS

Teste no navegador:

fetch("http://localhost:8765/hr-worker/workers", {
  "headers": {
    "accept": "*/*",
    "accept-language": "en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "cross-site"
  },
  "referrer": "http://localhost:3000",
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": null,
  "method": "GET",
  "mode": "cors",
  "credentials": "omit"
});

About

šŸ“•Curso referente Ć  MicrosserviƧos Java com Spring Boot e Spring Cloud - Sistema composto por vĆ”rios microsserviƧos que comunicam entre si de forma transparente, escalĆ”vel e com balanceamento de carga

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages