SpringCloudAlibaba完整章节四(创建服务消费者)

创建一个工程名为 spring-cloud-alibaba-nacos-consumer 的服务消费者项目,

pom.xml 配置如下:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.wsl</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../spring-cloud-alibaba-dependencies/pom.xml</relativePath>
    </parent>
    <artifactId>spring-cloud-alibaba-consumer-feign</artifactId>
    <packaging>jar</packaging>

    <name>spring-cloud-alibaba-consumer-feign</name>
    <description>Demo project for Spring Cloud Alibaba</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <!-- sentinel持久化 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.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>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.wsl.consumer.ConsumerFeignApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Application
加上注释@EnableDiscoveryClient ,将服务注册到nacos上去

package com.lb.nacosconsumer;

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

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

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

}

Configuration

创建一个名为 NacosConsumerConfiguration 的 Java 配置类,
主要作用是为了注入 RestTemplate

package com.lb.nacosconsumer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @program: springcloud-alibaba
 * @description: RestTemplate的配置类
 * @author: lb
 * @create: 2021-07-03 10:48
 **/
@Configuration
public class NacosConsumerConfiguration {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

application.yml

server:
  port: 9091

spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

management:
  endpoints:
    web:
      exposure:
        include: "*"

在这里插入图片描述
启动工程

通过浏览器访问 http://localhost:8848/nacos,即 Nacos Server 网址

在这里插入图片描述
你会发现多了一个名为 nacos-consumer 的服务
这时打开 http://localhost:9091/test/app/name ,你会在浏览器上看到:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了搭建一个完整的Spring Cloud Alibaba项目,你需要按照以下步骤进行操作: 1. 创建Spring Boot项目: - 使用Spring Initializr或者Maven手动创建一个新的Spring Boot项目。 2. 添加依赖: - 在项目的pom.xml文件中,添加Spring Cloud Alibaba的相关依赖,包括spring-cloud-starter-alibaba-nacos-config、spring-cloud-starter-alibaba-nacos-discovery、spring-cloud-starter-alibaba-sentinel等。 3. 配置Nacos注册中心: - 在application.properties或者application.yml文件中,配置Nacos注册中心的地址和端口。 4. 编写服务提供者: - 创建一个服务提供者类,使用@EnableDiscoveryClient注解启用服务发现功能,并使用@RestController注解定义一个简单的REST接口。 5. 编写服务消费者: - 创建一个服务消费者类,使用@LoadBalanced注解实现负载均衡,并通过RestTemplate调用服务提供者的接口。 6. 配置服务降级和限流: - 使用Sentinel进行服务降级和限流的配置。可以在配置文件中定义Sentinel规则,或者通过代码方式进行动态规则配置。 7. 配置服务网关: - 使用Spring Cloud Gateway或者Nacos Gateway配置服务网关,实现请求路由、访问控制等功能。 8. 部署和运行: - 打包应用程序,并部署到服务器上运行。 这些步骤涵盖了Spring Cloud Alibaba项目的基本搭建过程。根据具体需求,你可能还需要进行更多的配置和开发工作。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值