consumer
服务消费者
RestTemplate
在Spring
中,提供了RestTemplate
。RestTemplate
是Spring
提供的用于访问Rest服务的客户端。而在SpringCloud
中也是使用此服务进行服务调用的。
创建bill-service项目
新的模块初始化三部曲:
- pom.xml
- 启动类
- 配置文件
pom.xml
添加如下内容:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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 https://maven.apache.org/xsd/maven-4.0.0.xsd"\>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<relativePath/> \<!-- lookup parent from repository --\>
</parent>
<groupId>com.luffy</groupId>
<artifactId>bill-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bill-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
BillServiceApplication
package com.luffy.billservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class BillServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BillService.class, args);
}
}
application.yml
server:
port: 7001
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_SERVER:http://admin:admin@localhost:8761/eureka/}
instance:
instance-id: ${eureka.instance.hostname}:${server.port}
prefer-ip-address: true
hostname: ${INSTANCE_HOSTNAME:bill-service}
#eureka客户端需要多长时间发送心跳给eureka服务器,表明他仍然或者,默认30秒
lease-renewal-interval-in-seconds: 2
#eureka服务器在接受到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除
lease-expiration-duration-in-seconds: 2
spring:
application:
name: bill-service
BillController
package com.luffy.billservice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class BillController {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
@GetMapping("/bill/user")
public String getUserInfo(){
return restTemplate.getForObject("http://localhost:7000/user", String.class);
}
}
问题:
- 服务调用采用指定IP+Port方式,注册中心未使用
- 多个服务负载均衡