1、创建Ribbon-Service服务端
引入项目依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
启动类配置:
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonServiceAppRun {
public static void main(String[] args) {
SpringApplication.run(RibbonServiceAppRun.class, args);
}
}
配置文件:
spring:
application:
name: ribbon-service
server:
port: 7003
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/
service-url:
order-service: http://order-service # order-service的调用路径
配置负载均衡:
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
添加一个接口-调用Order-service
@RestController
@RequestMapping("/order")
public class OrderRibbonController {
@Autowired
private RestTemplate restTemplate;
@Value("${service-url.order-service}")
private String orderServiceUrl;
@GetMapping("/{id}")
public CommonResult<Order> getOrder(@PathVariable Long id) {
return restTemplate.getForObject(orderServiceUrl + "/order/{1}", CommonResult.class, id);
}
}
2、创建Order-Service服务
引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<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>
<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>
配置启动类:
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonServiceAppRun {
public static void main(String[] args) {
SpringApplication.run(RibbonServiceAppRun.class, args);
}
}
配置文件:
spring:
application:
name: order-service
server:
port: 7101
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/
启动Order-Service服务,另外通过IDEA的设置,覆盖端口号,直接开启两个服务
同时启动Ribbon-Service服务,一起Eureka-server
此时注册中心页面如下:
3、接口调用
调用两次:http://localhost:7003/order/1
控制台信息如下: