背景:spring boot主張微服務,所以這里不得不提出服務之間的調用
這次我們使用srping cloud作為服務集成與管理者
(轉載請注明來源:cnblogs coder-fang)
- 根據實踐1,創建一個spring boot 項目(springServer),作為服務中心:
- springServer的pom文件如下:
<?xml version="1.0" encoding="UTF-8"?> <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.test</groupId> <artifactId>springbootService</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springmvcTest-1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
- springServer的application.properties配置如下:
server.port=8000 eureka.instance.hostname=0.0.0.0 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
設置服務中心的端口為8000,eureka.client.register-with-eureka=false,eureka.client.fetch-registry=false,這兩個是禁止自己注冊到服務中心,
eureka.client.serviceUrl.defaultZone 這個參數是指定客服端調用的服務地址。
- 在ServiceApplication中使用@EnableEurekaServer啟用服務中心:
package com.springdemo; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class ServiceApplication { public static void main(String[] args) { new SpringApplicationBuilder(ServiceApplication.class).run(args); } }
-
運行Spring server,在瀏覽器輸入 http://localhost:8000/ 進入服務中心頁面,此時頁面中application為空,暫時無服務注冊:
- 修改springmvcTest項目,即實踐1中創建的項目,使其支持spring cloud eureka。
- pom中加入依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- application.properties中加入
#for eureka spring.application.name=db-service eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
eureka.client.serviceUrl.defaultZone是上述spring server項目的地址,
spring.application.name 是本項目在服務中心的名稱,用於其它服務訪問本服務
- 在SpringmvcTestApplication中加入注解@EnableDiscoveryClient 將自己注冊到服務中心:
@EnableDiscoveryClient @SpringBootApplication @ServletComponentScan public class SpringmvcTestApplication { public static void main(String[] args) { SpringApplication.run(SpringmvcTestApplication.class, args); } }
- 運行springmvcTest ,觀察服務注冊中心頁面,此時application中已有我們注冊的服務:
- 此時 springmvcTest中的restful接口,已可以被服務中心的其它服務調用。
- pom中加入依賴:
- 創建服務消費者,使其能夠調用springmvcTest項目中的restful服務:
- 根據實踐1 方式創建項目(springClient),pom文件如下:
<?xml version="1.0" encoding="UTF-8"?> <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.test</groupId> <artifactId>springClient</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springmvcTest-1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Eureka --> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
- application.properties中加入:
spring.application.name=client eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
- 項目入口application代碼如下:
package com.test.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class SpringBootClientApp { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { ConfigurableApplicationContext ctx = SpringApplication.run(SpringBootClientApp.class, args); RestTemplate restTemp = ctx.getBean(RestTemplate.class); String val=restTemp.getForEntity("http://db-service/user/zh", String.class).getBody(); System.out.println("getVal="+val); } }
首先注入一個 restTemplate用來調用遠程rest服務
- 運行client,且關鍵輸出如下:
- 根據實踐1 方式創建項目(springClient),pom文件如下:
到這里遠程服務調用已基本完成