美文网首页springcloud学习笔记
SpringCloud学习五:服务注册以消费测试

SpringCloud学习五:服务注册以消费测试

作者: Bertram_Wang | 来源:发表于2019-03-18 10:36 被阅读0次

前面文章搭建好的服务注册中心使用。
服务注册:
---首先创建一个子项目spring-cloud-netflix-eureka-serverone服务一,添加pom.xml依赖如下:

<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.study.springcloud</groupId>
    <artifactId>spring-cloud-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>study-3-spring-cloud-netflix-eureka-serverone</artifactId>
  <description>服务一</description>
  
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

配置文件拷贝以往的bootstrap.yml文件

spring:
  application:
    name: spring-cloud-netflix-eureka-serverone
  profiles:
    active: dev
  cloud: 
    config:
      uri: http://localhost:8888/config/
      label: master

创建启动类添加注解:@EnableDiscoveryClient示例:

package bertram.springcloud.study;

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

/**
 * <p>启动类<p>
 * @Author Bertram.Wang
 * @Date 2019年3月12日
 */
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

添加测试控制器: HelloController.java 示例:

package bertram.springcloud.study.server.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p> <p>
 * @Author Bertram.Wang
 * @Date 2019年3月15日
 */
@RestController
public class HelloController {
    private static final Logger log = LoggerFactory.getLogger(HelloController.class);
    @GetMapping("/hello")
    public String hello() {
        log.info("========:server one 执行hello===========");
        return "hello server one";
    }
}

一个简单的请求返回Json.
启动配置中心和注册中心。


注册服务

已经注册成功了。

使用服务:
创建子项目:spring-cloud-api项目;pom.xml文件示例:

<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.study.springcloud</groupId>
        <artifactId>spring-cloud-study</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>study-0-spring-cloud-api</artifactId>
    <description>API 入口</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- 发现服务列表并使用带有客户端负载均衡功效 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        

    </dependencies>

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

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

工件: <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> 负载均衡器,默认的策略是轮询

添加配置文件:

spring:
    application:
      name: spring-cloud-api
    cloud: 
      config:
        uri: http://localhost:8888/config/
        label: master

添加启动类示例:

package bertram.springcloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * <p>启动类<p>
 * @Author Bertram.Wang
 * @Date 2019年3月12日
 */
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    // 标识restTemolate拥有负载均衡的能力
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

创建测试控制器:

package bertram.springcloud.study.api.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * <p> <p>
 * @Author Bertram.Wang
 * @Date 2019年3月15日
 */
@RestController
public class HelloController {
    private static final Logger log = LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/hello")
    public String hello() {
        log.info("========:api 执行hello===========");
        return restTemplate.getForEntity("http://SPRING-CLOUD-NETFLIX-EUREKA-SERVERONE/serverone/hello", String.class).getBody();
    }
}

链接地址写服务名:SPRING-CLOUD-NETFLIX-EUREKA-SERVERONE全部大写。
(重复启动服务一更改端口号:20003)
启动项目测试:


效果图

每次点击重新加载会不同访问服务20002,20003端口服务。说明负载均衡生效了。

相关文章

网友评论

    本文标题:SpringCloud学习五:服务注册以消费测试

    本文链接:https://www.haomeiwen.com/subject/nebmmqtx.html