Spring-Cloud-Alibaba的简单使用(二)

简介: Spring-Cloud-Alibaba的简单使用

1.5 创建服务消费者

spring-cloud-alibaba-demo-consumer

pom.xml

xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.lzj</groupId>
        <artifactId>spring-cloud-alibaba-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>spring-cloud-alibaba-demo-consumer</artifactId>
    <packaging>jar</packaging>
    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->
        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.lzj.spring.cloud.alibaba.consumer.ConsumerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


ConsumerApplication

java

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }
}

application.yml

yaml

spring:
  application:
    name: service-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
server:
  port: 8081
#健康检查
management:
  endpoints:
    web:
      exposure:
        include: "*"

feignClient

java

@FeignClient(value = "service-provider")
public interface ConsumerService {
    @GetMapping(value = "/echo/{string}")
    String echo(@PathVariable("string") String string);
}

controller

java

@RestController
public class ConsumerController {
    @Resource
    private ConsumerService consumerService;
    @GetMapping("/echo/{str}")
    public String echo(@PathVariable String str){
        return "Consumer send str and reply is:" + consumerService.echo(str);
    }
}

1.6 加入分布式配置中心

  • 在消费者中增加依赖
    xml
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
  • 创建bootstrap.properties 文件
    properties
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
  • application.yml中的配置 添加再nacos中

  • application.yml文件删除,这里为保证项目完整,更名为tmp_application.yml
  • 启动测试
  • 动态刷新配置

在nacos配置中加入新配置

yaml

user:
  name: 张三

更改ConsumerController 增加注解@RefreshScope

java

@RestController
@RefreshScope
public class ConsumerController { 
  @Value("${user.name}")
    private String username;
    @GetMapping("/user")
    public String user(){
        return username;
    }
}
  • 启动测试 访问http://localhost:8081/user 返回结果为:张三
  • 更改配置为

yaml

user:
  name: 李四
  • 再次访问: 可以看到结果为:李四

1.7 加入sentinel 流量监控

获取控制台

release 页面 下载最新版本的控制台 jar 包。

启动控制台 sentinel-dashboard-version.jar 替换成真实jar包名

shell

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-version.jar

引入依赖

xml

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

当前spring-cloud-starter-alibaba-sentinel并不支持最新版sentinel 1.7.1的客户端,所以我们下载1.7.0的客户端

  • 配置控制台信息
    yml
spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080
  • 配置文件打开 Sentinel 对 Feign 的支持:feign.sentinel.enabled=true
  • 加入 spring-cloud-starter-openfeign 依赖使 Sentinel starter 中的自动化配置类生效(已加入)

2.使用dubbo

1.1 创建dubbo服务提供者

spring-cloud-alibaba-demo-dubbo-provider

pom.xml

xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.lzj</groupId>
        <artifactId>spring-cloud-alibaba-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>spring-cloud-alibaba-demo-dubbo-provider</artifactId>
    <packaging>pom</packaging>
    <properties>
        <nacos.version>1.1.3</nacos.version>
        <dubbo.version>2.7.3</dubbo.version>
        <spring-context-support.version>1.0.6</spring-context-support.version>
    </properties>
    <dependencies>
        <!-- Apache Dubbo Begin -->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>${nacos.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-kryo</artifactId>
            <version>${dubbo.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.dubbo</groupId>
                    <artifactId>dubbo-common</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring-context-support.version}</version>
        </dependency>
        <!-- Apache Dubbo End -->
    </dependencies>
    <modules>
        <module>spring-cloud-alibaba-demo-dubbo-provider-api</module>
        <module>spring-cloud-alibaba-demo-dubbo-provider-service</module>
    </modules>
</project>


1.2 创建api

spring-cloud-alibaba-demo-dubbo-provider-api

pom.xml

xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.lzj</groupId>
        <artifactId>spring-cloud-alibaba-demo-dubbo-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>spring-cloud-alibaba-demo-dubbo-provider-api</artifactId>
    <packaging>jar</packaging>
</project>

interface

java

package com.lzj.spring.cloud.alibaba.dubbo.provider;
/**
 * @author Zijian Liao
 * @date 2020/1/17 18:36
 * @description
 */
public interface HelloService {
    String hello(String string);
}
目录
相关文章
|
7月前
|
存储 Java Maven
Spring Cloud Alibaba服务问题之服务报错如何解决
Spring Cloud Alibaba提供了一套在Spring Cloud框架基础上构建的微服务解决方案,旨在简化分布式系统的开发和管理;本合集将探讨Spring Cloud Alibaba在实际应用中的部署和使用技巧,以及该框架常见问题的诊断方法和解决步骤。
|
Java Nacos Sentinel
Spring Cloud Alibaba学习指南
由于在2018年Netflix公司宣布对其核心组件Hystrix、Ribbon、zuul、Eureka等进入维护状态,也就是Spring Cloud Netflix系列。由此Spring Cloud Alibaba就诞生了,值得注意的是Spring Cloud Alibaba完全兼容了Spring Cloud Netflix中的Ribbon、Feign、Eureka等组件,所以基于Spring Cloud Netflix的项目可以无缝迁移到Spring Cloud Alibaba。
564 0
|
存储 Dubbo 算法
一.SpringCloudAlibaba极简入门-什么是 Spring Cloud Alibaba
早期在国内做分布式(微服务)应用Dubbo是比较热门的框架,被许多互联网公司所采用,并产生了许多衍生版本,如网易,京东,新浪,当当等等,奈何在2014年10月Dubbo停止维护,在Dubbo停更的时间里Spring Cloud快速追赶上。在2017年9月,阿里宣布重启Dubbo项目,计划对Dubbo进行持续更新维护。2018.2月,阿里将Dubbo捐献给Apache基金会,Dubbo成为Apache孵化器项目。 所以当前微服务架构,Dubbo和SpringCloud比较火,另外还有Thrift、gRPC等等 。很多人把SpringCloud 和Dubbo进行对比,其实两个框架并没有太大的可比
|
7月前
|
消息中间件 Java Maven
Spring Cloud Alibaba 简介
Spring Cloud Alibaba 简介
328 1
|
7月前
|
消息中间件 负载均衡 Java
【从Spring Cloud到Spring Cloud Alibaba,这些改变你都知道吗?】—— 每天一点小知识
【从Spring Cloud到Spring Cloud Alibaba,这些改变你都知道吗?】—— 每天一点小知识
193 0
【从Spring Cloud到Spring Cloud Alibaba,这些改变你都知道吗?】—— 每天一点小知识
|
SpringCloudAlibaba Dubbo 算法
|
XML 负载均衡 Java
Spring Cloud Alibaba学习笔记
Spring Cloud Alibaba学习笔记
|
Java Maven Spring
Spring Cloud Alibaba 项目搭建
Spring Cloud Alibaba 项目搭建
703 0
|
Java Spring
Spring Cloud Alibaba源码 - 21 Ribbon 源码解析
Spring Cloud Alibaba源码 - 21 Ribbon 源码解析
125 0
|
Cloud Native Dubbo Java
Spring Cloud Alibaba遇到的版本兼容问题
Spring Cloud Alibaba遇到的版本兼容问题
876 0