組件名:Netflix Eureka
作用:支撐微服務的自注冊、自發現,提供負載均衡能力
開發環境使用IDEA,jdk1.8
一、搭建eureka服務
1.新建maven項目,配置pom.xml文件
<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.新建啓動類
@SpringBootApplication @EnableEurekaServer public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } }
3.新建配置文件application.yml
server: port: 1000 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false spring: application: name: eureka-server
4.啓動(啓動類)
5.訪問 eureka:http://localhost:1000/
erueka服務器啓動成功,目前還未有服務註冊
二、搭建服務提供方
1.新建maven項目,配置pom.xml文件
<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服務
@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配置文件
eureka: client: serviceUrl: defaultZone: http://localhost:1000/eureka/ spring: application: name: feign-client-test-001 server: port: 2000
查看路徑id展示,需要添加配置
eureka: client: serviceUrl: defaultZone: http://admin:admin123@localhost:1000/eureka instance: prefer-ip-address: true
4、運行,查看之前Erueka服務端的頁面,FEIGN-CLIENT-TEST-001在註冊中心變爲了大寫這個注意下
5、訪問:http://127.0.0.1:2000/hello
三、搭建服務消費方
使用@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文件
<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
/** * 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是全大寫的
@FeignClient("FEIGN-CLIENT-TEST-001") public interface IHello { @RequestMapping(value = "/hello",method = RequestMethod.GET) String hello(); }
其中@FeignClient中指定需要調用的微服務的名稱(全大寫),@RequestMapping中指定訪問微服務響應接口的路徑,如之前微服務的hello方法是通過"/hello"路徑訪問,那麼這裏需要配置一致
4、新建Controller類,爲前端提供REST接口
@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
server: port: 8081 eureka: client: serviceUrl: defaultZone: http://localhost:1000/eureka/ spring: application: name: feign-client-test-002
6、運行,查看之前Erueka服務端的頁面
7、訪問:http://127.0.0.1:8081/gethello
這裏訪問的就是feign-client-test-001的hello服務。