spring cloud alibaba入门demo


一、搭建环境

1.1 安装Nacos

下载解压启动
bin/startup.sh -m standalone
访问
ip:8848/nacos/#/login

1.2 构建环境

创建环境,引入依赖

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2.2.0.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

二、开始

2.1 服务注册

2.1.1 引入依赖

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
    dependencies>

2.1.2 启动nacos 客户端

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

2.1.3 配置文件

位置在:src\main\resources\application.yml

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.88.51:8848
  application:
    name: ordermanager
server:
  port: 8080

2.2 配置中心

2.2.1 引入依赖

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>

2.2.2 配置

在src\main\resources创建配置文件 bootstrap.yml

spring:
  application:
    name: ordermanager
  cloud:
    nacos:
      config:
        server-addr: 192.168.88.51:8848

2.2.3 发布配置

在nacos 中发布 Data Id 为**${appllication-name}.${file-extension}** 的配置文件

user.id=1
user.name=james
user.age=23

2.2.4 注入配置

@RestController
@RefreshScope //实时刷新
public class SampleCroller {
    @Value("${user.name}")
    String userName;
    @Value("${user.age}")
    int age;

    @RequestMapping("/user")
    public String simple() {
        return "Hello Nacos Config!" + "Hello " + userName + " " + age ;
    }
}

2.3 服务调用

2.3.1 引入依赖

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
            <version>2.2.2.RELEASEversion>
        dependency>

2.3.2 启用openfien 客户端

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

2.3.3 服务调用

客户端代码

@Component
@FeignClient("ordermanager")
public interface OrderManager {
    @RequestMapping("/user")
    public String getConfig();
}

服务端代码

@RestController
@RefreshScope
public class SampleCroller {
    @Value("${user.name}")
    String userName;
    @Value("${user.age}")
    int age;

    @RequestMapping("/user")
    public String simple() {
        return "Hello Nacos Config!" + "Hello " + userName + " " + age ;
    }
}

2.4 服务网关

2.4.1 引入依赖

<dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
            <version>2.2.2.RELEASEversion>
            <exclusions>
                <exclusion>
                    <artifactId>spring-webartifactId>
                    <groupId>org.springframeworkgroupId>
                exclusion>
                <exclusion>
                    <artifactId>spring-boot-starter-webfluxartifactId>
                    <groupId>org.springframework.bootgroupId>
                exclusion>
            exclusions>
        dependency>

2.4.2 配置路由

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: https://www.baidu.com
          predicates:
            - Query=url,baidu

        - id: qq_route
          uri: https://www.qq.com
          predicates:
            - Query=url,qq

附上github地址: https://github.com/RoundYuanYuan/springcloudalibabademo

你可能感兴趣的:(微服务)