Spring Cloud Alibaba 创建服务消费者

项目创建

服务消费者的创建其实和服务提供者的创建大同小异,这里先用最原始的LoadBalanceClientRestTemplate结合的方式来访问

  1. 创建一个名为idreamyou4cloud-auth的模块,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">
    <parent>
        <artifactId>idreamyou4cloud</artifactId>
        <groupId>cn.idreamyou</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>idreamyou4cloud-auth</artifactId>
    <packaging>jar</packaging>

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

        <!--注册中心客户端-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. 编写SpringBoot启动类
package cn.idreamyou.idreamyou4cloud.auth;

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

/**
 * 认证授权中心
 *
 * @author juning
 * @date 2020/9/14
 */
@SpringBootApplication
@EnableDiscoveryClient
public class AuthApplication {

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

}
  1. 编写RestTemplate配置
package cn.idreamyou.idreamyou4cloud.auth.config;

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

/**
 * RestTemplate配置类
 *
 * @author juning
 * @date 2020/9/14
 */
@Configuration
public class NacosConsumerConfiguration {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
  1. 编写控制器controller
package cn.idreamyou.idreamyou4cloud.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 服务消费者
 *
 * @author juning
 * @date 2020/9/14
 */
@RestController
public class NacosConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${spring.application.name}")
    private String appName;

    @GetMapping(value = "/test/app/name")
    public String test() {
        //使用 LoadBalanceClient 和 RestTemplate 结合的方式来访问
        ServiceInstance serviceInstance = loadBalancerClient.choose("idreamyou4cloud-upms");
        String url = String.format("http://%s:%s/test/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
        return restTemplate.getForObject(url, String.class);
    }
}
  1. 编写application.yml配置文件
server:
  # 服务端口
  port: 8082

spring:
  application:
    # 应用名称,这里采用Maven项目的模块名
    name: @artifactId@
  cloud:
    nacos:
      # 注册中心nacos的地址
      server-addr: localhost:8848

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

整体项目结构如下:

image.png

启动工程

浏览器访问http://localhost:8848/nacos,可以发现auth已经出现在nacos的服务注册列表下:

image.png

浏览器访问http://localhost:8082/test/app/name,你会看到Hello Nacos Discovery idreamyou4cloud-auth i am from port 8081

image.png

上一篇 下一篇