- 服务注册和服务发现:Nacos
- 服务调用:Feign
- 统一网关:Gateway
- 消息中间件:RabbitMQ
- 数据库:MySQL
- 中间件及部署:Docker
- SpringBoot+MyBatis再次不多赘述
- feign-api:feign抽取后的模块,进行解耦合
- feign-service:服务消费者
- gateway:统一网关
- user-service:服务生产者
父工程指定了SpringCloud,SpringCloudAlibaba等依赖版本,子工程依赖各自导入
Nacos:org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import org.springframework.boot spring-boot-dependencies${spring-boot.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies${spring-cloud-alibaba.version} pom import
由于在父工程指定了版本,子模块不再指定版本,需要注册的服务都需要进行这一步
-
引入依赖
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery -
编写配置文件
# 端口配置 server: port: 8081 # 数据库配置 spring: # mysql数据库配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=true&serverTimezone=UTC username: root password: ssx14233 application: name: userservice # 服务名称 cloud: nacos: server-addr: localhost:8848 # Nacos服务地址
-
编写配置类,添加注解
@EnableDiscoveryClient @Configuration public class NacosDiscoveryConfiguration { }
服务间的调用使用Feign
-
引入依赖
org.springframework.cloud spring-cloud-starter-openfeign -
在启动类添加自动装配注解
@EnableFeignClients @SpringBootApplication public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); } }
-
写服务提供者,注意使用Restful风格
@RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/getUser/{id}") public User getUserById(@PathVariable("id") Long id) { return new User(id, "Feign", "Use Feign"); } }
-
编写client接口
@GetMapping("/user/getUser/{id}") User getUser(@PathVariable("id") Long id);
-
注意,这里使用的是feign-api,对feign进行了抽取,解耦合
-
引入依赖
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoveryorg.springframework.cloud spring-cloud-starter-gateway -
编写配置文件
# 端口配置 server: port: 10001 # 数据库配置 spring: # mysql数据库配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=true&serverTimezone=UTC username: root password: ssx14233 application: name: gateway # 服务名称 cloud: nacos: server-addr: localhost:8848 # Nacos服务地址 gateway: routes: # 网关路由配置 - id: user-service # 路由表示,必须唯一 uri: lb://userservice # 路由的目标地址,lb是负载均衡 predicates: # 路由断言,判断请求是否符合规则 - Path=/user/** - id: feign-service uri: lb://feignservice predicates: - Path=/feign/**
-
因为统一网关也需要在Nacos注册,编写配置类
@EnableDiscoveryClient @Configuration public class NacosDiscoveryConfiguration { }
引入依赖
org.springframework.boot spring-boot-starter-amqpcom.fasterxml.jackson.core jackson-databind
编写配置文件,此处联合上面的Nacos
spring: # mysql数据库配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=true&serverTimezone=UTC username: root password: ssx14233 application: name: userservice # 服务名称 cloud: nacos: server-addr: localhost:8848 # Nacos服务地址 rabbitmq: # rabbitmq配置 host: 127.0.0.1 # ip地址 port: 5672 # 端口 virtual-host: / # 虚拟主机 username: guest password: guest消息发送
- 消息提供者使用了controller调用service发送消息,请求controller即可发送消息
controller代码@GetMapping("/sendMessageToRabbitmq/{id}") public void sendMessageToRabbitmq(@PathVariable Long id) { userService.sendMessageToRabbitmq(new User(id, "RabbitMQ", "Use RabbitMQ")); }
service代码@Service public class UserServiceImpl implements UserService { @Autowired private RabbitTemplate rabbitTemplate; @Override public void sendMessageToRabbitmq(User user) { // 交换机名称 String exchangeName = "user.topic"; // 发送消息 rabbitTemplate.convertAndSend(exchangeName, "user.insert", user); } }
-
声明队列在消费者这方,当接收到了消息就执行service中的代码,通过@RabbitListener声明队列
@Component public class UserListener { @Autowired private UserService userService; @RabbitListener(bindings = @QueueBinding( value = @Queue(name = "user.insert.queue"), exchange = @Exchange(name = "user.topic", type = ExchangeTypes.TOPIC), key = "#.insert" )) public void listenSaveUser(@RequestBody User user) { userService.saveUser(user); } }
service代码
@Override public void saveUser(User user) { System.out.println("通过RabbitMQ调用,插入对象n" + user); }
此处以作者的CentOS7.6为例
-
安装依赖
yum -y install gcc
yum -y install gcc-c++
-
安装
yum install docker
-
启动docker服务
service docker start
- 查询RabbitMQ镜像
docker search rabbitmq:management
- 拉取镜像
docker pull rabbitmq:3-management
- 创建并启动rabbitmq容器
docker run -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest --name mq -p 15672:15672 -p 5672:5672 -d rabbitmq:3-management
如果是阿里云服务器,可以在阿里云控制台进行端口开放
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)