springCloud Alibaba-Nacos 初探

一.使用Nacos代替Eureka作为注册服务中心

 

1.从github下载Nacos压缩包,在本地解压

2.搭建maven聚合工程

3.将nacos作为一个子模块放入其中,并进入bin目录进行启动,启动前需要改几个配置

#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
 spring.datasource.platform=mysql

### Count of DB:
 db.num=1

### Connect URL of DB:
 db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
 db.user=root
 db.password=123

4.执行nacos-mysql.sql脚本,再进行启动

5.打开页面 localhost:8848/nacos  进行访问,账号/密码   nacos/nacos

 

二.父工程引入相关依赖

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>
    ​
    <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>

三.创建服务调用方、提供方 子模块

3.1调用方 启动类

package com.wang.nacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**  客户端服务:调用方
 * @author Wang
 * @description
 * @create 2020-08-02 11:33
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.wang.nacos.feign")
public class NacosClientApp {

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

}

3.2 yml配置

spring:
  application:
    # 服务名
    name: nacos-client
  cloud:
    nacos:
      discovery:
        #必须配置ip地址
        server-addr: localhost:8848
  datasource:
    username: root
    password: 123
    url: jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC

server:
  port: 8090

3.3创建feign接口 ,注解中的值指向服务调用方的应用名称

package com.wang.nacos.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Wang
 * @description
 * @create 2020-08-02 14:54
 */

@FeignClient("nacos-service")
public interface ClientFeignService {


    @RequestMapping("/nacos/service/getService")
    public Object getService();

}

3.4提供方 子模块

package com.wang.nacos;

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

/**
 * @author Wang
 * @description
 * @create 2020-08-02 11:59
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosServiceApp {

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

3.5yml配置文件

spring:
  application:
    # 服务名
    name: nacos-service
  cloud:
    nacos:
      discovery:
        #必须配置ip地址
        server-addr: localhost:8848
  datasource:
    username: root
    password: 123
    url: jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC

server:
  port: 8091

3.6 提供方 controller层

package com.wang.nacos.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Wang
 * @description
 * @create 2020-08-02 14:55
 */

@RefreshScope
@RestController
@RequestMapping("/nacos/service")
public class NacosServiceController {

    @Value("${client.name}")
    private String name;
    @Value("${client.age}")
    private String age;

    @RequestMapping("/getService")
    public Object getService(){
        List<String> list = new ArrayList<>();
        list.add("www.itdream.site");
        list.add("www.baidu.com");
        list.add(name);
        list.add(age);
        return list;
    }
}

=============================

当微服务启动,就可以将服务注册到注册中心,这样调用方 便可以通过 feign 从注册中心调用 提供方服务。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁漂打工仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值