Micro-service problems encountered in building

Foreword

Now xx company I want to reconstruct the user's system. The old system is a single user application. Downstream of each system to achieve the user query, log in, check, manage menu by calling the user's system. In order to adapt to the new enterprise architecture model, with a micro-services architecture to reconstruct the old user's system.

In fact, from a single application to build a micro-service architecture, exercise is a reconstruction of the old single application project idea: a single application == "micro-service architecture applications.

Process is as follows:

1, from the familiar to the original project: requirements documents, manuals, database design document, external interface, page prototype (the practical operation of the system), call the principle of external interfaces. (Here because I am not very familiar with the user system, you need to be familiar with all aspects of the various functions of the user's system, if it is to reconstruct the reserve system, it is much easier, because I reserve the function of the system, the table structure design are very familiar (table I are designed), and the reserve system does not need to call other services to micro-packaging functions of the system)

2, the preparation of the development progress of the document;

3, the preparation of new users to build micro-services system development and design documentation;

4, see the Corporation micro-services architecture, front and back end development specifications, where the company's infrastructure to build documents, specifications;

4, familiar with the micro-service architecture, the various components.

5, to build a micro-service.

Build a lot of problems encountered in the process, the following several major record description. (Because I only had 17 contacts in a SOA architecture projects, and later have been doing OA, with the SSM, there is no contact with the micro-services, for SpringBoot, SpingCloud not very familiar with, everything from scratch, Go !!! ).

First, the user system micro-services architecture build problems encountered in the process

1, the development environment

Development Tools: idea

jdk version: jdk8

jar package management: mavn warehouse

Micro Services Architecture: Springboot + SpringCloud

2, the following components

 

 

 

3, build a micro-service process:

Ado, we now only a local development environment (idea, jdk, maven repository), nothing else. Our project is the separation of the front and rear end, following first set up back-end architecture.

Local also a lot of software is not installed, install a variety of Baidu ,,, ,,, configuration error ,, ,,. Take over local projects should all understand.

3.1, the first micro-service development to build back-end architecture:

1, the company offers a demo micro architecture department services framework, download the demo, import,

2, the JDK configuration;

3、配置maven仓库;pom.xml文件报错,不要慌,配置下JDKmaven-Reimport,导入SpringBoot与SpringCloud及相关依赖。

4、所有配置都OK了,启动项目。

5、很完美,开始进入报错、修改、重启、再报错阶段。

6、下面是我的配置文件

application-dev.properties

#配置服务端口
server.port= 31010

# 注册中心配置
#开发,本地开发时不需要注册
eureka.client.service-url.defaultZone=http://localhost:21000/eureka/
#获取主机ip作为地址注册到注册中心
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
[email protected]@

eureka.client.register-with-eureka=false
eureka.client.fetchRegistry=false
eureka.client.server.waitTimeInMsWhenSyncEmpty=0

# 获取注册信息间隔时间 default 30
#eureka.client.registry-fetch-interval-seconds=30
# 更新实例变化到服务端 default 30
#eureka.client.instance-info-replication-interval-seconds=30
# 动态刷新eureka的serviceURl地址的间隔时间 与Config集成需要 default 300
#eureka.client.eureka-service-url-poll-interval-seconds=300

#开启健康检查(需要spring-boot-starter-actuator依赖)
#eureka.client.healthcheck.enabled = true
#租期更新时间间隔(默认30秒)
#eureka.instance.lease-renewal-interval-in-seconds =30
#租期到期时间(默认90秒)
#eureka.instance.lease-expiration-duration-in-seconds =90

#数据库配置
spring.datasource.url= jdbc:postgresql://127.0.0.1:5432/imsdb
spring.datasource.username= postgres
spring.datasource.password= root
spring.datasource.driver-class-name= org.postgresql.Driver

mybatis.type-aliases-package= cn.com.piccre.template.entity
mybatis.mapper-locations=classpath:/mapper/*Mapper.xml
mybatis.configuration.map-underscore-to-camel-case=true

#日志扫描
logging.level.=info
logging.level.cn.com.piccre.template.dao=debug
logging.config=classpath:logback-dev.xml

#apollo配置中心
apollo.bootstrap.enabled = true
#### apollo server地址
apollo.meta=http://10.129.202.74:8080
### apollo server的应用id
app.id=ServiceTemplate

#redis配置
spring.redis.host=10.129.202.73
spring.redis.port=6379
spring.redis.password=picc123456
spring.redis.timeout=2000

#调用公司用户中心的接口地址
#7.3用户登录检查(按用户名和密码形式)—非集成单点(checkLoginByPwd)接口地址
check_user_url=http://10.129.202.72:4789/UserTransferServer/userService/check
#3.3根据用户获取菜单(getMenuByUser)
get_menu_url=http://10.129.202.72:4789/UserTransferServer/userService/menu
#10.4根据用户代码获取用户详细信息(getUserMsg)
get_user_info=http://10.129.202.72:4789/UserTransferServer/userService/userInfo

#密钥文件用于加密token,将demo附带的密钥文件放于本地目录,只在登录时生成token用到
keyPublicPath=F:/cjp/key.pub
keyPrivatePath=F:/cjp/key.pri

bootstrap.properties文件:用以本地测试时切换配置文件application.properties

spring.cloud.config.enabled=false
spring.cloud.config.fail-fast=false
#配置项目的id,注册到eureka时显示的名字,保证注册到eureka时是不重复的
spring.application.name=demo-client
#以下为设置启用哪个环境的配置文件,应保证只开启一个
#开发,即application-dev.properties生效
spring.profiles.active=dev
#configType=dev
#测试,即application-test.properties生效
#spring.profiles.active=test
#configType=test
#生产,即application-prod.properties生效
#spring.profiles.active=prod
#configType=prod

3.1.1 报错一  发布服务失败(没有搭建注册中心)

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187)
    at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123)
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27)
    at com.sun.jersey.api.client.Client.handle(Client.java:652)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)    

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111)
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137)
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77)
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
    at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1013)    
    
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187)
    at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123)
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27)
    at com.sun.jersey.api.client.Client.handle(Client.java:652)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)

原因如下:本地导入项目后,并没有对配置文件做任何改动,看配置文件,发现本地根本没有配置服务中心,故不停报上述异常。

解决:关闭eureka注册中心。配置里添加标黄的配置(不向注册中心发布服务,问题解决)

3.1.2 报错二  连接数据库失败

org.postgresql.util.PSQLException: Connection to 127.0.0.1:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
    at org.postgresql.Driver.makeConnection(Driver.java:458)
    at org.postgresql.Driver.connect(Driver.java:260)

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection to 127.0.0.1:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:342)

查询是测试环境数据库挂了,好解决,本地安装一个PostgreSQL。(出现JDBC相关的,肯定跟数据库的配置有关)

重启服务,报下述错误。

安装PostgreSQL见链接:

https://www.runoob.com/postgresql/windows-install-postgresql.html

https://blog.csdn.net/qq_36330643/article/details/72763155

https://www.postgresql.org/ftp/source/v10.10/

 

3.1.2 报错三  连接数据库失败

 

 

2020-01-22 09:59:20.904 INFO  [service-template] 11176 [RMI TCP Connection(1)-10.129.206.99] com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
2020-01-22 09:59:21.901 INFO  [service-template] 11176 [RMI TCP Connection(1)-10.129.206.99] com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Driver does not support get/set network timeout for connections. (这个 org.postgresql.jdbc.PgConnection.getNetworkTimeout() 方法尚未被实作。)
2020-01-22 09:59:21.919 INFO  [service-template] 11176 [RMI TCP Connection(1)-10.129.206.99] com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.

前台提示:

exception":"org.springframework.transaction.CannotCreateTransactionException","message":"Request processing failed; 
nested exception is org.springframework.transaction.CannotCreateTransactionException:
Could not open JDBC Connection for transaction; nested exception is java.sql.SQLTransientConnectionException:
HikariPool-1 - Connection is not available, request timed out after 30000ms.","path":"/demo/user/login"}

 原因如下:因为我安装的PostgreSQL是最新版本,跟demo的依赖版本不匹配。从maven官网重新下载了标黄的依赖,问题解决。

<dependency>
     <groupId>org.postgresql</groupId>
     <artifactId>postgresql</artifactId>
     <version>9.4.1212.jre7</version>
</dependency>
<!--<dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.2.9.jre7</version>
</dependency>-->

3.1.4 报错三  连接数据库失败

The server doesn't grant access to the database: the server reports 
FATAL: no pg_hba.conf entry for host "192.168.0.123", user "postgres", database "postgres" FATAL: no pg_hba.conf entry for host "192.168.0.123", user "postgres", database "postgres"

PostgreSQL数据库为了安全,它不会监听除本地以外的所有连接请求,当用户通过JDBC访问是,会报一些如下的异常:

org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host

要解决这个问题,只需要在PostgreSQL数据库的安装目录下找到/data/pg_hba.conf,找到“# IPv4 local connections:”

在其下加上请求连接的机器IP

host all all 127.0.0.1/32 md5

32是子网掩码的网段;md5是密码验证方法,可以改为trust

如下图:红色部分都是我修改的。

 

 

 解决完上述问题,项目终于成功启动,不再报错。

在没有搭建前端开发框架的时候,可以通过postman等工具进行测试。

get方式访问:

3.2、搭建前端的微服务开发架构:

前端用的是Vue框架。

3.2.1 搭建开发环境

搭建前端框架很顺利,公司提供了项目Demo。分为下面几步:

  1. 安装软件:node.js。
      1. 下载安装包,傻瓜式安装即可。
  2. 安装开发工具Sublime。
    1. 下载安装包,https://www.sublimetext.com/3官网下载,傻瓜式安装。
  3. 导入项目
    1. 解压webProject到自己工作目录,例如:D:\workspace\webProject打开Sublime->file->open folder找到刚才解压的webProject目录,选择导入完成。

  4. 下载依赖
    1. 打开cmd命令行,cd到webProject目录下

    2. 输入命令:yarn 回车,等待下载完成。

  5. 本地启动
    1. 打开cmd命令行,cd到webProject目录下

    2. 输入命令:yarn serve

    3. 回车,等待启动完成。

    4. 完成后可以访问默认地址:http://localhost:8080

  6. 打包发布
    1. 打开cmd命令行,cd到webProject目录下

    2. 输入命令:npm run build

    3. 回车,等待打包完成,会在webProject目录下生成一个dist文件夹这个就是打包好的文件,将整个dist上传到nginx服务器配置好的目录下就可以了,由于前端打包好的文件是静态的所以不需要重启即可生效。

通过安装软件,直接启动项目即可。

3.2.2 遇到的问题

1、在项目导入,启动时,用到了命令yarn ,命令行执行,报错,,,没有安装yarn。。需要自己下载安装。

参看链接:https://yarnpkg.com/zh-Hans/docs/install#windows-stable

2、打包时用到了命令npm,需要学习了解。 

3、访问http://localhost:8080报错。解决:需要在vue.config.js中配置下后端应用的地址

3.2.3 遇到的问题

上面提到的这些软件工具一个没用过,下面进入学习阶段。

1、node.js是什么?npm?

2、开发Vue的工具?

3、什么是yarn?

参看链接:https://www.cnblogs.com/vole/p/12228658.html

二、本地如何搭建服务中心与网关

1、

三、组件

1、日志监控

ELK+skywalking

2、服务跟踪

3、apollo

四、本地搭建本地虚拟机与Linux

centos

Guess you like

Origin www.cnblogs.com/vole/p/12222661.html