整合SpringCloud Alibaba nacos 2.2.1

一、Nacos安装与启停

这个网上一大堆,建议自行参考。

 

二、将应用注册到Nacos上

首先贴一下应用所需要的依赖,不一定非用这些,仅作参考。
 


    <properties>
        <java.version>1.8</java.version>
        <sentinel-core.version>1.7.1</sentinel-core.version>
        <spring-orm.version>4.3.25.RELEASE</spring-orm.version>
        <spring-boot.version>2.2.4.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
        <spring-context-support.version>1.0.6</spring-context-support.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>${sentinel-core.version}</version>
        </dependency>
        <!-- Nacos 组件依赖 discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- Nacos 组件依赖 config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring-context-support.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.6.3</version>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 整合openfeign 时会用到 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

现在开始整合应用

1. 数据服务提供者

配置文件  bootstrap.yml:这里提供者与消费者配置文件都一样,唯一的区别就是端口与 应用名称 不同

server:
  port: 8000    #端口

spring:
  application:
    name: service     #注册到naocos时的应用名
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      config:
        #nacos配置中心地址
        server-addr: 192.168.2.139:8848
        #配置中心创建的配置文件,这里指定引用,多个配置文件用 , 分割
        shared-dataids: public-cinfig-dev.properties,application-db.yml
        refreshable-dataids: public-cinfig-dev.properties,application-db.yml
      discovery:
        #nacos注册中心地址
        server-addr: 192.168.2.139:8848

新建Controller :


@RestController
@RequestMapping(value = "/data", produces = "application/json;charset=UTF-8")
public class TestController {
    @Value("${server.port}")
    private int port;
    @Value("${spring.application.name}")
    private String name;

    @ResponseBody
    @RequestMapping("/test")
    @SentinelResource(value = "collect-extract", blockHandler = "blockHandler")
    public String test() {
        return name + "  " + port;
    }
}

这里只为测试,返回的内容为服务的 端口 以作区别。

Application启动类上添加开启服务注册 的注解:@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class TestApplication {

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

}

2. 消费者

配置文件同提供者一样,只需要修改端口和服务名称即可,这里我们命名为consumer,端口8005

新建测试的Controller以供测试:

@RestController
public class TestController {
    @Resource
    private RemoteDataFeginService feginService;

    @ResponseBody
    @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8")
    @SentinelResource(value = "collect-notice", blockHandler = "blockHandler")
    public String extract() {
        return feginService.test("");
    }
}

新建Fegin:

//这里的name和contextId为服务提供者的服务名称
@FeignClient(name = "service", contextId = "service", fallbackFactory = DataServiceFallbackFactory.class)
public interface RemoteDataFeginService {

    @PostMapping(value = "/data/test")
    String test(@SpringQueryMap String param);
}

新建FallbackFactory


@Component
public class DataServiceFallbackFactory implements FallbackFactory {
    @Override
    public Object create(Throwable throwable) {
        return new RemoteDataFeginService() {

            @Override
            public String test(String param) {
                // TODO Auto-generated method stub
                return throwable.getMessage();
            }
        };
    }
}

 

启动类上添加注解:

//开启服务注册
@EnableDiscoveryClient
//指定RemoteDataFeginService 包的地址
@EnableFeignClients(basePackages = {"com"})
 

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients(basePackages = {"com"})
public class TestApplication {

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

接下来依次启动服务提供者和消费者就可以了,为方便测试,我以不同端口启动了三个 服务提供者,和一个消费者。

 

到这里所有服务都已经注册到Nacos中了,Nacos中配置文件能正常读取并使用。
 访问测试:

每刷新一次都可见返回的数据不同,拿到了三个服务提供者的端口,到这里就基本完成了。

码云项目地址:
https://gitee.com/SGHDXX/springcloud-nacos-test.git

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值