eureka服务提供和调用
一.项目描述(非负载均衡)
案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是我们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行
1.Eureka服务注册中心
(1)pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!--注意:这里必须要添加,否则各种依赖有问题-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>(2)application.yml
spring:
application:
name: EurekaServer
server:
port: 5555
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://127.0.0.1:5555/eureka/(3) main方法启动
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerRun {
public static void main(String[] args) {
SpringApplication.run(EurekaServerRun.class, args);
}
}2.生产者 Cloud-producer
(1)pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!--注意:这里必须要添加,否则各种依赖有问题-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>(2)application.yml
server:
port: 8001
context-path: /
spring:
application:
name: producer
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:5555/eureka/(3) main方法启动
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerRun {
public static void main(String[] args) {
SpringApplication.run(ProducerRun.class, args);
}
}(4)Controller
@RestController
public class TestHello {
@RequestMapping("/hello")
public String testHello(@RequestParam String param){
return "hello "+param+" ,this is Producer1 message";
}
}3.消费者 Cloud-consumer
(1)pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId> org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!--注意:这里必须要添加,否则各种依赖有问题-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>(2)application.yml
server:
port: 8002
spring:
application:
name: Consumer
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:5555/eureka/ (3)main方法启动
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
/**
* @EnableDiscoveryClient :启用服务注册与发现
* @EnableFeignClients:启用feign进行远程调用
* Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单,
* 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。
* Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。
* Feign可以与Eureka和Ribbon组合使用以支持负载均衡
* @author cw
*
*/
public class ConsumerRun {
public static void main(String[] args) {
SpringApplication.run(ConsumerRun.class, args);
}(4)remote远程接口
@Component
@FeignClient(name="producer") //name:远程服务名,spring.application.name配置的名称
public interface HelloRemote {
@RequestMapping(value="/hello")
public String testHello(@RequestParam(value="param") String param);
//注意: 此方法和远程服务的contoller中的方法名和参数需保持一致。
}(5)Controller
@RestController
public class TestHello {
@Autowired
HelloRemote helloRemote;
@RequestMapping("/hello/{param}")
public String index(@PathVariable("param") String param){
return helloRemote.testHello(param);
}
}调试说明
1.依次启动Cloud-EurekaServer、Cloud-Producer、Cloud-Consumer三个项目
2.先输入:http://127.0.0.1:5555/检查Cloud-EurekaServe注册服务是否正常
再输入:http://192.168.18.106:8001/hello?param=’test’ 检查Cloud-Producer服务是否正常
返回:hello ‘test’ ,this is Producer message
说明Cloud-Producr正常启动,提供的服务也正常。
3.浏览器中输入:http://192.168.18.106:8002/hello/param
返回:hello param ,this is Producer2 message
说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。
二.负载均衡
1.eureka服务注册(同上 一.1)
2.生产者(同上 一.2)
但需要如下修改:Application.yml
server:
port: 8003
context-path: /
spring:
application:
name: producer
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:5555/eureka/需要如下修改:Controller
@RestController
public class TestHello {
@RequestMapping("/hello")
public String testHello(@RequestParam String param){
return "hello "+param+" ,this is Producer2 message";
}
}再次启动该服务。
3.消费者(同上 一.3)
调试说明
会交互显示:hello param ,this is Producer1 message
hello param ,this is Producer2 message
文章标题:eureka服务提供和调用
发布时间:2019-11-13, 15:11:06
最后更新:2019-11-13, 15:10:49