您的位置:首页 > 编程语言 > Java开发

Spring Cloud Alibaba 快速入门

2020-01-13 22:57 381 查看

快速开始

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

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

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

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

 

 

 

[code]生产者

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);
}

}

 

 

[code]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);
}

 

 

 

[code]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

[code]spring.application.name=alibaba-server

server.port=8070

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

 

 

 

消费者

[code]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);
}

}

 

 

 

 

[code]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)
*/
}

 

 

 

 

 

 

[code]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

[code]spring.application.name=alibaba-client

server.port=9000

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

 

 

 

pom文件

[code]<?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>
  • 点赞
  • 收藏
  • 分享
  • 文章举报
Mar向 发布了12 篇原创文章 · 获赞 14 · 访问量 1万+ 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: