微服务项目sangomall代码配置修改及资源清单文件
1. 中间件的地址
1.1 Nacos
集群外
nacos-server.intra.com 192.168.31.211
集群内
nacos-server.sangomall.svc.cluster.local.
nacos-server.sangomall.svc.cluster.local.:8848
1.2 Redis
集群内
redis.sangomall.svc.cluster.local.
redis.sangomall.svc.cluster.local.:6379
1.3 Sentinel
集群外
sentinel.intra.com 192.168.31.211
集群内
sentinel-server.sangomall.svc.cluster.local.
sentinel-server.sangomall.svc.cluster.local.:8858
1.4 Zipkin
集群外
zipkin-server.intra.com 192.168.31.211
集群内
zipkin-server.sangomall.svc.cluster.local.
zipkin-server.sangomall.svc.cluster.local.:9411
1.5 RocketMQ
集群外 Dashboard
rocketmq-dashboard.intra.com 192.168.31.211
集群内
rocketmq-namesrv.sangomall.svc.cluster.local.
rocketmq-namesrv.sangomall.svc.cluster.local.:9876
1.6 Mysql
集群内
mysql-master.sangomall.svc.cluster.local.:3306
1.7 Elasticsearch
集群内
elasticsearch.sangomall.svc.cluster.local.
elasticsearch.sangomall.svc.cluster.local.:9200
对以上中间件内网域名进行解析,确认域名正确性
root@ks-master:~# dig -t a @10.233.0.3 nacos-server.sangomall.svc.cluster.local.
; <<>> DiG 9.16.1-Ubuntu <<>> -t a @10.233.0.3 nacos-server.sangomall.svc.cluster.local.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41358
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 7c4725e5d04b6f33 (echoed)
;; QUESTION SECTION:
;nacos-server.sangomall.svc.cluster.local. IN A
;; ANSWER SECTION:
nacos-server.sangomall.svc.cluster.local. 30 IN A 10.233.80.58
;; Query time: 4 msec
;; SERVER: 10.233.0.3#53(10.233.0.3)
;; WHEN: Thu Apr 13 11:15:46 CST 2023
;; MSG SIZE rcvd: 137
root@ks-master:~# dig -t a @10.233.0.3 redis.sangomall.svc.cluster.local.
; <<>> DiG 9.16.1-Ubuntu <<>> -t a @10.233.0.3 redis.sangomall.svc.cluster.local.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22327
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 8f7a62a8f6d2e04a (echoed)
;; QUESTION SECTION:
;redis.sangomall.svc.cluster.local. IN A
;; ANSWER SECTION:
redis.sangomall.svc.cluster.local. 30 IN A 10.233.83.50
;; Query time: 8 msec
;; SERVER: 10.233.0.3#53(10.233.0.3)
;; WHEN: Thu Apr 13 11:15:56 CST 2023
;; MSG SIZE rcvd: 123
root@ks-master:~# dig -t a @10.233.0.3 sentinel-server.sangomall.svc.cluster.local.
; <<>> DiG 9.16.1-Ubuntu <<>> -t a @10.233.0.3 sentinel-server.sangomall.svc.cluster.local.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3093
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 9ccf923e5889c9fd (echoed)
;; QUESTION SECTION:
;sentinel-server.sangomall.svc.cluster.local. IN A
;; ANSWER SECTION:
sentinel-server.sangomall.svc.cluster.local. 30 IN A 10.233.3.139
;; Query time: 4 msec
;; SERVER: 10.233.0.3#53(10.233.0.3)
;; WHEN: Thu Apr 13 11:16:07 CST 2023
;; MSG SIZE rcvd: 143
root@ks-master:~# dig -t a @10.233.0.3 zipkin-server.sangomall.svc.cluster.local.
; <<>> DiG 9.16.1-Ubuntu <<>> -t a @10.233.0.3 zipkin-server.sangomall.svc.cluster.local.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1576
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 22ae7a40ec88e5ce (echoed)
;; QUESTION SECTION:
;zipkin-server.sangomall.svc.cluster.local. IN A
;; ANSWER SECTION:
zipkin-server.sangomall.svc.cluster.local. 30 IN A 10.233.27.128
;; Query time: 8 msec
;; SERVER: 10.233.0.3#53(10.233.0.3)
;; WHEN: Thu Apr 13 11:16:15 CST 2023
;; MSG SIZE rcvd: 139
root@ks-master:~# dig -t a @10.233.0.3 rocketmq-namesrv.sangomall.svc.cluster.local.
; <<>> DiG 9.16.1-Ubuntu <<>> -t a @10.233.0.3 rocketmq-namesrv.sangomall.svc.cluster.local.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29412
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 823d809e1d2cddd1 (echoed)
;; QUESTION SECTION:
;rocketmq-namesrv.sangomall.svc.cluster.local. IN A
;; ANSWER SECTION:
rocketmq-namesrv.sangomall.svc.cluster.local. 30 IN A 10.233.83.92
;; Query time: 4 msec
;; SERVER: 10.233.0.3#53(10.233.0.3)
;; WHEN: Thu Apr 13 11:16:22 CST 2023
;; MSG SIZE rcvd: 145
root@ks-master:~# dig -t a @10.233.0.3 elasticsearch.sangomall.svc.cluster.local.
; <<>> DiG 9.16.1-Ubuntu <<>> -t a @10.233.0.3 elasticsearch.sangomall.svc.cluster.local.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11074
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 53aecc2b03256ad4 (echoed)
;; QUESTION SECTION:
;elasticsearch.sangomall.svc.cluster.local. IN A
;; ANSWER SECTION:
elasticsearch.sangomall.svc.cluster.local. 30 IN A 10.233.83.62
;; Query time: 4 msec
;; SERVER: 10.233.0.3#53(10.233.0.3)
;; WHEN: Thu Apr 13 11:16:30 CST 2023
;; MSG SIZE rcvd: 139
2. 修改项目配置文件
依次将微服务配置文件中的中间件地址由ip改为域名
application.yaml
# Nacos
spring:
cloud:
nacos:
discovery:
# 下面这行
server-addr: nacos-server.sangomall.svc.cluster.local.:8848
sentinel:
transport:
port: 8719
# 下面这行
dashboard: sentinel-server.sangomall.svc.cluster.local.:8858
client-ip: localhost
application:
name: mall-auth
jackson:
date-format: yyyy-MM-dd HH:mm:ss
thymeleaf:
cache: false
prefix: classpath:/templates
suffix: .html
redis:
# 下面这行
host: redis.sangomall.svc.cluster.local.
port: 6379
session:
store-type: redis
redis:
namespace: spring:session
server:
port: 30000
servlet:
session:
timeout: 30m # session 超时30分钟
management:
endpoints:
web:
exposure:
include: '*'
bootstrap.properties
spring.application.name=mall-auth
spring.cloud.nacos.config.server-addr=nacos-server.sangomall.svc.cluster.local.:8848
oss配置ak,se
mall-product/src/main/resources/application.yml
alicloud:
access-key: LTAI5tF8jvwYoP8gaeLBmuGh
secret-key: 7t73nSDEEb0uXDsKiGweYEz0mvj2kq
oss:
endpoint: oss-cn-beijing.aliyuncs.com
修改oss和sms的key及路径
mall-third-party/src/main/resources/application.yml
alicloud:
access-key: LTAI5tF8jvwYoP8gaeLBmuGh
secret-key: 7t73nSDEEb0uXDsKiGweYEz0mvj2kq
oss:
endpoint: oss-cn-beijing.aliyuncs.com
bucket: msb-laoshi-public
sms:
host: https://dfsns.market.alicloudapi.com
path: /data/send_sms
method: POST
appCode: 192a6811b5a4458f9402da3b0c3b9aa5
3. 编译打包
mvn_setting.xml
<settings>
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
</settings>
mvn clean install
排除了一些错误后,完成了. .jar文件生成
4. 编写Dockerfile
根据每个微服务不同的端口修改Dockerfile中的配置
FROM openjdk:8
ENV TZ Asia/Shanghai
EXPOSE 30000
VOLUME /tmp
ADD target/*.jar /app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
端口对应(查看src/main/application.yml或者properfiles)
auth
30000
cart
22200
commons
这个不需要
coupon
8010
gateway
8072
member
20300
order
8030
product
8040
search
8090
seckill
9601
third-party
8100
ware
8050
renren-fast
8093
renren-generator
80
5. 编写Deploy.yaml
在应用的根目录新建一个deploy目录,在目录中编写deploy.yaml文件
将该服务涉及到的资源配置都写入deploy.yaml文件中(deployment和service)
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mall-auth-server
namespace: sangomall
labels:
app: mall-auth-server
spec:
replicas: 1
selector:
matchLabels:
app: mall-auth-server
template:
metadata:
labels:
app: mall-auth-server
spec:
containers:
- name: mall-auth-server
image: $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest
imagePullPolicy: Always
ports:
- name: tcp-30000
containerPort: 30000
protocol: TCP
resources:
limits:
cpu: 1000m
memory: 500Mi
requests:
cpu: 10m
memory: 10Mi
---
kind: Service
apiVersion: v1
metadata:
name: mall-auth-server
namespace: sangomall
labels:
app: mall-auth-server
spec:
ports:
- name: http
protocol: TCP
port: 30000
targetPort: 30000
selector:
app: mall-auth-server
所有微服务重复此操作