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>
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Spring Cloud 3:Spring Boot快速入门
- Spring Cloud Zuul 快速入门
- Spring Cloud Alibaba从入门到放弃-Nacos配置加载规则详解
- Spring Cloud Alibaba从入门到放弃-Nacos多环境配置文件详解
- Spring Cloud Alibaba从入门到放弃-Nacos数据持久化
- Spring Cloud Spring Boot mybatis分布式微服务云架构(一)快速入门
- Spring Cloud(六)服务网关 zuul 快速入门
- Spring Cloud Spring Boot mybatis分布式微服务云架构(一)快速入门
- spring cloud快速入门教程(二)服务注册中心Eureka
- Spring Cloud Alibaba Nacos 入门详解
- 基于SpringCloudAlibaba和Nacos微服务体系下的FeignClient客户端负载均衡入门实例
- Spring Cloud Alibaba从入门到放弃-构建服务注册中心(Nacos)
- (Spring Cloud微服务实战-书中之坑)分布式服务跟踪:Spring Cloud Sleuth 快速入门
- SpringCloud SpringBoot mybatis 分布式微服务(一)Spring Boot快速入门
- spring cloud快速入门教程(五)进程间调用和微服务负载均衡(RestTemplate+Ribbon)
- spring-cloud-alibaba-dubbo 入门
- spring cloud快速入门教程(四)路由网关Zuul
- SpringCloud快速入门
- spring基础:快速入门spring cloud(6):统一配置管理之spring cloud config
- spring cloud快速入门教程(七)服务断路器Hystrix