javascript
SpringCloud学习之运行第一个Eureka程序
場景
關于Eureka
1.提供了Eureka服務端與客戶端。
2.主要用于服務管理。
Eureka架構
構建第一個應用
1.建立服務器端。
2.建立服務提供者。
3.建立服務調用者。
實現
建立服務器端
打開eclipse,新建Maven Project--create a simple project
打開pom.xml,添加依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.badao.cloud</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><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> </project>新建啟動類
代碼:
package com.badao.cloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication @EnableEurekaServer public class ServerApplication {public static void main(String[] args) throws Exception {SpringApplication.run(ServerApplication.class, args);}}注:
@EnableEurekaServer注解是配置為服務端
在resource下新建配置文件application.yml
修改端口號并使其注冊為服務器端。
server:port: 8761 eureka:client:register-with-eureka: falsefetch-registry: false運行啟動類效果:
建立服務提供者
打開eclipse,新建Maven Project--create a simple project
打開pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bao.cloud</groupId><artifactId>eureka-provide</artifactId><version>0.0.1-SNAPSHOT</version><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> </project>新建啟動類
package com.badao.cloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class ProvideApplication {public static void main(String[] args) throws Exception {SpringApplication.run(ProvideApplication.class, args);}}
新建實體類
新建Controller
運行啟動類,默認8080端口,打開瀏覽器輸入:
http://localhost:8080/call/1
?
將服務提供者注冊到服務器端
打開服務提供者的啟動類添加注解:
@EnableEurekaClient
完整代碼:
package com.badao.cloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication @EnableEurekaClient public class ProvideApplication {public static void main(String[] args) throws Exception {SpringApplication.run(ProvideApplication.class, args);}}在服務提供者項目下新建配置文件application.yml
spring:application:name: eureka-provide eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/?重新運行服務提供者的啟動類和服務端的啟動類
查看服務器端,已經成功注冊:
新建服務調用者
打開eclipse,新建Maven Project--create a simple project
?
打開項目的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.badao.cloud</groupId><artifactId>eureka-call</artifactId><version>0.0.1-SNAPSHOT</version><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></project>新建啟動類
package com.badao.cloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication @EnableEurekaClient public class CallApplication {public static void main(String[] args) throws Exception {SpringApplication.run(CallApplication.class, args);}}新建Controller
package com.badao.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@LoadBalancedpublic RestTemplate getRestTemplate() {return new RestTemplate();}@GetMapping("/router")@ResponseBodypublic String router() {RestTemplate tpl = getRestTemplate();String json = tpl.getForObject("http://eureka-provide/call/1", String.class);return json;}}新建配置文件application.yml
server:port: 8081 spring:application:name: eureka-call eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/打開瀏覽器輸入:
http://localhost:8081/router
源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11206065
總結
以上是生活随笔為你收集整理的SpringCloud学习之运行第一个Eureka程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eureka服务器端启动时报错:Conn
- 下一篇: Kindeditor中上传本地照片后需要