Eureka (ribbon负载均衡)
1.创建springboot项目Eureka注册中心(server)
1.导入相关组件
<?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 http://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>1.5.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xing</groupId>
<artifactId>Spring_Boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring_Boot-2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
2.编写yml配置文件
server:
port: 8001
eureka:
instance:
hostname: eurakaServer
client:
register-with-eureka: false
fetch-registry: false
service-url:
{defaultZone : "http://localhost:8001/eureka/" }
3.注解开启Eureka
package com.xing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//开启Eureka Server
public class SpringBoot2Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot2Application.class, args);
}
}
4.启动注册中心
2.创建springboot项目Eureka生产者(provider)
注:可有多个端口不同的生产者应用程序,注册到同一个注册中心
1.导入相关组件
<?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 http://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>1.5.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xing</groupId>
<artifactId>spring-eureka-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-eureka-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR5</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.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>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.编写yml配置文件
server:
port: 8082
spring:
application:
name: provider
eureka:
client:
service-url: { defaultZone : "http://localhost:8001/eureka/" }
3.编写简单测试类
//为方便测试我将测试代码写在主启动类
package com.xing;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient //启动EnableEureka客户端
public class SpringEurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEurekaProviderApplication.class, args);
}
@Value("${server.port}")
private String port;
@Value("${spring.application.name}")
private String name1;
@RequestMapping("/hello")
public String hello(@RequestParam(name="name") String name) {
return "hello"+name+"name:"+name1+"port:"+port;
}
}
4.启动生产者
2.创建springboot项目Eureka消费者(customer)
1.导入相关组件
<?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 http://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>1.5.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xing</groupId>
<artifactId>spring-eureka-customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-eureka-customer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR5</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.编写yml配置文件
server:
port: 8002
spring:
application:
name: customer
eureka:
client:
service-url:
{defaultZone : "http://localhost:8001/eureka/" }
3.编写简单测试类
//为方便测试我将测试代码写在主启动类
package com.xing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class SpringEurekaCustomerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEurekaCustomerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@GetMapping("hello/{name}")
public String hello(@PathVariable("name")String name) {
String result= restTemplate.getForObject("http://provider/hello?name="+name, String.class);
return result;
}
}
4.启动消费者,访问/hello
实现负载均衡
Eureka (feign负载均衡)
1.创建springboot项目Eureka消费者(customer-feign)
2.编写yml配置文件
server:
port: 7750
spring:
application:
name: customerfeign
eureka:
client:
service-url: {defaultZone : "http://localhost:8001/eureka/" }
3.编写简单的测试类
//为方便测试我将测试代码写在主启动类
package com.xing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient//开启EnableEureka客户端
@EnableFeignClients//开启Feign相关功能
@RestController
public class SpringEurekaCustomerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEurekaCustomerFeignApplication.class, args);
}
//编写生产者接口
@FeignClient("provider")//生产者yml配置的服务名称
public interface helloService{
//接口方法与生产者实现保持一致
@RequestMapping("/hello")
String hello(@RequestParam(name="name")String name);
}
//注入
@Autowired
helloService service;
//调用接口
@GetMapping("/nihao/{name}")
public String hello(@PathVariable("name")String name) {
return service.hello(name);
}
}
4.启动消费者服务程序测试
通过feign实现负载均衡