1.创建module
修改配置文件类型
spring:
application:
name: service-ribbon
server:
port: 9003
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8889/eureka/
pom.xml文件
<?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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qdzwq</groupId>
<artifactId>ribbonservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ribbonservice</name>
<description>ribbonservice</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</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-actuator</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建启动类
package com.qdzwq.ribbonservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonserviceApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonserviceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
创建一个两个client、 ,通过spring.application.name 的项目名称来设置消费的服务提供者;在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名(eureka的两个客户端,可以参考我的eureka客户端配置文章)
package com.qdzwq.ribbonservice.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RibbonService {
@Autowired
private RestTemplate restTemplate;
public String helloRibbon(String name) {
return restTemplate.getForObject("http://client/?name=" + name, String.class);
}
}
创建controller
package com.qdzwq.ribbonservice.Controller;
import com.qdzwq.ribbonservice.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RibbonController {
@Autowired
private RibbonService ribbonService;
@RequestMapping("/helloRibbon")
public String helloRibbon(String name){
return ribbonService.helloRibbon(name);
}
}
启动服务,访问注册中心
http://127.0.0.1:9003/helloRibbon
访问上面的链接
有问题,请联系我13522561676