I have application.yaml configuration file with few profiles:
server:
address: 0.0.0.0
port: 9090
db:
url: 'jdbc:postgresql://localhost:5432/the_db'
driver: 'org.postgresql.Driver'
username: 'postgres'
password: ''
---
spring:
profiles: devArtem
db:
url: 'jdbc:postgresql://localhost:5432/my_db'
---
spring:
profiles: prod_1
db:
password: 'first_pass'
---
spring:
profiles: prod_2
db:
password: 'second_pass'
And I want to remove other profiles before build jar file. I don't want to give access for prod_1's password to prod_2 platform for example.
For prod_1 it must be something like this:
server:
address: 0.0.0.0
port: 9090
db:
url: 'jdbc:postgresql://localhost:5432/the_db'
driver: 'org.postgresql.Driver'
username: 'postgres'
password: 'first_pass'
or this:
server:
address: 0.0.0.0
port: 9090
db:
url: 'jdbc:postgresql://localhost:5432/the_db'
driver: 'org.postgresql.Driver'
username: 'postgres'
password: ''
---
spring:
profiles: prod_1
db:
password: 'first_pass'
You can use multiple application-{profile}.yml and give each team the right file.
for example :
application-devArtem.yml ---> to team devArtem
application-prod_1.yml ---> to team prod_1
application-prod_2.yml ---> to team prod_2
Related
I'm trying to configure the Eureka port with Spring Cloud with a eureka server, and config server (which is also a eureka client). Eureka service is successfully deployed and got a ip address. My Goal is to get the instance under eureka server.
I'm getting below error
2022-05-24 12:26:10.914 ERROR 21280 --- [freshExecutor-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}
2022-05-24 12:03:09.673 WARN 21280 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator : There was a problem with the instance info replicator
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
This is my application.yml file from the test-service
spring:
datasource:
url: jdbc:oracle:thin:#//[ip address]/[address]
username: *******
password: *******
driver-class-name: oracle.jdbc.OracleDriver
profiles:
active:#activatedProperties#
jpa:
database-platform: org.hibernate.dialect.Oracle12cDialect
hibernate:
use-new-id-generator-mappings: false
ddl-auto: update
application:
name: test-service
eureka:
instance:
preferIpAddress: 'true'
client:
fetchRegistry: 'true'
registerWithEureka: 'true'
enabled: 'true'
service-url:
defaultZone: http://[username]:[password]#localhost:8761/eureka
server:
port: 8080
Debug: true
This is my eureka-serviece application.property file
server.port=8761
eureka.server.enable-self-preservation = false
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
spring.security.user.name=******
spring.security.user.password=*******
eureka.instance.preferIpAddress=true
In classpath:/db.migration have migrations V1__Strory_create.sql and V2__Task_create.sql but flyway dosen't see that and fall with msg
2022-05-02 17:28:07.993 INFO 45296 --- [ restartedMain]
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server
at : http://localhost:8888/story 2022-05-02 17:28:08.213 INFO 45296
--- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=story, profiles=[story], label=default,
version=null, state=null 2022-05-02 17:28:08.215 INFO 45296 --- [
restartedMain] b.c.PropertySourceBootstrapConfiguration : Located
property source: [BootstrapPropertySource
{name='bootstrapProperties-configClient'}, BootstrapPropertySource
{name='bootstrapProperties-classpath:/config/story.yml'}] 2022-05-02
17:28:08:331 INFO com.openhelp.story.StoryApplication - The following
1 profile is active: "default" 2022-05-02 17:28:11:368 DEBUG
com.zaxxer.hikari.HikariConfig - Driver class org.postgresql.Driver
found in Thread context class loader
org.springframework.boot.devtools.restart.classloader.RestartClassLoader#707d8885
2022-05-02 17:28:11:426 ERROR o.s.b.d.LoggingFailureAnalysisReporter -
*************************** APPLICATION FAILED TO START
Description:
Flyway failed to initialize: none of the following migration scripts
locations could be found:
classpath:db/migration
Action:
Review the locations above or check your Flyway configuration
Process finished with exit code 0
Configuration fetch from config service with bootstrap.yml
spring:
application:
name: story
profiles:
active: default
cloud:
config:
uri: ${vcap.services.configserver.credentials.uri:http://localhost:8888/story}
And fetched config
server:
port: 0
eureka:
instance:
instance-id: ${spring.application.name}:${random.uuid}
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
jpa:
generate-ddl: 'false'
properties:
hibernate:
jdbc:
time_zone: UTC
generate_statistics: 'false'
format_sql: 'true'
use_sql_comments: 'false'
show_sql: 'true'
hibernate:
ddl-auto: validate
open-in-view: 'false'
flyway:
baseline-on-migrate: 'true'
datasource:
driver-class-name: org.postgresql.Driver
password: '1'
username: postgres
url: jdbc:postgresql://127.0.0.1:5432/openhelp_story
autoconfigure:
exclude: org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
logging:
level:
org:
apache:
tomcat:
jdbc:
pool: debug
nodeValue: ERROR
springframework:
security: info
nodeValue: DEBUG
hibernate:
nodeValue: ERROR
jdbc: ERROR
stat: DEBUG
SQL: WARN
cache: ERROR
root: ERROR
com:
openhelp: debug
zaxxer:
hikari: debug
pattern:
console: '%d{yyyy-MM-dd HH:mm:ss:SSS} %-5level %logger{36} - %msg%n'
management:
endpoints:
jmx:
exposure:
include: health
openhelp:
logging:
path: logs
archive-path: logs/logs-archive
As we can see from log above the configuration fetched successully. Perhaps config is wrong?
Problem was in classpath:/db.migration instead of classpath:/db/migration. Accidentally I'm create one folder.
I add new syntax in application.yml but failed.
Config file structure:
- resources
- application.yml
- application-dev.yml
- application-test.yml
In application.yml:
spring:
config:
activate:
on-profile: dev
In application-dev.yml:
server:
port: 9010
spring:
application:
name: account-service
datasource:
url: jdbc:mysql:///account_db
username: root
password: root
Runtime:
2022-04-20 14:16:02.354 INFO 13796 --- [ main] c.lingyuango.seckill.SeckillServiceMain : No active profile set, falling back to 1 default profile: "default"
everyone. Forgive me for my poor English and strange grammar. I'm just beginning to learn Java
I got an error when try to query database,the application can be booted successfully.
enter image description here
when I query the database through the API,the error occurred.
2021-02-03 13:15:46.693 INFO 3761 --- [nio-8088-exec-3] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-02-03 13:15:46.693 INFO 3761 --- [nio-8088-exec-3] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-02-03 13:15:46.694 INFO 3761 --- [nio-8088-exec-3] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession#c939991]
2021-02-03 13:15:46.759 INFO 3761 --- [nio-8088-exec-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-02-03 13:15:51.891 ERROR 3761 --- [nio-8088-exec-3] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
then i try to connect the local database via Navicat,succeed
enter image description here
here is my application.yml
server:
port: 8088
tomcat:
uri-encoding: UTF-8
max-http-header-size: 80KB
spring:
profiles:
active: dev
datasource: # 数据源的相关配置
type: com.zaxxer.hikari.HikariDataSource # 数据源类型:HikariCP
driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动
url: jdbc:mysql://localhost:3306/demo-shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=CST
username: root
password: 12345678
hikari:
connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 默认:30秒
minimum-idle: 10 # 最小连接数
maximum-pool-size: 10 # 最大连接数
auto-commit: true # 自动提交
idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
pool-name: DateSourceHikariCP # 连接池名字
max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟 1800000ms
connection-test-query: SELECT 1
servlet:
multipart:
max-file-size: 512000 # 文件上传大小限制为500kb
max-request-size: 512000 # 请求大小限制为500kb
mybatis:
mapper-locations: classpath:mapper/*.xml # mapper映射文件
type-aliases-package: com.mooc.pojo # 所有POJO类所在包路径
# mapper-locations: classpath:mapper/*.xml # mapper映射文件
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper:
mappers: com.mooc.my.mapper.MyMapper
not-empty: false # 在进行数据库操作的的时候,判断表达式 username != null, 是否追加 username != ''
identity: MYSQL
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
What happened?:(
problem solved
before
url: jdbc:mysql://localhost:3306/demo-shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=CST
username: root
password: 12345678
after
url: jdbc:mysql://localhost:3306/demo-shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CST
username: root
password: 12345678
I have few microservices as :
APIGateway: common gateway for all requestwith zuul proxy
ConfigService: like config server for properties files
RegistryService: service registry with eureka server
HomePageService: 1 service registered with eureka and config-service
ProductService: 1 service registered with eureka and config-service
When I ran in local like order:
RegistryService->ConfigService THEN all services APIGateway, HomePageService, ProductService, its working fine.
Now i created docker images by providing config and run in docker container and pushed to GCR.
I have created account for google cloud(free for 1 Yr) and able to see images in repo.
All fine BUT how can I deploy those images in GKE. I ran individually and deploying but no linking. What is the way to deploy those services?
kubectl run service-registry --image=gcr.io/salesstock/service-registry:v1 --port=7002
kubectl expose deployment service-registry --name=service-registry --type=LoadBalancer --port=7002 --target-port=7002
I tried something and shared some code snipet.
RegistryService properties:
spring:
profiles:
active: dev
application:
name: registry-service
server:
port: 7002
eureka:
instance:
hostname: localhost
port: 7002
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
#======docker======
---
spring:
profiles: docker
eureka:
instance:
hostname: 192.168.99.100
port: 7002
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
APIGateway properties:
spring:
profiles:
active: dev
application:
name: api-gateway
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-service
# uri: http://localhost:8888
server:
port: 7001
eureka:
instance:
hostname: localhost
port: 7002
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
#======docker======
---
spring:
profiles: docker
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-service
# uri: http://192.168.99.100:8888
eureka:
instance:
hostname: 192.168.99.100
port: 7002
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
ConfigService properties:
spring:
profiles:
active: dev
application:
name: config-service
cloud:
config:
server:
git:
uri: git url
search-paths: ConfigFiles
server:
port: 8888
management:
security:
enabled: false
eureka:
instance:
hostname: service-registry
port: 7002
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
#======docker======
---
spring:
profiles: docker
cloud:
config:
server:
git:
uri: git repo
search-paths: ConfigFiles
eureka:
instance:
hostname: 192.168.99.100
port: 7002
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
HomePageService properties:
spring:
profiles:
active: dev
application:
name: homepage-service
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-service
# uri: http://localhost:8888
server:
port: 7003
#for dynamic port
#server:
# port: 0
feign:
client:
config:
default:
connectTimeout: 160000000
readTimeout: 160000000
management:
security:
enabled: false
## endpoints:
## web:
## exposure:
## include: *
eureka:
instance:
hostname: localhost
port: 7002
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
#======docker======
---
spring:
profiles: docker
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-service
# uri: http://192.168.99.100:8888
eureka:
instance:
hostname: 192.168.99.100
port: 7002
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
for Docker image sample:
FROM openjdk:8
EXPOSE 7003
ADD /target/homepage-service.jar homepage-service.jar
ENTRYPOINT ["java","-Dspring.profiles.active=docker", "-jar", "homepage-service.jar"]
You deploy Docker containers by defining the container in the podspec of the k8s resource. You are already doing this with the kubectl run commands.
You then expose the pods using services, this is what you are doing with kubectl expose (the default value for this is ClusterIP, which is perfect for inter pod communication)
The reason this is not working for you is the way each container is trying to reach other containers. Localhost won't work. the IP you are using (192.168.x.x) likely isn't working either.
Instead, configure each container to target the FQDN of the corresponding service.
Example:
RegistryService->ConfigService
Expose the ConfigService using a ClusterIP service (if you use kubectl expose, the name will be ConfigService).
Configure the RegistryService to look for the ConfigService using "ConfigService.default.svc.cluster.local"