├── [Help ├── target └── sonar │ ├── .sonar_lock │ └── report-task.txt ├── baseToken.txt ├── README.md ├── spring-cloud-alibaba-api ├── src │ └── main │ │ └── java │ │ └── com │ │ └── hzihui │ │ └── cloud │ │ └── alibaba │ │ ├── service │ │ ├── UserService.java │ │ └── ProductService.java │ │ ├── vo │ │ └── UserVO.java │ │ └── entity │ │ ├── User.java │ │ └── Product.java └── pom.xml ├── spring-cloud-alibaba-consumer ├── target │ └── classes │ │ ├── bootstrap.yml │ │ └── application.yml ├── src │ └── main │ │ ├── resources │ │ ├── bootstrap.yml │ │ └── application.yml │ │ └── java │ │ ├── com │ │ └── hzihui │ │ │ └── cloud │ │ │ └── alibaba │ │ │ └── consumer │ │ │ ├── configuration │ │ │ ├── GlobalFeignClientConfiguration.java │ │ │ ├── UserCenterRibbonConfiguration.java │ │ │ ├── ExtendBalancer.java │ │ │ ├── NacosWeightRandomRule.java │ │ │ └── NacosFinalRule.java │ │ │ ├── controller │ │ │ └── TestController.java │ │ │ ├── router │ │ │ ├── MqRouter.java │ │ │ └── UserRouter.java │ │ │ ├── ConsumerApplication.java │ │ │ ├── service │ │ │ ├── UserServiceImpl.java │ │ │ └── DubboUserServiceImpl.java │ │ │ └── handler │ │ │ ├── MqHandler.java │ │ │ └── UserHandler.java │ │ └── ribbonConfiguration │ │ └── RibbonConfiguration.java └── pom.xml ├── spring-cloud-alibaba-actuator ├── target │ └── classes │ │ └── application.yml ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── hzihui │ │ └── cloud │ │ └── alibaba │ │ └── actuator │ │ └── SpringBootAdminApplication.java └── pom.xml ├── spring-cloud-alibaba-gateway ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── hzihui │ │ │ └── cloud │ │ │ └── alibaba │ │ │ └── gateway │ │ │ └── GatewayApplication.java │ │ └── resources │ │ └── application.yml ├── target │ └── classes │ │ └── application.yml └── pom.xml ├── .gitignore ├── spring-cloud-alibaba-rocketmq-consumer ├── target │ └── classes │ │ └── application.yml ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── hzihui │ │ └── cloud │ │ └── alibaba │ │ └── rocketmq │ │ ├── service │ │ └── StreamRocketMQService.java │ │ └── RocketConsumerApplication.java └── pom.xml ├── spring-cloud-alibaba-provider ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── hzihui │ │ │ └── cloud │ │ │ └── alibaba │ │ │ └── provider │ │ │ ├── ProviderAplication.java │ │ │ ├── service │ │ │ └── ProductServiceImpl.java │ │ │ ├── router │ │ │ └── ProductRouter.java │ │ │ └── handler │ │ │ └── ProductHanlder.java │ │ └── resources │ │ └── application.yml ├── target │ └── classes │ │ └── application.yml └── pom.xml ├── LICENSE └── pom.xml /[Help: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /target/sonar/.sonar_lock: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /baseToken.txt: -------------------------------------------------------------------------------- 1 | mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=bf029bbbdd3f5db653435a30430d0871cc810a14 -Dsonar.java.binaries=target/sonar 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-cloud-alibaba-demo 2 | > spring cloud alibaba Integrate everything demo
3 | ### 目前已整合内容: 4 | - Nacos服务发现 5 | - Ribbon 负载均衡 6 | - Feign RestAPI服务调用 7 | - Dubbo RPC服务调用 8 | - Sentinel 流量防控 9 | - Nacos 配置中心 10 | - Spring cloud Gateway API网关 11 | - Apache RocketMQ 消息通信 12 | - Sleuth 链路监控 13 | 14 | -------------------------------------------------------------------------------- /target/sonar/report-task.txt: -------------------------------------------------------------------------------- 1 | projectKey=com.hzihui.cloud.alibaba:spring-cloud-alibaba-demo 2 | serverUrl=http://localhost:9000 3 | serverVersion=7.8.0.26217 4 | dashboardUrl=http://localhost:9000/dashboard?id=com.hzihui.cloud.alibaba%3Aspring-cloud-alibaba-demo 5 | ceTaskId=AW0PyWK6_wuxgT0UK9tZ 6 | ceTaskUrl=http://localhost:9000/api/ce/task?id=AW0PyWK6_wuxgT0UK9tZ 7 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-api/src/main/java/com/hzihui/cloud/alibaba/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.service; 2 | 3 | import com.hzihui.cloud.alibaba.vo.UserVO; 4 | 5 | /** 6 | * @author HZI.HUI 7 | * @date 2019/8/7 22:05 8 | */ 9 | public interface UserService { 10 | 11 | UserVO getUserAndProductsByUserId(String userId); 12 | } 13 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/target/classes/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: consumer 4 | cloud: 5 | nacos: 6 | config: 7 | server-addr: localhost:8848 8 | file-extension: yml 9 | #shared-dataids: ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} 10 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 11 | profiles: 12 | active: dev 13 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: consumer 4 | cloud: 5 | nacos: 6 | config: 7 | server-addr: localhost:8848 8 | file-extension: yml 9 | #shared-dataids: ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} 10 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 11 | profiles: 12 | active: dev 13 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-actuator/target/classes/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8082 3 | spring: 4 | application: 5 | name: actuator 6 | main: 7 | # Spring Boot 2.1 需要设定 8 | allow-bean-definition-overriding: true 9 | cloud: 10 | nacos: 11 | discovery: 12 | server-addr: localhost:8848 13 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 14 | cluster-name: SZ 15 | metadata: 16 | version: v1 17 | target-version: v1 18 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-actuator/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8082 3 | spring: 4 | application: 5 | name: actuator 6 | main: 7 | # Spring Boot 2.1 需要设定 8 | allow-bean-definition-overriding: true 9 | cloud: 10 | nacos: 11 | discovery: 12 | server-addr: localhost:8848 13 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 14 | cluster-name: SZ 15 | metadata: 16 | version: v1 17 | target-version: v1 18 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-api/src/main/java/com/hzihui/cloud/alibaba/vo/UserVO.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.vo; 2 | 3 | import com.hzihui.cloud.alibaba.entity.User; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author HZI.HUI 10 | * @date 2019/8/7 22:05 11 | */ 12 | @Data 13 | public class UserVO extends User implements Serializable { 14 | 15 | private static final long serialVersionUID = -3342623646828335179L; 16 | 17 | String bakcName; 18 | } 19 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/configuration/GlobalFeignClientConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.configuration; 2 | 3 | import feign.Logger; 4 | import org.springframework.context.annotation.Bean; 5 | 6 | 7 | /** 8 | * 全局feign日志打印配置 9 | * @author HZI.HUI 10 | * @date 2019/7/21 14:32 11 | */ 12 | public class GlobalFeignClientConfiguration { 13 | 14 | @Bean 15 | public Logger.Level level(){ 16 | return Logger.Level.FULL; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-gateway/src/main/java/com/hzihui/cloud/alibaba/gateway/GatewayApplication.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.gateway; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @author HZI.HUI 8 | * @date 2019/9/7 10:42 9 | */ 10 | @SpringBootApplication(scanBasePackages = "com.hzihui.cloud.alibaba") 11 | public class GatewayApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(GatewayApplication.class); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/configuration/UserCenterRibbonConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.configuration; 2 | 3 | import ribbonConfiguration.RibbonConfiguration; 4 | import org.springframework.cloud.netflix.ribbon.RibbonClients; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * Ribbon负载均衡 9 | * @author HZI.HUI 10 | * @date 2019/7/17 22:34 11 | */ 12 | @Configuration 13 | @RibbonClients(defaultConfiguration = RibbonConfiguration.class) 14 | public class UserCenterRibbonConfiguration { 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Example user template template 3 | ### Example user template 4 | 5 | # IntelliJ project files 6 | .idea 7 | *.iml 8 | out 9 | gen 10 | 11 | ### Java template 12 | # Compiled class file 13 | *.class 14 | 15 | # Log file 16 | *.log 17 | 18 | # BlueJ files 19 | *.ctxt 20 | 21 | # Mobile Tools for Java (J2ME) 22 | .mtj.tmp/ 23 | 24 | # Package Files # 25 | *.jar 26 | *.war 27 | *.nar 28 | *.ear 29 | *.zip 30 | *.tar.gz 31 | *.rar 32 | 33 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 34 | hs_err_pid* 35 | 36 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/ribbonConfiguration/RibbonConfiguration.java: -------------------------------------------------------------------------------- 1 | package ribbonConfiguration; 2 | import com.alibaba.cloud.nacos.ribbon.NacosRule; 3 | import com.netflix.loadbalancer.IRule; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * 负载均衡配置 9 | * @author HZI.HUI 10 | * @date 2019/7/17 22:36 11 | */ 12 | @Configuration 13 | public class RibbonConfiguration{ 14 | /** 15 | * 手动配置随机负载均衡 16 | * @return 17 | */ 18 | @Bean 19 | public IRule iRule(){ 20 | return new NacosRule(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-actuator/src/main/java/com/hzihui/cloud/alibaba/actuator/SpringBootAdminApplication.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.actuator; 2 | 3 | import de.codecentric.boot.admin.server.config.EnableAdminServer; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | /** 8 | * @author HZI.HUI 9 | * @date 2019/9/8 16:05 10 | */ 11 | @SpringBootApplication 12 | @EnableAdminServer 13 | public class SpringBootAdminApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(SpringBootAdminApplication.class,args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-api/src/main/java/com/hzihui/cloud/alibaba/service/ProductService.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.service; 2 | 3 | import com.alibaba.cloud.dubbo.annotation.DubboTransported; 4 | import org.springframework.cloud.openfeign.FeignClient; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | 8 | /** 9 | * @author HZI.HUI 10 | * @date 2019/8/7 21:27 11 | */ 12 | @FeignClient(name = "provider") 13 | @DubboTransported(protocol = "dubbo") 14 | public interface ProductService { 15 | 16 | @RequestMapping("/getProductById") 17 | String getProductByUserId(@RequestParam String name); 18 | } 19 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-api/src/main/java/com/hzihui/cloud/alibaba/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.io.Serializable; 9 | 10 | /** 11 | * @author HZI.HUI 12 | * @date 2019/8/7 22:03 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | @Builder 18 | public class User implements Serializable { 19 | 20 | private static final long serialVersionUID = -1170712489013521742L; 21 | private String id; 22 | 23 | private String name; 24 | 25 | private String gender; 26 | 27 | private Integer age; 28 | } 29 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-rocketmq-consumer/target/classes/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: rocketmq-consumer 4 | main: 5 | # Spring Boot 2.1 需要设定 6 | allow-bean-definition-overriding: true 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 11 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 12 | cluster-name: BJ 13 | metadata: 14 | version: v1 15 | target-version: v1 16 | stream: 17 | rocketmq: 18 | binder: 19 | name-server: localhost:9876 20 | bindings: 21 | input: 22 | destination: stream-topic 23 | group: stream-rocketmq-group 24 | server: 25 | port: 4000 26 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/configuration/ExtendBalancer.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.configuration; 2 | 3 | import com.alibaba.nacos.api.naming.pojo.Instance; 4 | import com.alibaba.nacos.client.naming.core.Balancer; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * 继承实现获取实例 10 | * @author HZI.HUI 11 | * @date 2019/7/17 23:20 12 | */ 13 | public class ExtendBalancer extends Balancer { 14 | 15 | /** 16 | * 根据权重,随机选择实例 17 | * 18 | * @param instances 实例列表 19 | * @return 选择的实例 20 | */ 21 | public static Instance getHostByRandomWeight2(List instances) { 22 | return getHostByRandomWeight(instances); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-rocketmq-consumer/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: rocketmq-consumer 4 | main: 5 | # Spring Boot 2.1 需要设定 6 | allow-bean-definition-overriding: true 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 11 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 12 | cluster-name: BJ 13 | metadata: 14 | version: v1 15 | target-version: v1 16 | stream: 17 | rocketmq: 18 | binder: 19 | name-server: localhost:9876 20 | bindings: 21 | input: 22 | destination: stream-topic 23 | group: stream-rocketmq-group 24 | server: 25 | port: 4000 26 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-rocketmq-consumer/src/main/java/com/hzihui/cloud/alibaba/rocketmq/service/StreamRocketMQService.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.rocketmq.service; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.cloud.stream.annotation.StreamListener; 5 | import org.springframework.cloud.stream.messaging.Sink; 6 | import org.springframework.stereotype.Service; 7 | 8 | /** 9 | * @author HZI.HUI 10 | * @date 2019/8/11 15:05 11 | */ 12 | @Service 13 | @Slf4j 14 | public class StreamRocketMQService { 15 | 16 | /** 17 | * 接收普通消息 18 | * @param message 19 | */ 20 | @StreamListener(Sink.INPUT) 21 | public void revice(String message){ 22 | log.info("接收到普通的消息:{}",message); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-provider/src/main/java/com/hzihui/cloud/alibaba/provider/ProviderAplication.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.provider; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.context.annotation.ComponentScan; 7 | 8 | /** 9 | * @author HZI.HUI 10 | * @date 2019/8/7 21:24 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | @ComponentScan(basePackages = {"com.hzihui.cloud.alibaba"}) 15 | public class ProviderAplication { 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(ProviderAplication.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-api/src/main/java/com/hzihui/cloud/alibaba/entity/Product.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.io.Serializable; 9 | import java.math.BigDecimal; 10 | 11 | /** 12 | * @author HZI.HUI 13 | * @date 2019/8/7 21:28 14 | */ 15 | @Data 16 | @AllArgsConstructor 17 | @NoArgsConstructor 18 | @Builder 19 | public class Product implements Serializable { 20 | 21 | private static final long serialVersionUID = -7331710101790130857L; 22 | private String id; 23 | 24 | private String name; 25 | 26 | private String content; 27 | 28 | private BigDecimal price; 29 | 30 | private String userId; 31 | } 32 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-provider/src/main/java/com/hzihui/cloud/alibaba/provider/service/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.provider.service; 2 | 3 | import com.hzihui.cloud.alibaba.entity.Product; 4 | import com.hzihui.cloud.alibaba.service.ProductService; 5 | import org.apache.dubbo.config.annotation.Service; 6 | 7 | import javax.annotation.PostConstruct; 8 | import java.math.BigDecimal; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * @author HZI.HUI 14 | * @date 2019/8/7 21:33 15 | */ 16 | @Service(version = "1.0.0",interfaceName = "dubboProductService") 17 | public class ProductServiceImpl implements ProductService { 18 | 19 | 20 | @Override 21 | public String getProductByUserId(String name) { 22 | return name + "调用了Product服务"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.cloud.context.config.annotation.RefreshScope; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @author HZI.HUI 11 | * @date 2019/9/8 10:25 12 | */ 13 | @RestController 14 | @RequestMapping("test") 15 | @RefreshScope 16 | public class TestController { 17 | 18 | @Value("${test.config}") 19 | private String testConfigStr; 20 | 21 | @GetMapping("testNacosConfig") 22 | public String testNacosConfig(){ 23 | return testConfigStr; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-provider/target/classes/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: provider 4 | main: 5 | # Spring Boot 2.1 需要设定 6 | allow-bean-definition-overriding: true 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 11 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 12 | cluster-name: BJ 13 | metadata: 14 | version: v1 15 | target-version: v1 16 | dubbo: 17 | scan: 18 | # dubbo 服务扫描基准包 19 | base-packages: com.hzihui.cloud.alibaba 20 | protocol: 21 | # dubbo 协议 22 | name: dubbo 23 | # dubbo 协议端口( -1 表示自增端口,从 20880 开始) 24 | port: -1 25 | registry: 26 | # 挂载到 Spring Cloud 注册中心 27 | address: spring-cloud://localhost 28 | server: 29 | port: 2003 30 | management: 31 | endpoints: 32 | web: 33 | exposure: 34 | include: "*" 35 | endpoint: 36 | health: 37 | show-details: always 38 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-provider/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: provider 4 | main: 5 | # Spring Boot 2.1 需要设定 6 | allow-bean-definition-overriding: true 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 11 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 12 | cluster-name: BJ 13 | metadata: 14 | version: v1 15 | target-version: v1 16 | dubbo: 17 | scan: 18 | # dubbo 服务扫描基准包 19 | base-packages: com.hzihui.cloud.alibaba 20 | protocol: 21 | # dubbo 协议 22 | name: dubbo 23 | # dubbo 协议端口( -1 表示自增端口,从 20880 开始) 24 | port: -1 25 | registry: 26 | # 挂载到 Spring Cloud 注册中心 27 | address: spring-cloud://localhost 28 | server: 29 | port: 2003 30 | management: 31 | endpoints: 32 | web: 33 | exposure: 34 | include: "*" 35 | endpoint: 36 | health: 37 | show-details: always 38 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-rocketmq-consumer/src/main/java/com/hzihui/cloud/alibaba/rocketmq/RocketConsumerApplication.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.rocketmq; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.openfeign.EnableFeignClients; 6 | import org.springframework.cloud.stream.annotation.EnableBinding; 7 | import org.springframework.cloud.stream.messaging.Sink; 8 | import org.springframework.context.annotation.ComponentScan; 9 | 10 | /** 11 | * @author HZI.HUI 12 | * @date 2019/8/11 14:59 13 | */ 14 | @SpringBootApplication 15 | @ComponentScan(basePackages = "com.hzihui.cloud.alibaba") 16 | @EnableFeignClients(basePackages = "com.hzihui.cloud.alibaba") 17 | @EnableBinding(Sink.class) 18 | public class RocketConsumerApplication { 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(RocketConsumerApplication.class,args); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/router/MqRouter.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.router; 2 | 3 | import com.hzihui.cloud.alibaba.consumer.handler.MqHandler; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.reactive.function.server.RequestPredicates; 7 | import org.springframework.web.reactive.function.server.RouterFunction; 8 | import org.springframework.web.reactive.function.server.RouterFunctions; 9 | import org.springframework.web.reactive.function.server.ServerResponse; 10 | 11 | /** 12 | * @author HZI.HUI 13 | * @date 2019/8/11 15:09 14 | */ 15 | @Configuration 16 | public class MqRouter { 17 | 18 | @Bean 19 | public RouterFunction responseMqRouter(MqHandler mqHandler){ 20 | return RouterFunctions.route(RequestPredicates.GET("/sendMessage/{content}"), 21 | mqHandler::sendMessage); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-gateway/target/classes/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: gateway 4 | main: 5 | allow-bean-definition-overriding: true 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: localhost:8848 10 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 11 | gateway: 12 | # discovery: 13 | # locator: 14 | # enabled: true 15 | routes: 16 | - id: test 17 | uri: lb://consumer 18 | predicates: 19 | - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2027-01-21T17:42:47.789-07:00[America/Denver] 20 | #- Method=POST 21 | server: 22 | port: 5000 23 | #服务监控 24 | management: 25 | endpoints: 26 | web: 27 | exposure: 28 | include: "*" 29 | endpoint: 30 | health: 31 | show-details: always 32 | logging: 33 | level: 34 | org.springframework.cloud.gateway: trace 35 | org.springframework.http.server.reactive: debug 36 | org.springframework.web.reactive: debug 37 | reactor.ipc.netty: debug 38 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-gateway/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: gateway 4 | main: 5 | allow-bean-definition-overriding: true 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: localhost:8848 10 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 11 | gateway: 12 | # discovery: 13 | # locator: 14 | # enabled: true 15 | routes: 16 | - id: test 17 | uri: lb://consumer 18 | predicates: 19 | - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2027-01-21T17:42:47.789-07:00[America/Denver] 20 | #- Method=POST 21 | server: 22 | port: 5000 23 | #服务监控 24 | management: 25 | endpoints: 26 | web: 27 | exposure: 28 | include: "*" 29 | endpoint: 30 | health: 31 | show-details: always 32 | logging: 33 | level: 34 | org.springframework.cloud.gateway: trace 35 | org.springframework.http.server.reactive: debug 36 | org.springframework.web.reactive: debug 37 | reactor.ipc.netty: debug 38 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-provider/src/main/java/com/hzihui/cloud/alibaba/provider/router/ProductRouter.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.provider.router; 2 | 3 | import com.hzihui.cloud.alibaba.provider.handler.ProductHanlder; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.reactive.function.server.RequestPredicates; 7 | import org.springframework.web.reactive.function.server.RouterFunction; 8 | import org.springframework.web.reactive.function.server.RouterFunctions; 9 | import org.springframework.web.reactive.function.server.ServerResponse; 10 | 11 | /** 12 | * @author HZI.HUI 13 | * @date 2019/8/7 21:37 14 | */ 15 | @Configuration 16 | public class ProductRouter { 17 | 18 | 19 | @Bean 20 | public RouterFunction responseRouterFunction(ProductHanlder productHanlder){ 21 | return RouterFunctions 22 | .route(RequestPredicates.GET("/getProductById"), 23 | productHanlder::getProductById); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/ConsumerApplication.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.openfeign.EnableFeignClients; 6 | import org.springframework.cloud.stream.annotation.EnableBinding; 7 | import org.springframework.cloud.stream.messaging.Source; 8 | import org.springframework.context.annotation.ComponentScan; 9 | 10 | /** 11 | * @author HZI.HUI 12 | * @date 2019/8/7 21:58 13 | */ 14 | @SpringBootApplication 15 | @ComponentScan(basePackages = "com.hzihui.cloud.alibaba") 16 | //@EnableFeignClients(basePackages = "com.hzihui.cloud.alibaba",defaultConfiguration = GlobalFeignClientConfiguration.class) 17 | @EnableFeignClients(basePackages = "com.hzihui.cloud.alibaba") 18 | @EnableBinding(Source.class) 19 | public class ConsumerApplication { 20 | 21 | public static void main(String[] args) { 22 | SpringApplication.run(ConsumerApplication.class); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 hzihui 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.service; 2 | 3 | import com.hzihui.cloud.alibaba.entity.User; 4 | import com.hzihui.cloud.alibaba.service.ProductService; 5 | import com.hzihui.cloud.alibaba.service.UserService; 6 | import com.hzihui.cloud.alibaba.vo.UserVO; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.beans.BeanUtils; 9 | import org.springframework.stereotype.Service; 10 | 11 | /** 12 | * @author HZI.HUI 13 | * @date 2019/8/7 22:07 14 | */ 15 | @Service("userService") 16 | @RequiredArgsConstructor 17 | public class UserServiceImpl implements UserService { 18 | 19 | private final ProductService productService; 20 | 21 | @Override 22 | public UserVO getUserAndProductsByUserId(String userId) { 23 | User user = User.builder().id("2000").name("Feign").gender("男").age(18).build(); 24 | UserVO userVO = new UserVO(); 25 | BeanUtils.copyProperties(user,userVO); 26 | userVO.setBakcName(productService.getProductByUserId(user.getName())); 27 | return userVO; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-provider/src/main/java/com/hzihui/cloud/alibaba/provider/handler/ProductHanlder.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.provider.handler; 2 | 3 | import com.hzihui.cloud.alibaba.service.ProductService; 4 | import lombok.RequiredArgsConstructor; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.web.reactive.function.server.ServerRequest; 7 | import org.springframework.web.reactive.function.server.ServerResponse; 8 | import reactor.core.publisher.Mono; 9 | 10 | import java.util.Optional; 11 | 12 | /** 13 | * @author HZI.HUI 14 | * @date 2019/8/7 21:38 15 | */ 16 | @Component 17 | @RequiredArgsConstructor 18 | public class ProductHanlder { 19 | 20 | private final ProductService productService; 21 | 22 | public Mono getProductById(ServerRequest serverRequest){ 23 | Optional name = serverRequest.queryParam("name"); 24 | String resultDesc = null; 25 | if(name.isPresent()){ 26 | resultDesc = this.productService.getProductByUserId(name.get()); 27 | } 28 | return ServerResponse.ok().body(Mono.just(resultDesc), String.class); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/router/UserRouter.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.router; 2 | 3 | import com.hzihui.cloud.alibaba.consumer.handler.UserHandler; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.reactive.function.server.RequestPredicates; 7 | import org.springframework.web.reactive.function.server.RouterFunction; 8 | import org.springframework.web.reactive.function.server.RouterFunctions; 9 | import org.springframework.web.reactive.function.server.ServerResponse; 10 | 11 | /** 12 | * 用户Mapping控制 13 | * @author HZI.HUI 14 | * @date 2019/8/7 15 | */ 16 | @Configuration 17 | public class UserRouter { 18 | 19 | @Bean 20 | public RouterFunction responseRouterFunction(UserHandler userHandler){ 21 | return RouterFunctions 22 | .route(RequestPredicates.GET("/feign/{id}"), 23 | userHandler::getByUserId) 24 | .andRoute(RequestPredicates.GET("/dubbo/{id}"), 25 | userHandler::dubboUserInfo); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-provider/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-cloud-alibaba-demo 7 | com.hzihui.cloud.alibaba 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-cloud-alibaba-provider 13 | 14 | 15 | 1.8 16 | 17 | 18 | 19 | 20 | com.hzihui.cloud.alibaba 21 | spring-cloud-alibaba-api 22 | 1.0-SNAPSHOT 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-actuator 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/service/DubboUserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.service; 2 | 3 | import com.hzihui.cloud.alibaba.entity.User; 4 | import com.hzihui.cloud.alibaba.service.ProductService; 5 | import com.hzihui.cloud.alibaba.service.UserService; 6 | import com.hzihui.cloud.alibaba.vo.UserVO; 7 | import org.apache.dubbo.config.annotation.Reference; 8 | import org.springframework.beans.BeanUtils; 9 | import org.springframework.stereotype.Service; 10 | 11 | /** 12 | * @author HZI.HUI 13 | * @date 2019/8/7 22:07 14 | */ 15 | @Service("dubboUserService") 16 | public class DubboUserServiceImpl implements UserService { 17 | 18 | @Reference(interfaceName = "dubboProductService",version = "1.0.0") 19 | private ProductService productService; 20 | 21 | @Override 22 | public UserVO getUserAndProductsByUserId(String userId) { 23 | User user = User.builder().id("1000").name("Dubbo").gender("男").age(28).build(); 24 | UserVO userVO = new UserVO(); 25 | BeanUtils.copyProperties(user,userVO); 26 | userVO.setBakcName(productService.getProductByUserId(user.getName())); 27 | return userVO; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-actuator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-cloud-alibaba-demo 7 | com.hzihui.cloud.alibaba 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-cloud-alibaba-actuator 13 | 14 | 15 | 16 | de.codecentric 17 | spring-boot-admin-starter-server 18 | 2.1.6 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | com.alibaba.cloud 26 | spring-cloud-starter-alibaba-nacos-discovery 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/target/classes/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: consumer 4 | main: 5 | # Spring Boot 2.1 需要设定 6 | allow-bean-definition-overriding: true 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 11 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 12 | cluster-name: SZ 13 | metadata: 14 | version: v1 15 | target-version: v1 16 | sentinel: 17 | transport: 18 | dashboard: localhost:8080 19 | stream: 20 | rocketmq: 21 | binder: 22 | name-server: localhost:9876 23 | bindings: 24 | output: 25 | destination: stream-topic 26 | dubbo: 27 | registry: 28 | # 挂载到 Spring Cloud 注册中心 29 | address: spring-cloud://localhost 30 | cloud: 31 | subscribed-services: provider 32 | 33 | server: 34 | port: 3000 35 | ribbon: 36 | eager-load: 37 | enabled: true 38 | clients: provider 39 | #java代码形式配置细粒度日志 40 | #logging: 41 | # level: 42 | # com.hzihui.cloud.alibaba.service.ProductService: debug 43 | #服务监控 44 | management: 45 | endpoints: 46 | web: 47 | exposure: 48 | include: "*" 49 | endpoint: 50 | health: 51 | show-details: always 52 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: consumer 4 | main: 5 | # Spring Boot 2.1 需要设定 6 | allow-bean-definition-overriding: true 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 11 | namespace: 9466c0d9-b5bd-4f1a-9936-10e37656afdf 12 | cluster-name: SZ 13 | metadata: 14 | version: v1 15 | target-version: v1 16 | sentinel: 17 | transport: 18 | dashboard: localhost:8080 19 | stream: 20 | rocketmq: 21 | binder: 22 | name-server: localhost:9876 23 | bindings: 24 | output: 25 | destination: stream-topic 26 | dubbo: 27 | registry: 28 | # 挂载到 Spring Cloud 注册中心 29 | address: spring-cloud://localhost 30 | cloud: 31 | subscribed-services: provider 32 | 33 | server: 34 | port: 3000 35 | ribbon: 36 | eager-load: 37 | enabled: true 38 | clients: provider 39 | #java代码形式配置细粒度日志 40 | #logging: 41 | # level: 42 | # com.hzihui.cloud.alibaba.service.ProductService: debug 43 | #服务监控 44 | management: 45 | endpoints: 46 | web: 47 | exposure: 48 | include: "*" 49 | endpoint: 50 | health: 51 | show-details: always 52 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/handler/MqHandler.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.handler; 2 | 3 | import lombok.RequiredArgsConstructor; 4 | import org.springframework.cloud.stream.messaging.Source; 5 | import org.springframework.messaging.support.MessageBuilder; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.reactive.function.server.ServerRequest; 8 | import org.springframework.web.reactive.function.server.ServerResponse; 9 | import reactor.core.publisher.Mono; 10 | 11 | /** 12 | * @author HZI.HUI 13 | * @date 2019/8/11 15:10 14 | */ 15 | @RestController 16 | @RequiredArgsConstructor 17 | public class MqHandler { 18 | 19 | private final Source source; 20 | /** 21 | * 消息提供者 22 | * @param serverRequest 23 | * @return 24 | */ 25 | public Mono sendMessage(ServerRequest serverRequest){ 26 | String content = serverRequest.pathVariable("content"); 27 | this.source.output() 28 | .send( 29 | MessageBuilder.withPayload(content).build() 30 | ); 31 | return ServerResponse.ok() 32 | .body(Mono.just(true),Boolean.class); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-rocketmq-consumer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-cloud-alibaba-demo 7 | com.hzihui.cloud.alibaba 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-cloud-alibaba-rocketmq-consumer 13 | 14 | 15 | 1.8 16 | 17 | 18 | 19 | 20 | com.hzihui.cloud.alibaba 21 | spring-cloud-alibaba-api 22 | 1.0-SNAPSHOT 23 | 24 | 25 | com.alibaba.cloud 26 | spring-cloud-starter-dubbo 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-gateway/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-cloud-alibaba-demo 7 | com.hzihui.cloud.alibaba 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-cloud-alibaba-gateway 13 | 14 | 15 | org.springframework.cloud 16 | spring-cloud-starter-gateway 17 | 18 | 19 | 20 | 21 | com.alibaba.cloud 22 | spring-cloud-starter-alibaba-nacos-discovery 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-actuator 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-cloud-alibaba-demo 7 | com.hzihui.cloud.alibaba 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-cloud-alibaba-consumer 13 | 14 | 15 | 16 | com.hzihui.cloud.alibaba 17 | spring-cloud-alibaba-api 18 | 1.0-SNAPSHOT 19 | 20 | 21 | 22 | 23 | com.alibaba.cloud 24 | spring-cloud-starter-alibaba-sentinel 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-actuator 31 | 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-zipkin 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/configuration/NacosWeightRandomRule.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.configuration; 2 | import com.alibaba.cloud.nacos.NacosDiscoveryProperties; 3 | import com.alibaba.cloud.nacos.ribbon.NacosServer; 4 | import com.alibaba.nacos.api.exception.NacosException; 5 | import com.alibaba.nacos.api.naming.pojo.Instance; 6 | import com.netflix.client.config.IClientConfig; 7 | import com.netflix.loadbalancer.AbstractLoadBalancerRule; 8 | import com.netflix.loadbalancer.DynamicServerListLoadBalancer; 9 | import com.netflix.loadbalancer.Server; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | 13 | /** 14 | * Ribbon 整合 nacos 权重负载均衡 15 | * @author HZI.HUI 16 | * @date 2019/7/17 23:01 17 | */ 18 | @Slf4j 19 | public class NacosWeightRandomRule extends AbstractLoadBalancerRule { 20 | 21 | @Autowired 22 | private NacosDiscoveryProperties discoveryProperties; 23 | 24 | @Override 25 | public Server choose(Object o) { 26 | //动态获取服务列表中的服务 27 | DynamicServerListLoadBalancer loadBalancer = (DynamicServerListLoadBalancer) getLoadBalancer(); 28 | String name = loadBalancer.getName(); 29 | try { 30 | //通过nacos根据获取的服务名称获取具体的服务实例 31 | Instance instance = discoveryProperties.namingServiceInstance() 32 | .selectOneHealthyInstance(name); 33 | 34 | log.info("选中的instance = {}", instance); 35 | return new NacosServer(instance); 36 | } catch (NacosException e) { 37 | log.error("发生异常", e); 38 | return null; 39 | } 40 | } 41 | 42 | @Override 43 | public void initWithNiwsConfig(IClientConfig iClientConfig) { 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/handler/UserHandler.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.handler; 2 | 3 | import com.hzihui.cloud.alibaba.consumer.service.DubboUserServiceImpl; 4 | import com.hzihui.cloud.alibaba.consumer.service.UserServiceImpl; 5 | import com.hzihui.cloud.alibaba.service.UserService; 6 | import com.hzihui.cloud.alibaba.vo.UserVO; 7 | import org.springframework.web.bind.annotation.RestController; 8 | import org.springframework.web.reactive.function.server.ServerRequest; 9 | import org.springframework.web.reactive.function.server.ServerResponse; 10 | import reactor.core.publisher.Mono; 11 | 12 | import javax.annotation.Resource; 13 | 14 | /** 15 | * @author HZI.HUI 16 | * @date 2019/8/7 22:01 17 | */ 18 | @RestController 19 | public class UserHandler { 20 | 21 | @Resource(name = "userService") 22 | private UserService userService; 23 | 24 | @Resource(name = "dubboUserService") 25 | private UserService dubboUserService; 26 | 27 | /** 28 | * 使用 openfeign 调用 {@link UserServiceImpl} 29 | * @param serverRequest 30 | * @return 31 | */ 32 | public Mono getByUserId(ServerRequest serverRequest){ 33 | UserVO userVo = this.userService.getUserAndProductsByUserId( 34 | serverRequest.pathVariable("id")); 35 | return ServerResponse.ok() 36 | .body(Mono.just(userVo), UserVO.class); 37 | } 38 | 39 | /** 40 | * 使用dubbo调用 {@link DubboUserServiceImpl} 41 | * @param serverRequest 42 | * @return 43 | */ 44 | public Mono dubboUserInfo(ServerRequest serverRequest){ 45 | UserVO userVo = this.dubboUserService.getUserAndProductsByUserId( 46 | serverRequest.pathVariable("id")); 47 | return ServerResponse.ok() 48 | .body(Mono.just(userVo), UserVO.class); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.hzihui.cloud.alibaba 8 | spring-cloud-alibaba-demo 9 | pom 10 | 1.0-SNAPSHOT 11 | 12 | spring-cloud-alibaba-provider 13 | spring-cloud-alibaba-api 14 | spring-cloud-alibaba-consumer 15 | spring-cloud-alibaba-rocketmq-consumer 16 | spring-cloud-alibaba-gateway 17 | spring-cloud-alibaba-actuator 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-parent 23 | 2.1.6.RELEASE 24 | 25 | 26 | 27 | 28 | 1.8 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-dependencies 36 | Greenwich.SR1 37 | pom 38 | import 39 | 40 | 41 | com.alibaba.cloud 42 | spring-cloud-alibaba-dependencies 43 | 2.1.0.RELEASE 44 | pom 45 | import 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-cloud-alibaba-demo 7 | com.hzihui.cloud.alibaba 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-cloud-alibaba-api 13 | 14 | 15 | 16 | org.projectlombok 17 | lombok 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-webflux 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-test 26 | test 27 | 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-openfeign 32 | 33 | 34 | 35 | com.alibaba.cloud 36 | spring-cloud-starter-dubbo 37 | 38 | 39 | 40 | com.alibaba.cloud 41 | spring-cloud-starter-alibaba-nacos-discovery 42 | 43 | 44 | 45 | com.alibaba.cloud 46 | spring-cloud-starter-alibaba-nacos-config 47 | 48 | 49 | 50 | org.apache.rocketmq 51 | rocketmq-spring-boot-starter 52 | 2.0.3 53 | 54 | 55 | 56 | com.alibaba.cloud 57 | spring-cloud-starter-stream-rocketmq 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /spring-cloud-alibaba-consumer/src/main/java/com/hzihui/cloud/alibaba/consumer/configuration/NacosFinalRule.java: -------------------------------------------------------------------------------- 1 | package com.hzihui.cloud.alibaba.consumer.configuration; 2 | import com.alibaba.cloud.nacos.NacosDiscoveryProperties; 3 | import com.alibaba.cloud.nacos.ribbon.NacosServer; 4 | import com.alibaba.nacos.api.naming.NamingService; 5 | import com.alibaba.nacos.api.naming.pojo.Instance; 6 | import com.alibaba.nacos.client.naming.utils.CollectionUtils; 7 | import com.netflix.client.config.IClientConfig; 8 | import com.netflix.loadbalancer.AbstractLoadBalancerRule; 9 | import com.netflix.loadbalancer.DynamicServerListLoadBalancer; 10 | import com.netflix.loadbalancer.Server; 11 | import lombok.extern.slf4j.Slf4j; 12 | import org.apache.commons.lang3.StringUtils; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | 15 | import java.util.List; 16 | import java.util.Objects; 17 | import java.util.stream.Collectors; 18 | 19 | /** 20 | * Ribbon 整合 nacos 权重负载均衡 21 | * @author HZI.HUI 22 | * @date 2019/7/17 23:17 23 | */ 24 | @Slf4j 25 | public class NacosFinalRule extends AbstractLoadBalancerRule { 26 | 27 | @Autowired 28 | private NacosDiscoveryProperties nacosDiscoveryProperties; 29 | 30 | @Override 31 | public Server choose(Object key) { 32 | // 负载均衡规则:优先选择同集群下,符合metadata的实例 33 | // 如果没有,就选择所有集群下,符合metadata的实例 34 | try { 35 | String clusterName = this.nacosDiscoveryProperties.getClusterName(); 36 | String targetVersion = this.nacosDiscoveryProperties.getMetadata().get("target-version"); 37 | 38 | DynamicServerListLoadBalancer loadBalancer = (DynamicServerListLoadBalancer) getLoadBalancer(); 39 | String name = loadBalancer.getName(); 40 | 41 | NamingService namingService = this.nacosDiscoveryProperties.namingServiceInstance(); 42 | 43 | // 所有实例 44 | List instances = namingService.selectInstances(name, true); 45 | 46 | List metadataMatchInstances = instances; 47 | // 如果配置了版本映射,那么只调用元数据匹配的实例 48 | if (StringUtils.isNotBlank(targetVersion)) { 49 | metadataMatchInstances = instances.stream() 50 | .filter(instance -> Objects.equals(targetVersion, instance.getMetadata().get("version"))) 51 | .collect(Collectors.toList()); 52 | if (CollectionUtils.isEmpty(metadataMatchInstances)) { 53 | log.warn("未找到元数据匹配的目标实例!请检查配置。targetVersion = {}, instance = {}", targetVersion, instances); 54 | return null; 55 | } 56 | } 57 | 58 | List clusterMetadataMatchInstances = metadataMatchInstances; 59 | // 如果配置了集群名称,需筛选同集群下元数据匹配的实例 60 | if (StringUtils.isNotBlank(clusterName)) { 61 | clusterMetadataMatchInstances = metadataMatchInstances.stream() 62 | .filter(instance -> Objects.equals(clusterName, instance.getClusterName())) 63 | .collect(Collectors.toList()); 64 | if (CollectionUtils.isEmpty(clusterMetadataMatchInstances)) { 65 | clusterMetadataMatchInstances = metadataMatchInstances; 66 | log.warn("发生跨集群调用。clusterName = {}, targetVersion = {}, clusterMetadataMatchInstances = {}", clusterName, targetVersion, clusterMetadataMatchInstances); 67 | } 68 | } 69 | 70 | Instance instance = ExtendBalancer.getHostByRandomWeight2(clusterMetadataMatchInstances); 71 | log.info("调用的实例端口号:{},实例名称:{},targetVersion={},clusterName={}", 72 | instance.getPort(), 73 | instance.getServiceName(), 74 | instance.getMetadata().get("target-version"), 75 | instance.getClusterName()); 76 | return new NacosServer(instance); 77 | } catch (Exception e) { 78 | log.warn("发生异常", e); 79 | return null; 80 | } 81 | } 82 | 83 | @Override 84 | public void initWithNiwsConfig(IClientConfig iClientConfig) { 85 | 86 | } 87 | } 88 | --------------------------------------------------------------------------------