微服务辅助技术
大约 12 分钟
SpringCloudStream 简介
1、
// https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-stream-rocketmq
implementation group: 'com.alibaba.cloud', name: 'spring-cloud-starter-stream-rocketmq', version: '2021.1'
2、
ext.versions = [ // 定义全部的依赖库版本号
scsRocketMQ : '2021.1', // SpringCloudStream依赖库版本
]
ext.libraries = [ // 依赖库引入配置
// 以下的配置为RocketMQ与SpringCloudStream整合服务所需要的依赖:
'spring-cloud-starter-stream-rocketmq': "com.alibaba.cloud:spring-cloud-starter-stream-rocketmq:${versions.scsRocketMQ}"
]
3、
project(":scs-common") {}
project(":scs-producer") {
dependencies {
implementation(libraries.'spring-cloud-starter-stream-rocketmq')
implementation(project(":scs-common"))
}
}
project(":scs-consumer") {
dependencies {
implementation(libraries.'spring-cloud-starter-stream-rocketmq')
implementation(project(":scs-common"))
}
}
4、
package com.yootk.common.dto;
import lombok.Data;
@Data
public class DeptDTO {
private Long deptno;
private String dname;
private String loc;
}
SCS 消息生产者
1、
spring:
cloud:
stream:
rocketmq:
binder:
name-server: rocketmq-server:9876 # NameServer地址
bindings:
output:
destination: TopicSSC # 消息主题
group: scs-producer-group # 生产组
access-key: RocketMQMuyan # 用户名
secret-key: helloyootk # 密码
2、
package com.yootk.service;
import com.yootk.common.dto.DeptDTO;
public interface IDeptMessageService { // 定义业务接口
public void sendMessage(DeptDTO dto); // 消息发送
}
3、
package com.yootk.service.impl;
import com.yootk.common.dto.DeptDTO;
import com.yootk.service.IDeptMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
@Service
public class DeptMessageServiceImpl implements IDeptMessageService {
@Autowired
private MessageChannel output; // 消息发送通道
@Override
public void sendMessage(DeptDTO dto) {
this.output.send(MessageBuilder.withPayload(dto).build()); // 进行消息的发送
}
}
4、
package com.yootk.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ProducerConfig { // 生产者的配置类
@Bean
public ObjectMapper getJacksonObjectMapper() {
return new ObjectMapper();
}
}
5、
package com.yootk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
@SpringBootApplication
@EnableBinding(Source.class)
public class StartRocketMQProducerApplication { // 启动消息生产者服务
public static void main(String[] args) {
SpringApplication.run(StartRocketMQProducerApplication.class, args);
}
}
6、
package com.yootk.test;
import com.yootk.StartRocketMQProducerApplication;
import com.yootk.common.dto.DeptDTO;
import com.yootk.service.IDeptMessageService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.concurrent.TimeUnit;
@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@SpringBootTest(classes = StartRocketMQProducerApplication.class)
public class TestDeptMessageService { // 进行业务的测试
@Autowired
private IDeptMessageService messageService;
@Test
public void testSend() throws Exception {
DeptDTO dto = new DeptDTO();
dto.setDeptno(10L);
dto.setDname("沐言科技教学部");
dto.setLoc("北京");
this.messageService.sendMessage(dto); // 消息发送
TimeUnit.SECONDS.sleep(20); // 稍微等一等
}
}
SCS 消息消费者
1、
spring:
cloud:
stream:
rocketmq:
binder:
name-server: rocketmq-server:9876 # NameServer地址
bindings:
input:
destination: TopicSSC # 消息主题
group: scs-producer-group # 生产组
access-key: RocketMQMuyan # 用户名
secret-key: helloyootk # 密码
2、
package com.yootk.service;
import com.yootk.common.dto.DeptDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
@Slf4j // 进行日志的输出
public class DeptMessageListener { // 部门消息监听
@StreamListener(Sink.INPUT) // 绑定SCS的消费注解
public void receive(Message<DeptDTO> message) { // 实现消息监听处理
log.info("接收到消息内容:{}", message.toString());
}
}
3、
package com.yootk.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConsumerConfig { // 生产者的配置类
@Bean
public ObjectMapper getJacksonObjectMapper() {
return new ObjectMapper();
}
}
4、
package com.yootk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;
@SpringBootApplication
@EnableBinding(Sink.class)
public class StartRocketMQConsumerApplication { // 启动消息生产者服务
public static void main(String[] args) {
SpringApplication.run(StartRocketMQConsumerApplication.class, args);
}
}
消费过滤
1、
package com.yootk.service.impl;
import com.yootk.common.dto.DeptDTO;
import com.yootk.service.IDeptMessageService;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class DeptMessageServiceImpl implements IDeptMessageService {
@Autowired
private MessageChannel output; // 消息发送通道
@Override
public void sendMessage(DeptDTO dto) {
{ // 发送第一条消息,该消息会附加有tag标签
Map<String,Object> headers = new HashMap<>(); // 定义头信息
headers.put("author", "李兴华"); // 附加信息
headers.put("李兴华编程训练营", "edu.yootk.com"); // 附加信息
headers.put(RocketMQHeaders.TAGS, "dept"); // 设置操作的标签
dto.setLoc("Tag消息"); // 修改一下消息的标记
Message message = MessageBuilder.createMessage(dto, new MessageHeaders(headers));
this.output.send(message); // 消息发送
}
{ // 发送第二条消息,该消息将传递消息识别码KEYS
Map<String,Object> headers = new HashMap<>(); // 定义头信息
headers.put("author", "李兴华"); // 附加信息
headers.put("李兴华编程训练营", "edu.yootk.com"); // 附加信息
headers.put(RocketMQHeaders.KEYS, "dept-key"); // 设置操作的标签
dto.setLoc("Keys消息"); // 修改一下消息的标记
Message message = MessageBuilder.createMessage(dto, new MessageHeaders(headers));
this.output.send(message); // 消息发送
}
{ // 发送第三条消息,该消息将传递消息判断处理(SQL92判断)
Map<String,Object> headers = new HashMap<>(); // 定义头信息
headers.put("author", "李兴华"); // 附加信息
headers.put("李兴华编程训练营", "edu.yootk.com"); // 附加信息
headers.put("level", "10"); // 设置操作的标签
dto.setLoc("SQL92消息"); // 修改一下消息的标记
Message message = MessageBuilder.createMessage(dto, new MessageHeaders(headers));
this.output.send(message); // 消息发送
}
}
}
2、
package com.yootk.channel;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;
public interface YootkSink { // 创建自定义Sink
String INPUT_TAG = "inputTag"; // 配置的Sink名称一定要与application.yml定义匹配
String INPUT_KEYS = "inputKeys"; // 配置的Sink名称一定要与application.yml定义匹配
String INPUT_SQL92 = "inputSQL92"; // 配置的Sink名称一定要与application.yml定义匹配
@Input(YootkSink.INPUT_TAG)
SubscribableChannel inputTag();
@Input(YootkSink.INPUT_KEYS)
SubscribableChannel inputKeys();
@Input(YootkSink.INPUT_SQL92)
SubscribableChannel inputSQL92();
}
3、
spring:
cloud:
stream:
rocketmq:
binder:
name-server: rocketmq-server:9876 # NameServer地址
bindings: # 通道的具体绑定配置
inputTag:
consumer:
tags: dept||emp
inputKeys:
consumer:
keys: dept-key
inputSQL92:
consumer:
sql: 'level BETWEEN 8 AND 12'
bindings:
inputTag: # 编写自定义通道
destination: TopicSSC # 消息主题
group: scs-producer-group-tag # 生产组
access-key: RocketMQMuyan # 用户名
secret-key: helloyootk # 密码
inputKeys: # 编写自定义通道
destination: TopicSSC # 消息主题
group: scs-producer-group-keys # 生产组
access-key: RocketMQMuyan # 用户名
secret-key: helloyootk # 密码
inputSQL92: # 编写自定义通道
destination: TopicSSC # 消息主题
group: scs-producer-group-sql92 # 生产组
access-key: RocketMQMuyan # 用户名
secret-key: helloyootk # 密码
4、
package com.yootk;
import com.yootk.channel.YootkSink;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;
@SpringBootApplication
@EnableBinding(YootkSink.class)
public class StartRocketMQConsumerApplication { // 启动消息生产者服务
public static void main(String[] args) {
SpringApplication.run(StartRocketMQConsumerApplication.class, args);
}
}
5、
package com.yootk.service;
import com.yootk.channel.YootkSink;
import com.yootk.common.dto.DeptDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
@Slf4j // 进行日志的输出
public class DeptMessageListener { // 部门消息监听
@StreamListener(YootkSink.INPUT_TAG) // 绑定SCS的消费注解
public void receive(Message<DeptDTO> message) { // 实现消息监听处理
log.info("【消息头信息】{}", message.getHeaders()); // 接收消息的主题
log.info("【消息内容】{}", message.getPayload()); // 接收消息的主题
}
}
6、
package com.yootk.service;
import com.yootk.channel.YootkSink;
import com.yootk.common.dto.DeptDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
@Slf4j // 进行日志的输出
public class DeptMessageListener { // 部门消息监听
@StreamListener(YootkSink.INPUT_TAG) // 绑定SCS的消费注解
public void receiveTag(Message<DeptDTO> message) { // 实现消息监听处理
log.info("【TAG消息头信息】{}", message.getHeaders()); // 接收消息的主题
log.info("【TAG消息内容】{}", message.getPayload()); // 接收消息的主题
}
@StreamListener(YootkSink.INPUT_KEYS) // 绑定SCS的消费注解
public void receiveKeys(Message<DeptDTO> message) { // 实现消息监听处理
log.info("【KEYS消息头信息】{}", message.getHeaders()); // 接收消息的主题
log.info("【KEYS消息内容】{}", message.getPayload()); // 接收消息的主题
}
@StreamListener(YootkSink.INPUT_SQL92) // 绑定SCS的消费注解
public void receiveSQL92(Message<DeptDTO> message) { // 实现消息监听处理
log.info("【SQL92消息头信息】{}", message.getHeaders()); // 接收消息的主题
log.info("【SQL92消息内容】{}", message.getPayload()); // 接收消息的主题
}
}
SpringCloudConfig 简介
1、
https://docs.spring.io/spring-cloud-config/docs/3.0.4/reference/html/
2、
ssh-keygen -m PEM
3、
git init
4、
message.provider-dev.yml
yootk:
message:
flag: dev
content: 沐言科技:www.yootk.com
message.provider-test.yml
yootk:
message:
flag: test
content: 李兴华高薪就业编程训练营:edu.yootk.com
message.provider-prod.yml
yootk:
message:
flag: prod
content: 课程资源下载:www.yootk.com/resources
5、
git add .
6、
git commit -m "Create Message Config Files"
7、
git remote add origin git@gitlab-server:muyan/microcloud.git
8、
git push -u origin master
SpringCloudConfig 服务端
1、
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server
implementation group: 'org.springframework.cloud', name: 'spring-cloud-config-server', version: '3.0.5'
2、
project(":config-server-7501") { // 部门微服务
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.cloud:spring-cloud-config-server")
implementation(libraries.'spring-boot-admin-starter-client')
// 以下的依赖库为Nacos注册中心所需要的依赖配置
implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery') {
exclude group: 'com.alibaba.nacos', module: 'nacos-client' // 移除旧版本的Nacos依赖
}
implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config') {
exclude group: 'com.alibaba.nacos', module: 'nacos-client' // 移除旧版本的Nacos依赖
}
implementation(libraries.'nacos-client') // 引入与当前的Nacos匹配的依赖库
}
}
3、
git@gitlab-server:muyan/microcloud.git
4、
server: # 服务端配置
port: 7501 # 7501端口
spring:
application: # 配置应用信息
name: config.server # 是微服务的名称
cloud: # Cloud配置
nacos: # Nacos注册中心配置
discovery: # 发现服务
weight: 80
service: ${spring.application.name} # 使用微服务的名称作为注册的服务名称
server-addr: nacos-server:8848 # Nacos服务地址
namespace: 96c23d77-8d08-4648-b750-1217845607ee # 命名空间ID
group: MICROCLOUD_GROUP # 一般建议大写
cluster-name: MuyanCluster # 配置集群名称
username: muyan # 用户名
password: yootk # 密码
metadata: # 根据自身的需要配置元数据
version: 1.0 # 自定义元数据项
company: 沐言科技 # 自定义元数据项
url: www.yootk.com # 自定义元数据项
author: 李兴华(爆可爱的小李老师) # 自定义元数据项
config: # 定义SpringCloudConfig的配置服务器
server: # 服务器的信息配置
git: # 此时的存储类型为git
uri: git@gitlab-server:muyan/microcloud.git # GITLab仓库地址
default-label: master # 定义GIT分支名称
search-paths: # 定义仓库的配置匹配
- provider-* # 以provider-开头的所有的配置
private-key: # 私钥(.ssh目录下的id_rsa)
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuJDjXdP9IOwHmKNxQqPZVDBh/ybWLf5s0q28JbPg1aVXch4h
TUPt7heC4GgEVmZa2zlHu+iyu2tcyn2NjhpxwRXMkj8hMQ68Rtu0SWkJBDbuTd+W
b9PzRT5OnurVVDPGte58s4yWMT2lLmmWALDj9wjNe2Za+DA+5kRjpT3NmlFtmoJF
t7B/LraESRfDqB4/37Mql9Q2gOJ+nEkrqqw7A3PTF/wBAb34jTe83qn3c0pCPWji
+KP8s4g5M+aUdk8XtzHEA0DLMPtK3l3flipe71AtkRHNQeMzEzeXcqUHA7swdJRj
CSG1KVTI7sjlfg5itzO4LK1fnu6bdLHJ4RgN0QIDAQABAoIBACujHg7GG1gYVidx
pUqzQKQWGGViQkt3lzUsnIpxgBwjToyNsSP6hpu/dVMnZLck1iB054/3EaMcXEEH
9Fr/x5t2UvrU8D0QFdXPBk5BBj+32RoSXhgsjZz2gVIzhaK15yqGq3csKyqTP6W8
kxmwuQVkUGj3p1R827JHwyODfEQay7lP+4iXIozO5NDv4JGOx6ko9eKuKRFYRZ9A
zB+BpWpyMTqrh80DtYO9R8AjAEOem+De62ZHX4YBB7RbyFMY7ZIrnlfGkr4KcfO0
pOq7mf733ujfYXcpXyjWnCw0CL515nAFVQwxX5ZMFdG46RzlgV9YlV1dIOy2IC73
NUxPXL0CgYEA3O43PKBEBi0WPRQ2JQnEf8s/BxcZF7D1C3GsgcP0qQC+4vg1sR1Q
VYEbAI0ZM8gd0HjQp8cQgSp1RoFm3oNLVGj1LtXVrKn1Li2Ft0QjLdU0PeDEXZY8
VJsXxFLMIFzl4PCQTMMOUmsepJeZTxQLfOP8zerpxkjmKtxlWxOyF0cCgYEA1dz0
IpuRXN8KEj3XTUa3CxrWUzCRbKLvGkF8vO3O/8LXX8MLLqictt2blEv4BE6OcQQ9
GCo2FwuzNgQKbh2vedVDbj8QkrZ0MdX1Tr48Ljv/MOL4EHLJpDHQE1A3+5v3zyD1
BK+shtkMOZ+WXsofebYJ0PaiAY5akdnh3XRabicCgYEAl54j6kdSZ6qpiGFGh0Sc
xdcF+iev4MJgaOoqKSQB07nfRoRw1Iv5hbSEg2xRz2eWjmtbRskmWn543VoQ7sPL
/rHJEctOSKCa0oYLLipkuh+HKrAGEvP3k1ichu8Lh2o5mJV/ci6qtYUuEma3n71x
5NPRur9sj9eXnfKC3G432iECgYAia7GPcGJxBCTm0rU4Zs1HKEaelQ6S4ZaCL1AY
SF4so/tuGkJOfCzc8cX8lE2Sw+IBJqF+piyWgPeTFgtntbdscHqt39CXDG6fJsSY
q92bw2RLRfyPCSqTFsFammqoQG4eFDvbSTsHaR+ilqDpGLcAK+BglZF1lqfVGbBU
qEl1uQKBgQCPBiphh5yLGTmJBrcFtK1fX1Y8r4ibgyWKyXdlK2XXvKRdGrZM7Asm
3ftcUu+kruC82cqzpyV5aD77xfGJb1JzazbX7fgq4DlOb1JTYDVMkZ7pIw6a2oiN
3SI9no+9bfhtu/Lqk2pBYa9YPScPaMEmxr/zwcM54nr49cy3/l5EXQ==
-----END RSA PRIVATE KEY-----
5、
package com.yootk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class StartConfigServerApplication7501 {
public static void main(String[] args) {
SpringApplication.run(StartConfigServerApplication7501.class, args);
}
}
6、
127.0.0.1 config-server-7501
7、
config-server-7501:7501/master/message.provider-dev.yml
config-server-7501:7501/master/message.provider-test.yml
config-server-7501:7501/master/message.provider-prod.yml
8、
https://docs.spring.io/spring-cloud-config/docs/3.0.4/reference/html/#_spring_cloud_config_server
SpringCloudConfig 客户端
1、
project(":provider-message-8201") { // 消息微服务
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.cloud:spring-cloud-starter-config")
implementation(libraries.'spring-boot-admin-starter-client')
// 以下的依赖库为Nacos注册中心所需要的依赖配置
implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery') {
exclude group: 'com.alibaba.nacos', module: 'nacos-client' // 移除旧版本的Nacos依赖
}
implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config') {
exclude group: 'com.alibaba.nacos', module: 'nacos-client' // 移除旧版本的Nacos依赖
}
implementation(libraries.'nacos-client') // 引入与当前的Nacos匹配的依赖库
}
}
2、
server: # 服务端配置
port: 8201 # 8201端口
spring:
application: # 配置应用信息
name: message.provider # 是微服务的名称
cloud: # Cloud配置
nacos: # Nacos注册中心配置
discovery: # 发现服务
weight: 80
service: ${spring.application.name} # 使用微服务的名称作为注册的服务名称
server-addr: nacos-server:8848 # Nacos服务地址
namespace: 96c23d77-8d08-4648-b750-1217845607ee # 命名空间ID
group: MICROCLOUD_GROUP # 一般建议大写
cluster-name: MuyanCluster # 配置集群名称
username: muyan # 用户名
password: yootk # 密码
metadata: # 根据自身的需要配置元数据
version: 1.0 # 自定义元数据项
company: 沐言科技 # 自定义元数据项
url: www.yootk.com # 自定义元数据项
author: 李兴华(爆可爱的小李老师) # 自定义元数据项
3、
spring: # Spring配置项
cloud: # SpringCloud配置项
nacos: # Nacos注册中心的配置
config: # gRPC通讯配置
server-addr: nacos-server:8848 # Nacos地址
namespace: 96c23d77-8d08-4648-b750-1217845607ee # 命名空间ID
group: MICROCLOUD_GROUP # 一般建议大写
cluster-name: MuyanCluster # 配置集群名称
username: muyan # 用户名
password: yootk # 密码
config:
name: ${spring.application.name} # 服务的名称为加载的文件名称
profile: dev # profile文件名称
label: master # 分支名称
discovery:
service-id: config.server # 通过Nacos访问
enabled: true # 启用发现服务
application:
name: message.provider # 应用名称
4、
package com.yootk.provider.action;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/provider/message/*")
public class MessageAction {
@Value("${yootk.message.flag}") // 配置文件的加载KEY
private String flag;
@Value("${yootk.message.content}") // 配置文件的加载KEY
private String content;
@RequestMapping("config")
public Object config() {
Map<String, Object> result = new HashMap<>();
result.put("flag", this.flag);
result.put("content", this.content);
return result;
}
}
5、
package com.yootk.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class StartMessageApplication {
public static void main(String[] args) {
SpringApplication.run(StartMessageApplication.class, args);
}
}
6、
127.0.0.1 provider-message-8201
7、
spring: # Spring配置项
cloud: # SpringCloud配置项
nacos: # Nacos注册中心的配置
config: # gRPC通讯配置
server-addr: nacos-server:8848 # Nacos地址
namespace: 96c23d77-8d08-4648-b750-1217845607ee # 命名空间ID
group: MICROCLOUD_GROUP # 一般建议大写
cluster-name: MuyanCluster # 配置集群名称
username: muyan # 用户名
password: yootk # 密码
discovery: # 发现服务
weight: 80
service: ${spring.application.name} # 使用微服务的名称作为注册的服务名称
server-addr: nacos-server:8848 # Nacos服务地址
namespace: 96c23d77-8d08-4648-b750-1217845607ee # 命名空间ID
group: MICROCLOUD_GROUP # 一般建议大写
cluster-name: MuyanCluster # 配置集群名称
username: muyan # 用户名
password: yootk # 密码
metadata: # 根据自身的需要配置元数据
version: 1.0 # 自定义元数据项
company: 沐言科技 # 自定义元数据项
url: www.yootk.com # 自定义元数据项
author: 李兴华(爆可爱的小李老师) # 自定义元数据项
config:
name: ${spring.application.name} # 服务的名称为加载的文件名称
profile: dev # profile文件名称
label: master # 分支名称
discovery:
enabled: true # 启用发现服务
service-id: config.server # 通过Nacos访问
application:
name: message.provider # 应用名称
8、
provider-message-8201:8201/provider/message/config
SpringCloudBus
1、
https://docs.spring.io/spring-cloud-config/docs/3.0.4/reference/html/#_push_notifications_and_spring_cloud_bus
2、
// https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-bus-rocketmq
implementation group: 'com.alibaba.cloud', name: 'spring-cloud-starter-bus-rocketmq', version: '2021.1'
3、
project(":config-server-7501") { // 部门微服务
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.cloud:spring-cloud-config-server")
implementation('com.alibaba.cloud:spring-cloud-starter-bus-rocketmq')
implementation(libraries.'spring-boot-admin-starter-client')
// 以下的依赖库为Nacos注册中心所需要的依赖配置
implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery') {
exclude group: 'com.alibaba.nacos', module: 'nacos-client' // 移除旧版本的Nacos依赖
}
implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config') {
exclude group: 'com.alibaba.nacos', module: 'nacos-client' // 移除旧版本的Nacos依赖
}
implementation(libraries.'nacos-client') // 引入与当前的Nacos匹配的依赖库
}
}
project(":provider-message-8201") { // 消息微服务
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.cloud:spring-cloud-starter-config")
implementation('com.alibaba.cloud:spring-cloud-starter-bus-rocketmq')
implementation(libraries.'spring-boot-admin-starter-client')
// 以下的依赖库为Nacos注册中心所需要的依赖配置
implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery') {
exclude group: 'com.alibaba.nacos', module: 'nacos-client' // 移除旧版本的Nacos依赖
}
implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config') {
exclude group: 'com.alibaba.nacos', module: 'nacos-client' // 移除旧版本的Nacos依赖
}
implementation(libraries.'nacos-client') // 引入与当前的Nacos匹配的依赖库
}
}
4、
spring:
application: # 配置应用信息
name: config.server # 是微服务的名称
cloud: # Cloud配置
stream:
rocketmq:
binder:
name-server: rocketmq-server:9876
access-key: RocketMQMuyan
secret-key: helloyootk
5、
spring:
application: # 配置应用信息
name: message.provider # 是微服务的名称
cloud: # Cloud配置
stream:
rocketmq:
binder:
name-server: rocketmq-server:9876
access-key: RocketMQMuyan
secret-key: helloyootk
6、
package com.yootk.provider.vo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Data
@Component // 必须添加为Bean
@RefreshScope // 动态加载
public class MessageConfig {
@Value("${yootk.message.flag}") // 配置文件的加载KEY
private String flag;
@Value("${yootk.message.content}") // 配置文件的加载KEY
private String content;
}
7、
package com.yootk.provider.action;
import com.yootk.provider.vo.MessageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/provider/message/*")
public class MessageAction {
@Autowired
private MessageConfig messageConfig;
@RequestMapping("config")
public Object config() {
Map<String, Object> result = new HashMap<>();
result.put("flag", this.messageConfig.getFlag());
result.put("content", this.messageConfig.getContent());
return result;
}
}
8、
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
9、
yootk:
message:
flag: prod
content: 小李老师公益课程资源下载(隶属于沐言科技):www.yootk.com/resources
10、
git add .
git commit -m "Modify message.provider-prod.yml File"
11、
git push -u origin master
12、
config-server-7501:7501/actuator/bus-refresh
demo