开发工具idea,开发技术spring cloud
我的学习借鉴了 https://blog.csdn.net/sky786905664/article/details/78995068
作者 Sky786905664 的一些文章
新建工程module:service-ribbon
依赖如下:
<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dream.cloud</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-ribbon</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</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-ribbon</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
配置文件如下:
server.port=8084
spring.application.name=service-ribbon
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
在启动类上加上 @EnableDiscoveryClient 和 @EnableEurekaClient 注解
然后开始负载均衡:
启动类如下
@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
// 开启负载均衡
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
新建controller
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name) {
return helloService.hiService(name);
}
}
新建service
public interface HelloService {
public String hiService(String name);
}
实现service
@Service
public class HelloServiceImpl implements HelloService {
@Autowired
RestTemplate restTemplate;
@Override
public String hiService(String name) {
return restTemplate.getForObject("http://eurekaclient/index/hi?name=" + name, String.class);
}
}
启动注册中心全部eureka-server 和 eureka-client (eureka-client启动两个才能看出负载均衡)
访问地址:
http://localhost:8084/hi?name=forezp
可以看出服务向两个eureka-client 分别请求,实现了负载均衡,然后也对服务进行了消费
码云地址:https://gitee.com/hjc2/springcloud.git