SpringCloudAlibaba父类
在pom.xml
中添加spring-cloud-alibaba-dependencies
统一管理版本:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
微服务管理Nacos
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
增加注解application
通过 @EnableDiscoveryClient 注解表明是一个 Nacos 客户端,该注解是 Spring Cloud 提供的原生注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@RestController
public class EchoController {
@GetMapping(value = "/echo/{message}")
public String echo(@PathVariable String message) {
return "Hello Nacos Discovery " + message;
}
}
}
配置application.yml
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
注:server-addr为Nacos Server 网址
熔断处理Sentinel
Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
修改Application
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
@SpringBootApplication
@EnableCircuitBreaker
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
使用注解@SentinelResource
@SentinelResource(fallback = "fallbackEcho")
@GetMapping(value = "/echo/app/name")
public String echo() {
TbUser tbUser = new TbUser();
tbUser.setUsername("adfad");
TbUser serviceOne = userService.findOne(tbUser);
return "test";
}
public String fallbackEcho(){
return "error";
}
feign客户端使用
//feign客户端使用时,需要开启
feign:
sentinel:
enabled: true
Sentinel 监视器
Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
配置application.yml
spring:
cloud:
sentinel:
transport:
port: 8721 //sentinel监视端口
dashboard: 192.168.25.132:8080 //sentinel服务地址
服务配置中心Nacos Config
使用 Spring Cloud Alibaba Nacos Config,您可以在 Nacos Server 集中管理你 Spring Cloud 应用的外部属性配置。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
配置bootstrap.properties
# 多环境配置
spring.profiles.active=prod
# 这里的应用名对应 Nacos Config 中的 Data ID,实际应用名称以配置中心的配置为准
spring.application.name=nacos-provider-config
# 指定查找名为 nacos-provider-config.yaml 的配置文件
spring.cloud.nacos.config.file-extension=yaml
# Nacos Server 的地址
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# 关闭或开启动态刷新(默认是true)
spring.cloud.nacos.config.refresh.enabled=true
注意:Spring Boot 配置文件的加载顺序,依次为 bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml ,其中 bootstrap.properties 配置为最高优先级
消息队列RocketMQ
RocketMQ 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的、高可靠的消息发布与订阅服务。
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
生产者配置
配置Application
配置 Output(Source.class) 的 Binding 信息并配合 @EnableBinding 注解使其生效
import com.funtl.hello.spring.cloud.alibaba.rocketmq.provider.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
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 RocketMQProviderApplication{
@Autowired
private ProviderService providerService;
public static void main(String[] args) {
SpringApplication.run(RocketMQProviderApplication.class, args);
}
/**
* 实现了 CommandLineRunner 接口,只是为了 Spring Boot 启动时执行任务,不必特别在意
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
providerService.send("Hello RocketMQ");
}
}
配置application.yml
spring:
application:
name: rocketmq-producer
cloud:
stream:
rocketmq:
binder:
name-server: 192.168.25.139:9876
bindings:
# 这里是个 Map 类型参数,{} 为 YAML 中 Map 的行内写法
output: {destination: email-topic, content-type: application/json}
server:
port: 9093
运行成功后即可在 RocketMQ 控制台的 消息 列表中选择 test-topic 主题即可看到发送的消息
样例
@Autowired
private MessageChannel output;
public void sendMessage(String email){
output.send(MessageBuilder.withPayload(email).build());
}
消费者配置
配置Application
配置 Input(Sink.class) 的 Binding 信息并配合 @EnableBinding 注解使其生效
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 RocketMQConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RocketMQConsumerApplication.class, args);
}
}
配置application.yml
spring:
application:
name: rocketmq-consumer
cloud:
stream:
rocketmq:
binder:
name-server: 192.168.25.139:9876
bindings:
input:
consumer:
broadcasting: true # 设置消费模式为广播模式,不设置默认为队列模式
bindings:
input: {destination: email-topic, content-type: text/plain, group: email-group, consumer.concurrency: 1}
server:
port: 9094
样例
@StreamListener("input")
public void sendSimpleEmail(String email) {
System.out.println(email);
}
RPC框架DUBBO
RPC框架分为提供方和消费方,提供方提供服务,消费方消费服务。这里采用nacos注册中心和Dubbo框架配置。
提供方和消费方添加相同依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
提供方application.yml
配置
spring:
application:
name: provider
cloud:
nacos:
discovery:
server-addr: 192.168.25.138:8848
dubbo:
consumer:
default: false
registry:
address: nacos://192.168.25.138:8848
scan:
basePackages: com.demon.yunyoga.admin.email.service
#Dubbo 服务暴露的协议配置,其中子属性 name 为协议名称,port 为协议端口( -1 表示自增端口,从 20880 开始)
protocol:
port: -1
消费方application.yml
配置
spring:
application:
name: consumer
cloud:
nacos:
discovery:
server-addr: 192.168.25.138:8848
dubbo:
registry:
address: nacos://192.168.25.138:8848
consumer:
default: true
check: false
timeout: 40000
网友评论