搭建Eureka集群并测试

一 集群架构图
二 创建各个模块
创建服务器
创建服务提供者
创建服务调用者
三 修改C:\Windows\System32\drivers\etc\hosts文件
127.0.0.1 slave1 slave2
四 新建项目cloud-114
1 编写配置文件
server:
  port: 8761
spring:
  application:
    name: cloud-114
  profiles: slave1
eureka:
  client:
    serviceUrl:
      defaultZone: http://slave2:8762/eureka
---
server:
  port: 8762
spring:
  application:
    name: cloud-114
  profiles: slave2
eureka:
  client:
    serviceUrl:
      defaultZone: http://slave1:8761/eureka
2 编写启动类
package org.crazyit.cloud;

import java.util.Scanner;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ServerApp {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String profiles = scan.nextLine();
        new SpringApplicationBuilder(ServerApp.class).profiles(profiles).run(args);
    }

}
3 测试
控制台输入启动两个Eureka Server
五 新建项目cloud-police
1 编写配置文件
spring:
  application:
    name: cloud-police
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
2 编写启动类
package org.crazyit.cloud;

import java.util.Scanner;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class PoliceServer {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //通过控制台读取
        String port = scan.nextLine();
        new SpringApplicationBuilder(PoliceServer.class).properties("server.port=" + port).run(args);
    }

}
3 测试
通过控制台启动两个实例
4 编写实体类
package org.crazyit.cloud;
public class Police {
      private Integer id;
      private String name;
      private String message;
      
      public Integer getId() {
            return id;
      }
      public void setId(Integer id) {
            this.id = id;
      }
      public String getName() {
            return name;
      }
      public void setName(String name) {
            this.name = name;
      }
      public String getMessage() {
            return message;
      }
      public void setMessage(String message) {
            this.message = message;
      }
      
} 
5 编写控制类
package org.crazyit.cloud;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PoliceController {

    @RequestMapping(value = "/call/{id}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Police call(@PathVariable Integer id, HttpServletRequest request) {
        Police p = new Police();
        p.setId(id);
        p.setName("angus");
        p.setMessage(request.getRequestURL().toString());
        return p;
    }
}
6 测试
六 新建项目cloud-person
1 编写配置文件
server:
  port: 9000
spring:
  application:
    name: cloud-person
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
2 编写控制器
package org.crazyit.cloud;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Configuration
public class TestController {
    
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @GetMapping("/router")
    @ResponseBody
    public String router() {
        RestTemplate tpl = getRestTemplate();
        String json = tpl.getForObject("http://cloud-police/call/1", String.class);
        return json;
    }

}
3 测试轮询
七 新cloud-client项目
1 依赖查找方法:
2 新增依赖
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.crazyit.cloud</groupId>
      <artifactId>cloud-client</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <dependencies>
            <dependency>
                  <groupId>org.apache.httpcomponents</groupId>
                  <artifactId>httpclient</artifactId>
                  <version>4.5.2</version>
            </dependency>
      </dependencies>
      
</project>
3 新增测试类
package org.crazyit.cloud;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class TestClient {
      public static void main(String[] args) throws Exception {
            // 创建默认的HttpClient
            CloseableHttpClient httpclient = HttpClients.createDefault();
            // 调用6次服务并输出结果
            for(int i = 0; i < 6; i++) {
                  // 调用 GET 方法请求服务
                  HttpGet httpget = new HttpGet("http://localhost:9000/router");
                  // 获取响应
                  HttpResponse response = httpclient.execute(httpget);
                  // 根据 响应解析出字符串
                  System.out.println(EntityUtils.toString(response.getEntity()));
            }
      }
}
4 运行测试,观察负载均衡效果
{"id":1,"name":"angus","message":"http://DESKTOP-5SDKDG4:8081/call/1"}
{"id":1,"name":"angus","message":"http://DESKTOP-5SDKDG4:8082/call/1"}
{"id":1,"name":"angus","message":"http://DESKTOP-5SDKDG4:8081/call/1"}
{"id":1,"name":"angus","message":"http://DESKTOP-5SDKDG4:8082/call/1"}
{"id":1,"name":"angus","message":"http://DESKTOP-5SDKDG4:8081/call/1"}
{"id":1,"name":"angus","message":"http://DESKTOP-5SDKDG4:8082/call/1"}






猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/81050852