SpringCloud :
eureka服务应用>>>item-app
建造步骤:
一、POM
(继承了父项目Spring-Cloud-ForLearning中的一个依赖,父项目构造见博文:Spring-Cloud-ForLearning):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<?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">
<parent>
<artifactId>Spring-Cloud-ForLearning</artifactId>
<groupId>com.wx</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-item</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、启动类加注解:@EnableEurekaClient
三、application.yml
server:
port: 8081
#应用名称
spring:
application:
name: app-item
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###因为该应用为服务提供者,是eureka的一个客户端,需要注册到注册中心
register-with-eureka: true
###是否需要从eureka上检索服务
fetch-registry: true
四、项目结构:
五、服务调用
1.不经过eureka
可使用httpClient/resttemplate等等直接调用url( http://localhost:8081/item/1 )获取结果
注:这里的url写活的方法:
1)配在application.yml中
例如:
myspcloud:
item:
url: http://localhost:8081/item/
然后
@Value("${myspcloud.item.url}")
private String itemUrl;
2)配在application.yml中
例如:
myspcloud:
item:
url: http://localhost:8081/item/
然后新建类:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="myspcloud.item")
public class ItemProperties {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
然后 itemProperties.getUrl()
2.使用eureka负载均衡服务 调用
1)在生产Bean: restTemplate的方法上添加注解:
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
2)调用的url及方法就变成了:
public Item queryItemById2(Long id) {
// 该方法走eureka注册中心调用(去注册中心根据app-item(item注册的服务名,在item的application.yml中配置)查找服务,这种方式必须先开启负载均衡@LoadBalanced)
String itemUrl = "http://app-item/item/{id}";
ResponseEntity<Item> forEntity = restTemplate.getForEntity(itemUrl, Item.class,id);
System.out.println("订单系统调用商品服务,result:" + forEntity.getBody().toString());
return forEntity.getBody();
}
截图:
1.服务已注册到eureka
2.eureka服务调用