- JDK 11, variƔveis PATH e JAVA_HOME
- Configurar IDE para pegar Java 11
- Importar projetos na IDE
- Configurar credenciais do config server:
- Preparar Postman (collection e environment)
- Subir projetos em ordem:
- Config server
- Eureka server
- Outros
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
application.properties
spring.application.name=hr-payroll
server.port=8101
Run configuration
-Dserver.port=8002
Porta padrĆ£o: 8761
Acessar o dashboard no navegador: http://localhost:8761
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
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
AtenĆ§Ć£o: testar antes sem a annotation do Hystrix
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
ribbon.ConnectTimeout=10000
ribbon.ReadTimeout=20000
Porta padrĆ£o: 8765
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.
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
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
AtenĆ§Ć£o: colocar @RefreshScope em toda classe que possua algum acesso Ć s configuraƧƵes
AtenĆ§Ć£o: reinicie a IDE depois de adicionar as variĆ”veis de ambiente
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);
Source -> Override -> configure(AuthenticationManagerBuilder)
Source -> Override -> authenticationManager()
Basic authorization = "Basic " + Base64.encode(client-id + ":" + client-secret)
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);
}
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"
});