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; } }
運行程式:

重繪網站可以看到切換了服務器:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/192847.html
標籤:其他
上一篇:類中屬性和方法的訪問限制機制
