springcloud 学习-eureka搭建

简介:

组件名:Netflix Eureka 

作用:支撑微服务的自注册、自发现,提供负载均衡能力

开发环境使用IDEA,jdk1.8

一、搭建eureka服务

1.新建maven项目,配置pom.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<parent>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-parent</artifactId>
     <version>Camden.SR7</version>
</parent>
<dependencies>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka-server</artifactId>
     </dependency>
</dependencies>


2.新建启动类

1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaServer
public  class  Application {
     public  static  void  main(String[] args){
         SpringApplication.run(Application. class ,args);
     }
}


3.新建配置文件application.yml

1
2
3
4
5
6
7
8
9
10
11
server:
   port:  1000
eureka:
   instance:
     hostname:  localhost
   client:
     register-with-eureka:  false
     fetch-registry:  false
spring:
   application:
     name: eureka-server


4.启动(启动类)

wKiom1loQcWxBxOXAAFpvirsLA0941.png-wh_50

5.访问 eureka:http://localhost:1000/

wKioL1loQdPzub6yAAGmY2IEJ_0905.png-wh_50


erueka服务器启动成功,目前还未有服务注册


二、搭建服务提供方


1.新建maven项目,配置pom.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<parent>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-parent</artifactId>
     <version>Camden.SR7</version>
</parent>
<dependencies>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka-server</artifactId>
     </dependency>
</dependencies>

2.创建Application启动类,提供/hello服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
@ComponentScan
@EnableEurekaClient
@EnableAutoConfiguration
@RestController
public  class  Application {
     @RequestMapping (value =  "hello" ,method = RequestMethod.GET)
     public  String hello(){
         return  "你好,世界" ;
     }
     public  static  void  main(String[] args){
         new  SpringApplicationBuilder(Application. class ).web( true )
                 .run(args);
     }
}

3、新建application.yml配置文件

1
2
3
4
5
6
7
8
9
eureka:
   client:
     serviceUrl:
       defaultZone: http: //localhost:1000/eureka/
spring:
   application:
     name: feign-client-test- 001
server:
   port:  2000

查看路径id展示,需要添加配置

1
2
3
4
5
6
eureka:
   client:
     serviceUrl:
       defaultZone: http: //admin:admin123@localhost:1000/eureka
   instance:
     prefer-ip-address:  true

wKiom1lriGuQiWzZAAJ4yS0-35I611.png-wh_50

4、运行,查看之前Erueka服务端的页面,FEIGN-CLIENT-TEST-001在注册中心变为了大写这个注意下

wKioL1loaEnS0ZsfAAGxI5irgfs102.png-wh_50

5、访问:http://127.0.0.1:2000/hello

wKioL1loaHqzCx-eAABKirqiF0w075.png-wh_50

三、搭建服务消费方

使用@FeignClient注解

Feign is a declarative web service client. It makes writing web service clients easier.

如上是Spring Cloud文档中对于Feign的定义,结合之前的两篇博文,在这里我们就可以吧Feign简单的理解为用户(前端)可以直接接触到的REST接口提供者。在Feign中,我们可以方便的访问和使用意已经在Erueka服务器中注册过的服务了。

1、建立maven工程,配置pom.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version> 1.5 . 2 .RELEASE</version>
     <relativePath/> <!-- lookup parent from repository -->
</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.cloud</groupId>
         <artifactId>spring-cloud-starter-feign</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</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>Camden.SR6</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>

2、建立包及启动类FeignApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
  * Created by gaofeng on 2017/7/14.
  */
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public  class  FeignApplication {
     public  static  void  main(String[] args) {
         SpringApplication.run(FeignApplication. class , args);
     }
}

3、建立接口类,用来调用上文中FEIGN-CLIENT-TEST-001服务的方法hello(),FEIGN-CLIENT-TEST-001是全大写的

1
2
3
4
5
@FeignClient ( "FEIGN-CLIENT-TEST-001" )
public  interface  IHello {
     @RequestMapping (value =  "/hello" ,method = RequestMethod.GET)
     String hello();
}

其中@FeignClient中指定需要调用的微服务的名称(全大写),@RequestMapping中指定访问微服务响应接口的路径,如之前微服务的hello方法是通过"/hello"路径访问,那么这里需要配置一致

4、新建Controller类,为前端提供REST接口

1
2
3
4
5
6
7
8
9
@RestController
public  class  HelloController {
     @Autowired
     private  IHello iHello;
     @RequestMapping (value =  "gethello" ,method = RequestMethod.GET)
     public  String getHello() {
         return  iHello.hello();
     }
}

5、配置Feign的配置文件,指定Eureka服务器注册地址和访问端口application.yml

1
2
3
4
5
6
7
8
9
server:
   port:  8081
eureka:
   client:
     serviceUrl:
       defaultZone: http: //localhost:1000/eureka/
spring:
   application:
     name: feign-client-test- 002

6、运行,查看之前Erueka服务端的页面

wKioL1loa7zh0vBVAAHgmclJuas961.png-wh_50

7、访问:http://127.0.0.1:8081/gethello

wKiom1loa_TBHte8AAA_ht7WR9A893.png-wh_50

这里访问的就是feign-client-test-001的hello服务。


本文转自gaofeng36599 51CTO博客,原文链接:http://blog.51cto.com/786678398/1947471

相关文章
|
19天前
|
缓存 负载均衡 监控
SpringCloud&Eureka理论与入门
SpringCloud&Eureka理论与入门
21 0
|
4天前
|
监控 安全 Java
Spring cloud原理详解
Spring cloud原理详解
16 0
|
8天前
|
消息中间件 负载均衡 Java
【Spring Cloud 初探幽】
【Spring Cloud 初探幽】
15 1
|
9天前
|
安全 Java Docker
|
10天前
|
Java 开发者 微服务
Spring Cloud原理详解
【5月更文挑战第4天】Spring Cloud是Spring生态系统中的微服务框架,包含配置管理、服务发现、断路器、API网关等工具,简化分布式系统开发。核心组件如Eureka(服务发现)、Config Server(配置中心)、Ribbon(负载均衡)、Hystrix(断路器)、Zuul(API网关)等。本文讨论了Spring Cloud的基本概念、核心组件、常见问题及解决策略,并提供代码示例,帮助开发者更好地理解和实践微服务架构。此外,还涵盖了服务通信方式、安全性、性能优化、自动化部署、服务网格和无服务器架构的融合等话题,揭示了微服务架构的未来趋势。
35 6
|
14天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
14天前
|
负载均衡 Java 开发者
Spring Cloud:一文读懂其原理与架构
Spring Cloud 是一套微服务解决方案,它整合了Netflix公司的多个开源框架,简化了分布式系统开发。Spring Cloud 提供了服务注册与发现、配置中心、消息总线、负载均衡、熔断机制等工具,让开发者可以快速地构建一些常见的微服务架构。
|
15天前
|
Java Nacos 微服务
全面学习SpringCloud框架指南
V 哥建议:如果你正在学习SpringCloud,你可以把这篇文章作为学习指南,如果你已经学过了,可以查漏补缺,因为 V 哥这篇文章全面介绍了SpringCloud的方方面面,当然你还需要结合官方文档的细节和源码部分去学习,成功拿下SpringCloud不是问题。加油兄弟!
|
16天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
28 1
|
16天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
19 2