需求:实现简单得用户登录和注册,注册发送短信服务。
开发工具:IDEA
使用到得技术:spring cloud+spring boot+mybatis-plus+mysql
注册中心:consul
网关:zuul
服务:zuul-service,users-service,message-service
1.新建项目
新建一个web项目,将其他文件删除只留下pom.xml和相应得.iml文件就OK了
2.新建项目中得module
zuul-service,users-service,message-service
新建module得时候,在Dependencies勾选上自己需要得依赖就好了
eg:message-service 在 Spring Cloud Routing中勾选OpenFeign
3.pom文件修改
父级:
使用<modules></modules>子项目加入
<modules>
<module>zuul-service</module>
<module>users-service</module>
<module>message-service</module>
</modules>
子项目:
将parent节点改成父级指向
<parent>
<groupId>com.zhangyunyan</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
将子项中所有得pom加上服务发现依赖:
<!--服务发现依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!--consul健康检查依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
zuul-service:网关必须得依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
要实现message-service和users-service接口相互调用使用Feign:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
mybatis-plus 使用框架技术依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
mysql 数据库依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
4.更改项目下得applicatiom.properity文件(习惯使用yml,改后缀 注意:缩进问题)
zuul-service:
①服务端口号 ②服务名称 ③加入注册中心(consul)④网关中心配置 ⑤设置服务发现超时时间
message-service:
①服务端口号 ②服务名称 ③加入注册中心(consul)④数据库链接 ⑤mybatis-plus配置
users-service:
①服务端口号 ②服务名称 ③加入注册中心(consul)④数据库链接 ⑤mybatis-plus配置
eg: users-service服务得application.yml配置如下:
server:
port: 8093
servlet:
context-path: /${spring.application.name}
spring:
application:
name: users-service
cloud:
client:
ip-address: 127.0.0.1
consul:
host: 127.0.0.1
port: 8500
discovery:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}
health-check-interval: 10s
health-check-path: ${server.servlet.context-path}/actuator/health
register: true
service-name: ${spring.application.name}
datasource:
url: jdbc:mysql://192.168.1.101:3311/demo?useUnicode=true&characterEncoding=utf8&useSSL=true&useAffectedRows=true
username: root
password: pass
driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
typeAliasesPackage: com.zhangyunyan.usersservice.model.entity
global-config:
id-type: 3
refresh: true
db-config:
db-type: mysql
debug: true
注意:注册中心配置加入时,service-name: 是设置application.yml文件所在得项目名称,而不是使用注册中心得项目名。
5.测试,使用网关ip和端口号,访问users-service服务/message-service服务
转载:https://blog.csdn.net/qq_39778344/article/details/101267643