前文链接:https://blog.csdn.net/weixin_41682266/article/details/88850968
前言:
上文中介绍了如何搭建一个简单的微服务应用,可是,一个微服务如果几个人调用那问题不大,如果是几千上万上百万的访问调用呢?那该服务仅仅只有1个服务器那么是承受不住的,这时候我们可以对该服务进行集群搭建以达到分流抗压的目的。下面是对搭建一个简单的Eureka集群的描述:
步骤:
在以上一篇博文的项目为基础,我们开始对server进行改写,搭建一个简单的server集群
1.由于是在单机上搭建集群,所以要修改hosts文件,添加主机名的映射
2.由于要对同一个server启动2次,所以我们要分别创建applicatio-dev.properties和applicatio-prod.properties文件,内容如下:
applicatio-dev.properties
server.port=8761
spring.application.name=first-cloud-server
eureka.instance.hostname=slave1
eureka.client.service-url.defaultZone=http://slave2:8762/eureka
applicatio-prod.properties
server.port=8762
spring.application.name=first-cloud-server
eureka.instance.hostname=slave2
eureka.client.service-url.defaultZone=http://slave1:8761/eureka
然后applicatio.properties文件修改为:
spring.profiles.active=prod
每次我们启动服务器的时候只需要修改applicatio.properties中spring.profiles.active对应的值就可以了
注:因为2个服务器互相注册,所以当启动第一个服务器的时候会进行报错抛出异常,不必理会
结果如图:
3.当2个服务器启动成功后,接着我们要搭建服务发布者了,在client-1的基础上修改其对应的properties文件内容为:
spring.application.name=first-server-provider
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/
然后创建一个person类来保存数据,如下:
public class Person {
private Integer id;
private String name;
int page;
private String message;
public Person(Integer id,String name,int page){
this.id = id;
this.name = name;
this.page = page;
}
}
client-1的controller修改为:
import com.alibaba.fastjson.JSON;
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;
import java.util.HashMap;
import java.util.Map;
@RestController
public class FirstController {
@RequestMapping(value = "/person/user/{name}",method = RequestMethod.GET)
public String findName(@PathVariable("name") String name){
return name;
}
@RequestMapping(value = "/person/{id}",method = RequestMethod.GET)
public String findName(@PathVariable("id") Integer id, HttpServletRequest request){
Person person = new Person(id,"TOM",23);
person.setMessage(request.getRequestURL().toString());
Map<String,String> map = new HashMap<>();
map.put("id",person.getId().toString());
map.put("name",person.getName());
map.put("page", Integer.toString(person.getPage()));
map.put("message",person.getMessage());
return JSON.toJSONString(map);
}
}
为两次启动服务发布者实例,避免端口冲突,增加从控制台读取端口号来启动服务(8081端口号和8085端口号)。所以启动类修改为:
@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
String port = scanner.nextLine();
new SpringApplicationBuilder(DemoApplication.class)
.properties("server.port="+port).run(args);
}
}
然后启动,结果如图:
4.服务发布者client-1写好了,那么在服务调用者client-2项目作一下修改:
application.properties
server.port=9000
spring.application.name=first-server-invoker
eureka.instance.hostname=localhost
eureka.client.service.url.defultZone=http://slave1:8761/eureka/,http://slave2:8762/eureka/
其controller类修改如下:
@RestController
@Configuration
public class InvokerController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@RequestMapping(value = "/router1",method = RequestMethod.GET)
public String router1(){
RestTemplate restTemplate = getRestTemplate();
return restTemplate.getForObject("http://first-server-provider/person/user/TOM",String.class);
}
@RequestMapping(value = "/router2",method = RequestMethod.GET)
public String router2(){
RestTemplate restTemplate = getRestTemplate();
return restTemplate.getForObject("http://first-server-provider/person/1640129406",String.class);
}
}
然后启动,结果如图:
5.写到这里,我们有了服务器,服务发布者和服务调用者。我们还需要验证这个简单的集群是否通过,所以编写一个httpclient来验证一下:
public class FirstCloudRestClient {
public static void main(String[] arg) throws IOException,ClientProtocolException {
CloseableHttpClient client = HttpClients.createDefault();
for(int i=0;i<6;i++){
HttpGet httpGet = new HttpGet("http://localhost:9000/router2");
CloseableHttpResponse response = client.execute(httpGet);
System.out.println("====================");
System.out.println(EntityUtils.toString(response.getEntity(),"utf-8"));
System.out.println("====================");
}
}
结果如下:
通过结果显示,端口8081与8085分别被请求了3次,从这里可以知道了,一个简单的Eureka集群就这样子搭建成功了。
尾言:
一个简单的示例,我们在通过不断的思考,在对其不断的添加内容以后,那么它就变得不简单了。学习与思考是这路上的风景与路途。
下文链接:https://blog.csdn.net/weixin_41682266/article/details/88855058