Spring Cloud Alibaba 组件 急速入门

快速开始

docker 搭建各种环境资料:[必学docker容器技术](https://www.e404e.cn/)

  • Clone 项目 --这是注册中心先下载 window  和Linux版本 我使用的是Linux
git clone https://github.com/nacos-group/nacos-k8s.git

下载完成解压启动即可---存放的目录是自定义的  

Linux 进入 /usr/local/nacos/nacos/bin

---启动命令 sh startup.sh -m standalone

 

 

 

生产者

package com.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


/**
 * 启动类
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年08月15日 15:44:00
 */

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




    }

 

 

package com.alibaba.services;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 接口
 */
public interface ServiceA {


    //接口要加映射注解  --不然报错

    @GetMapping("/hello")
    public String hello(@RequestParam String name);
}

 

 

 

package com.alibaba.services.ServiceImpl;

import com.alibaba.services.ServiceA;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 实现类
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年08月15日 17:42:00
 */


@RestController              //必须加@RestController  注解不加就报错 实现类 ----因为通过远程调用的
public class ServiceAImpl implements ServiceA {

    public String hello(@RequestParam String name) {
        return "hello远程调用" + name;
    }
}

 

 

配置文件  bootstrap.properties

spring.application.name=alibaba-server

server.port=8070

spring.cloud.nacos.discovery.server-addr=192.168.220.128:8848

 

 

 


消费者

package com.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


/**
 * 启动类
 *
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年08月15日 15:47:00
 */

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApp {


    public static void main(String[] args) {
        SpringApplication.run(ClientApp.class, args);
    }


}

 

 

 

 

package com.alibaba.fegin;

import com.alibaba.services.ServiceA;
import org.springframework.cloud.openfeign.FeignClient;

/**
 * AlibabaCloud远程调用 单一接口要加影射 不然报错
 */

@FeignClient("alibaba-server")
public interface ClientFegin extends ServiceA {


    /**
     * 伪代码
     * ---加影射 (@requestMapper("/getName"))
     * public String getName(String name)
     */
}

 

 

 

 

 

 

package com.alibaba.controller;

import com.alibaba.fegin.ClientFegin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 消费Controller
 */
@Slf4j
@RestController
public class TestController {

    @Autowired
    ClientFegin client;

    @GetMapping("/test")
    public String test() {
        String result = client.hello("didi");
        return "Return : " + result;
    }
}

 

 

 

配置文件  bootstrap.properties

spring.application.name=alibaba-client

server.port=9000

spring.cloud.nacos.discovery.server-addr=192.168.220.128:8848

 

 

 

pom文件

<?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">
    <parent>
        <artifactId>alibaba-server-merber</artifactId>
        <groupId>com.alibaba</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>alibaba-server</artifactId>



    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <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>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <optional>true</optional>
        </dependency>


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


    </dependencies>

</project>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值