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

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/554474

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。