1. Eureka
1.1 關于Eureka
為springcloud提供了Eureka服務端和客戶端,主要用于服務管理,
Eureka架構:

1.2 搭建Eureka服務器
創建一個專案ServerDemo:
需要匯入的jar包:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
分別是springcloud和eureka服務端的依賴包,
在resources包下創建application.yml組態檔:
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false
禁止自己當做服務注冊
register-with-eureka: false
屏蔽注冊資訊
fetch-registry: false
在java包下創建ServerApp.java:
package org.crazyit.cloud; 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) { new SpringApplicationBuilder(ServerApp.class).web(true).run(args); } }
@EnableEurekaServer:將專案作為SpringCloud中的注冊中心,
運行專案:

這樣我們服務器就搭建好了,
1.3 建立服務提供者
創建專案ServerPolice,
匯入jar包:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
在resources包下創建application.yml組態檔:
spring: application: name: first-police eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
創建PoliceServer.java檔案:
package org.crazyit.cloud; 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) { new SpringApplicationBuilder(PoliceServer.class).web(true).run(args); } }
@EnableEurekaClient:與服務器配合向外提供注冊與發現服務介面,
創建物體類Police.java:
package org.crazyit.cloud; public class Police { private Integer id; private String name; 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; } }
創建PoliceController.java:
package org.crazyit.cloud; 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) { Police p = new Police(); p.setId(id); p.setName("angus"); return p; } }
運行程式:

可以看到多了一個叫first-police的服務,
也可以直接呼叫這個服務:

1.4 建立服務呼叫者
創建專案PersonClient
匯入jar包:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
組態檔application.yml:
server: port: 8081 spring: application: name: first-person eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
創建PersonServer.java:
package org.crazyit.cloud; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class PersonServer { public static void main(String[] args) { new SpringApplicationBuilder(PersonServer.class).web(true).run(args); } }
創建TestController.java:
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://first-police/call/1", String.class); return json; } }
運行程式:


最終的程式結構:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/192823.html
標籤:Java
