SpringCloud Alibaba

  1. 下载Nacos
    下载地址https://github.com/alibaba/nacos/releases

  2. 复制文件到Linux的/usr/local目录下
    cd /usr/local
    tar -vxf nacos-server-1.4.0.tar.gz
    cd nacos/bin
    sh startup.sh -m standalone
    PS:启动文件位于nacos的bin目录下,这里以单机模式启动,除此还有集群模式

  3. 打开浏览器,输入http://Linux主机IP:8848/nacos/index.html,就可以看到Nacos的登录界面

创建父项目

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.9.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

相当于可用的注册中心

创建子项目(服务者)

导入依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        

创建实体类

public class Goods {

    private Long id;
    private String name;
    private Double price;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Goods(long l, String name, double v) {
       this.id = l;
       this.name = name;
       this.price = v;
    }
}

创建service

@Service
public class GoodsService {

    public Goods findGoods(){
        return new Goods(1L,"MI笔记本5400",5555.0);
    }
}

创建controller

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("Goods")
    public ResponseEntity<Goods> findGoods(){
        return ResponseEntity.ok(goodsService.findGoods());
    }
}

在启动类上面加@EnableDiscoveryClient
访问ip:8848/goods/Goods查看结果

创建子项目(消费者)

导入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

创建config

@Configuration
public class RestTemplateConfig {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

entity

public class Goods {

    private Long id;
    private String name;
    private Double price;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Goods(long l, String name, double v) {
        this.id = l;
        this.name = name;
        this.price = v;
    }

    public Goods() {
    }
}

创建controller

@RestController
@RequestMapping("order")
public class GoodsController {


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("goods")
    public ResponseEntity<Order> findGoods(){
       Goods goods =   restTemplate.getForEntity("http://goods-service/goods/Goods", Goods.class).getBody();
        Order order = new Order(1L,"张三",555.6,goods);

        return ResponseEntity.ok(order);
    }
}

在启动类上面加@EnableDiscoveryClient
在浏览器访问地址 localhost:8848/order/goods

配置中心的使用

在goods-service中添加依赖

 <!--config需要的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

配置文件
bootstrap.yml或者bootstrap.application

spring:
  cloud:
    nacos:
      config:
        server-addr: 192.168.1.24:8848
        file-extension: yaml
        prefix: goods-service
  profiles:
    active: dev
#spring.cloud.nacos.config.server-addr=192.168.1.24:8848
#spring.cloud.nacos.config.file-extension=properties
#spring.cloud.nacos.config.prefix=goods-service
#spring.profiles.active=dev

在这里插入图片描述
在这里插入图片描述

项目测试类

@RestController
@RefreshScope
public class NacosConfigController {

    @Value("${server.port}")
    private String port;

    @Value("${spring.datasource.driver-class-name}")
    private String driver;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @RequestMapping("/config")
    public String config(){
        return "{port='" + port + '\'' +
                ", driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

直接访问:
localhost:8848/config

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值