在Spring Cloud Eureka配置集群中,我们介绍了Eureka集群部署的方式,这篇文章,我们继续编写服务的提供者及服务的消费者。
服务提供者
我们创建microservice-simple-provider-user
工程,作为服务的提供者。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
添加配置文件
spring:
application:
name: microservice-provider-user
eureka:
client:
service-url:
default-zone: http://peer1:8761/eureka,http://peer2:8762/eureka
instance:
prefer-ip-address: true
metadata-map:
my-metadata: 自定义元数据
创建用户实体User.java
@Data
public class User {
private Long id;
private String userName;
private String name;
private Integer age;
private BigDecimal balance;
}
创建服务提供者
@RestController
public class UserController {
@GetMapping("{id}")
public User findById(@PathVariable Long id){
User user = new User();
user.setId(id);
user.setName("zhangsan");
user.setName("张三");
user.setAge(31);
user.setBalance(new BigDecimal(300));
return user;
}
}
服务消费者
我们创建microservice-consumer-movie
工程,作为服务的消费者。
添加依赖
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
添加配置文件
spring:
application:
name: microservice-provider-user
eureka:
client:
service-url:
default-zone: http://peer1:8761/eureka,http://peer2:8762/eureka
instance:
prefer-ip-address: true
metadata-map:
my-metadata: 自定义元数据
我们通过metadata-map
可以自定义元数据。
测试获取服务提供者信息
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* 博客:https://www.xiangcaowuyu.net
* Description:
*
* @Author: 香草物语
* DateTime: 2021-07-17 22:11
*/
@RestController
public class MovieController {
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/user-instance")
public List<ServiceInstance> showInfo(){
return discoveryClient.getInstances("microservice-provider-user");
}
}
我们访问http://localhost:8081/user-instance
,可以获取到服务提供者的详细信息
[{
"scheme": "http",
"host": "192.168.3.48",
"port": 8080,
"metadata": {
"management.port": "8080",
"my-metadata": "自定义元数据"
},
"secure": false,
"uri": "http://192.168.3.48:8080",
"serviceId": "MICROSERVICE-PROVIDER-USER",
"instanceId": "DESKTOP-PPDVS3E:microservice-provider-user",
"instanceInfo": {
"instanceId": "DESKTOP-PPDVS3E:microservice-provider-user",
"app": "MICROSERVICE-PROVIDER-USER",
"appGroupName": null,
"ipAddr": "192.168.3.48",
"sid": "na",
"homePageUrl": "http://192.168.3.48:8080/",
"statusPageUrl": "http://192.168.3.48:8080/actuator/info",
"healthCheckUrl": "http://192.168.3.48:8080/actuator/health",
"secureHealthCheckUrl": null,
"vipAddress": "microservice-provider-user",
"secureVipAddress": "microservice-provider-user",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "192.168.3.48",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 30,
"durationInSecs": 90,
"registrationTimestamp": 1626531693579,
"lastRenewalTimestamp": 1626531693579,
"evictionTimestamp": 0,
"serviceUpTimestamp": 1626531693579
},
"isCoordinatingDiscoveryServer": false,
"metadata": {
"management.port": "8080",
"my-metadata": "自定义元数据"
},
"lastUpdatedTimestamp": 1626531693579,
"lastDirtyTimestamp": 1626531693019,
"actionType": "ADDED",
"asgName": null
}
}]
评论 (0)