1. Eureka集群
1.1 集群架构
1.2 实现Eureka集群
1.2.1 修改服务器项目
ServerApp:
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); } }
application.yml:
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
这样我们就可以使用控制台输入想要打开的服务器。
运行程序:
输入slave1,回车,即可启动slave1服务器。
这时候会报错,因为我们slave2服务器没有启动,不需要理会,直接再运行一遍程序,在控制台输入slave2:
1.2.2 修改服务提供者项目
PoliceServer:
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); } }
PoliceController:
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; } }
application.yml:
spring: application: name: cloud-police eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
运行项目:
然后再弄个8082。
1.2.3 修改服务调用者项目
application.yml:
server: port: 9000 spring: application: name: cloud-person eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
TestController:
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; } }
运行程序:
刷新网站可以看到切换了服务器: