SpringCloud之Eureka叢集搭建例項
阿新 • • 發佈:2019-01-29
一、說明
Eureka提供基於REST的服務,在叢集中主要用於服務管理。
這裡將會執行二個伺服器例項、二個伺服器提供者例項、然後配合服務呼叫請求服務,進行例項測試。
環境(Win+idea)
目錄結構:
二、Eureka服務端(first-cloud-server)
1、修改Windows上的host檔案
新增內容:
127.0.0.1 cloud1
127.0.0.1 cloud2
2、配置檔案application-cloud1.yml
server: port: 8010 spring: application: name: first-cloud-server profiles: cloud1 eureka: instance: hostname: cloud1 client: serviceUrl: defaultZone: http://cloud2:8020/eureka/
3、配置檔案application-cloud2.yml
server:
port: 8020
spring:
application:
name: first-cloud-server
profiles: cloud2
eureka:
instance:
hostname: cloud2
client:
serviceUrl:
defaultZone: http://cloud1:8010/eureka/
4、啟動類FirstCloudServerApplication
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import java.util.Scanner; @EnableEurekaServer @SpringBootApplication public class FirstCloudServerApplication { public static void main(String[] args) { /** * 讀取控制檯輸入,決定使用哪個profile */ Scanner scanner = new Scanner(System.in); String profiles = scanner.nextLine(); new SpringApplicationBuilder(FirstCloudServerApplication.class).profiles(profiles).run(args); // SpringApplication.run(FirstCloudServerApplication.class, args); } }
5、配置Idea的啟動項
6、啟動服務
先啟動一個輸入:cloud1、再次啟動服務輸入:cloud2。
先啟動的服務控制檯可能會丟擲異常,暫時不必理會。
7、eureka控制檯資訊
三、Eureka服務提供者(first-cloud-provider)
1.配置檔案application.yml
spring: application: name: first-cloud-provider eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8010/eureka/,http://localhost:8020/eureka/
2.啟動類FirstCloudProviderApplication
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import java.util.Scanner;
@EnableEurekaClient
@SpringBootApplication
public class FirstCloudProviderApplication {
public static void main(String[] args) {
/**
* 讀取控制檯輸入的埠
*/
Scanner scanner = new Scanner(System.in);
String port = scanner.nextLine();
new SpringApplicationBuilder(FirstCloudProviderApplication.class).properties("server.port=" + port).run(args);
}
}
3、功能類FirstController、Person
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;
import javax.servlet.http.HttpServletRequest;
@RestController
public class FirstController {
@RequestMapping(value = "/person/{pId}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Person findPerson(@PathVariable("pId") Integer pId, HttpServletRequest request) {
Person person = new Person(pId, "Lionel Messi", 18);
person.setMessage(request.getRequestURL().toString());
return person;
}
}
public class Person {
private Integer id;
private String name;
private Integer age;
private String message;
public Person() {
super();
}
public Person(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Person(Integer id, String name, Integer age, String message) {
super();
this.id = id;
this.name = name;
this.age = age;
this.message = message;
}
//略
}
4、啟動專案(8081、8082埠)
5、檢視控制檯
四、Eureka服務呼叫者(first-cloud-invoker)
1、配置檔案application.yml
server:
port: 9000
spring:
application:
name: first-cloud-invoker
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://slave1:8010/eureka/,http://slave2:8020/eureka/
2、啟動類FirstCloudInvokerApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class FirstCloudInvokerApplication {
public static void main(String[] args) {
SpringApplication.run(FirstCloudInvokerApplication.class, args);
}
}
3、功能類InvokerController
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Configuration
public class InvokerController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@RequestMapping(value = "/router", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public String router() {
RestTemplate restTpl = getRestTemplate();
// 根據應用名稱呼叫服務
return restTpl.getForObject("http://first-cloud-provider/person/1", String.class);
}
}
4、啟動呼叫者、控制檯資訊
五、Eureka測試(first-cloud-rest-client)
1、pom.xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
2、測試類TestHttpClient
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestHttpClient {
private static Logger logger = LoggerFactory.getLogger(TestHttpClient.class);
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);
// 根據 響應解析出字串
logger.info(EntityUtils.toString(response.getEntity()));
}
}
}
3、測試結果
17:39:53.923 [main] INFO com.springcloud.firstcloudrestclient.TestHttpClient - {"id":1,"name":"Lionel Messi","age":18,"message":"http://localhost:8082/person/1"}
17:39:54.031 [main] INFO com.springcloud.firstcloudrestclient.TestHttpClient - {"id":1,"name":"Lionel Messi","age":18,"message":"http://localhost:8081/person/1"}
17:39:54.043 [main] INFO com.springcloud.firstcloudrestclient.TestHttpClient - {"id":1,"name":"Lionel Messi","age":18,"message":"http://localhost:8082/person/1"}
17:39:54.053 [main] INFO com.springcloud.firstcloudrestclient.TestHttpClient - {"id":1,"name":"Lionel Messi","age":18,"message":"http://localhost:8081/person/1"}
17:39:54.062 [main] INFO com.springcloud.firstcloudrestclient.TestHttpClient - {"id":1,"name":"Lionel Messi","age":18,"message":"http://localhost:8082/person/1"}
17:39:54.070 [main] INFO com.springcloud.firstcloudrestclient.TestHttpClient - {"id":1,"name":"Lionel Messi","age":18,"message":"http://localhost:8081/person/1"}
根據結果可知,8081、8082埠分別被請求了3次。