搭建Eureka集群并测试

一 集群架构图
搭建Eureka集群并测试_第1张图片
二 创建各个模块
创建服务器
创建服务提供者
创建服务调用者
三 修改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
搭建Eureka集群并测试_第2张图片
五 新建项目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 测试
通过控制台启动两个实例
搭建Eureka集群并测试_第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 测试
搭建Eureka集群并测试_第4张图片
六 新建项目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 测试轮询
搭建Eureka集群并测试_第5张图片
七 新cloud-client项目
1 依赖查找方法:
http://repo1.maven.org/maven2/org/apache/httpcomponents/httpclient/4.5.2/ httpclient-4.5.2.pom
2 新增依赖

      4.0.0
      org.crazyit.cloud
      cloud-client
      0.0.1-SNAPSHOT
      
      
            
                  org.apache.httpcomponents
                  httpclient
                  4.5.2
            
      
      
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"}






你可能感兴趣的:(微服务)