飞道的博客

Spring-Clould-Alibaba-Nacos

280人阅读  评论(0)

概述:

  1. Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,
  2. 方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。
  3. 依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,
  4. 就可以将 Spring Cloud 应用接入阿里分布式应用解决方案,通过阿里中间件来迅速搭建分布式应用系统。

版本介绍:

Nacos概述

  1. 什么是Nacos
  1. Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,
  2. 帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
  1. 搭建NacosServer
    1.下载 官网地址
    2.网盘地址:百度网盘 提取码:rest
    3.启动

    双击startup.cmd

    浏览器输入http://localhost:8848/nacos/

    用户名和密码都是 nacos

服务注册与发现

1. 创建父工程,在父工程当中导入依赖


 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3.RELEASE</version>
    </parent>

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

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>
  1. 创建两个子工程user与goods工程

  2. 导入依赖
  3. 在启动类添加@EnableDiscoveryClient注解
  4. 创建util包和ResponseResult类用于返回信息
public class ResponseResult extends HashMap {
    public static String SUCCESS_CODE = "200";
    public static String ERROR_CODE = "500";
    public static String DATA_KEY = "data";
    public static String MSG_KEY = "msg";

    private ResponseResult() {
    }

    public ResponseResult set(String key, Object object) {
        super.put(key, object);
        return this;
    }

    private static ResponseResult newResponseResult() {
        return new ResponseResult();
    }

    public static ResponseResult success() {
        return ResponseResult.newResponseResult()
                .set("code", ResponseResult.SUCCESS_CODE)
                .set(ResponseResult.MSG_KEY, "操作成功");
    }

    public static ResponseResult success(String msg) {

        return ResponseResult.newResponseResult()
                .set("code", ResponseResult.SUCCESS_CODE)
                .set(ResponseResult.MSG_KEY, msg);
    }

    public static ResponseResult success(String msg, Object object) {

        return ResponseResult.newResponseResult()
                .set("code", ResponseResult.SUCCESS_CODE)
                .set(ResponseResult.MSG_KEY, msg)
                .set(ResponseResult.DATA_KEY, object);
    }

    public ResponseResult data(Object obj) {
        return this.set("data", obj);
    }

    public static ResponseResult error() {
        return ResponseResult.newResponseResult()
                .set(ResponseResult.MSG_KEY, "操作失败")
                .set("code", ResponseResult.ERROR_CODE);
    }

    public static ResponseResult error(String msg) {
        return ResponseResult.newResponseResult()
                .set(ResponseResult.MSG_KEY, msg)
                .set("code", ResponseResult.ERROR_CODE);
    }

    public static ResponseResult error(String msg, Object object) {
        return ResponseResult.newResponseResult()
                .set(ResponseResult.MSG_KEY, msg)
                .set(ResponseResult.DATA_KEY, object)
                .set("code", ResponseResult.ERROR_CODE);
    }

}


  1. 创建controller
@RestController
public class goodsController {

    @RequestMapping("/getGoods")
    public ResponseResult getGoods(){
        return ResponseResult.success("操作成功");
    }
}

  1. 在配置文件添加添加配置
spring:
  application:
    name: goods-provide

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos服务的地址 不要加http
server:
  port: 8001
  1. 启动运行,在nacos服务端查看
  2. 使用相同方式 把user注册到nacos上

  3. 6.在user工程中通过服务发现调用goods工程
    在user的启动类中添加RestTemplate
@Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

在controller中添加

   @Autowired
    public RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/getGoods")
    public ResponseResult getGoods() {
        List<ServiceInstance> instances = discoveryClient.getInstances("goods-provide");
        System.out.println(instances);
        ServiceInstance serviceInstance = instances.get(0);
        String url = serviceInstance.getUri()+"/getGoods".toString();
        return ResponseResult.success("操作成功",
                restTemplate.getForObject(url,Object.class));
    }

启动访问http://localhost:8000/getGoods


转载:https://blog.csdn.net/jokerdj233/article/details/105452425
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场