大纲
- 注册中心
- 功能服务one
- 功能服务two
整体目录结构如下
这里整个功能是一个maven项目,注册中心与功能服务都是maven项目里面的模块。
注册中心
利用eureka搭建服务注册中心模块server-eureka
这是一个空的springboot项目,主要就是把他当做服务注册中心使用
1.springboot运行入口代码package org.server.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
通过@EnableEurekaServer注解将应用标记为服务注册中心
2.pom配置文件
<?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> <packaging>jar</packaging> <name>server-eureka</name> <description>eureka-server project for Spring Boot</description> <groupId>cn.ysh.cloud</groupId> <artifactId>server-eureka</artifactId> <parent> <groupId>cn.ysh</groupId> <artifactId>cloud</artifactId> <version>0.0.1-SNAPSHOT</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> <spring-cloud.version>Finchley.SR1</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> </project>
最主要的是要引入spring-cloud-starter-netflix-eureka-server这个jar包,用于支持以eureka作为服务注册中心
3.eureka服务的基本配置
server: port: 8761(指定应用端口) spring: application: name: eureka-server(指定应用名称) eureka: instance: hostname: localhost client: register-with-eureka: false (不注册自己) fetch-registry: false(不开启检索) service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
做好这些配置,就完成了。
启动eureka-server,界面如下:
标红的应用中心显示目前没有服务注册到eureka中心
搭建第一个服务应用client-one-eureka
1.springboot入口代码
package org.client.one.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableEurekaClient @SpringBootApplication @EnableFeignClients public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
两个重要注解@EnableEurekaClient、@EnableFeignClients。
@EnableEurekaClient标记为Eureka客服端(就是标记为Eureka一个服务)
@EnableFeignClients开启Feign调用服务的方式。(调用服务有两种方式,一种是ribbon,一种是Feign),我这里想实现的是基于Feign实现client-one-eureka调用client-two-eureka服务方法。2.pom依赖
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven->4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.ysh</groupId> <artifactId>cloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.ysh.cloud</groupId> <artifactId>client-one-eureka</artifactId> <name>client-one-eureka</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <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> </project>
主要是spring-cloud-starter-netflix-eureka-server的jar和spring-cloud-starter-openfeign的jar。
3.应用的基本配置
server: port: 2001 spring: application: name: client-two-eureka eureka: client: service-url: defaultZone: http://localhost:8761/eureka/(这个和server配置的是一致的) fetch-registry: true register-with-eureka: true
到此第一个应用配置完成。
搭建第二个服务应用client-two-eureka
1.springboot入口代码
package org.client.two.eureka; 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.client.RestTemplate; @EnableEurekaClient @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
client-two-eureka与client-one-eureka区别在于引入了restTemplate类。因为ribbon调用其他的服务接口需要restTemplate配合。
2.pom配置文件
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven->4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.ysh</groupId> <artifactId>cloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.ysh.cloud</groupId> <artifactId>client-two-eureka</artifactId> <name>client-two-eureka</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</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> </project>
spring-cloud-starter-netflix-ribbon的jar,这里主要是我想让client-two-eureka通过ribbon的方式调用client-one-eureka的服务接口。
3.配置文件
server: port: 2001 spring: application: name: client-two-eureka eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ fetch-registry: true register-with-eureka: true
到此完成第二个服务应用的搭建。
启动client-one-eureka、client-two-eureka、server-eureka。当然server-eureka启动完成后,再启动client-one-eureka、client-two-eureka。原因很简单,服务中心起来之后,才能注册服务。
服务中心管理页面如下:
可以看到应用中心里面已经有两个了。到这里服务注册功能已经完成。
服务之间相互调用
1.client-one-eureka
创建一个Client1Test的controllerpackage org.client.one.eureka.controller; import org.client.one.eureka.service.FeignSevice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import colud.clent.model.User; @RestController @RequestMapping("/client1Test") public class Client1Test { @Autowired FeignSevice feignSevice; /**通过client-one-eureka调用client-two-eureka里面的client2Test/clint1(Feign方式) * @param id * @return */ @GetMapping("/clint1") public String name(@RequestParam String id) { System.out.println("进入client-one-eureka服务client1Test/clint1方法"); return feignSevice.test(id, new User("1", "ysh")); } /**等待被client-two-eureka调用 * * @param id * @return */ @GetMapping("/toCall") public String test1client(@RequestParam String id) { System.out.println("进入client-one-eureka服务client1Test/toCall方法"); return "调用eureka-client1服务test1client方法,参数id=" + id; } }
client-one-eureka调用client-two-eureka是通过Feign的方式。所以这边创建了一个FeignSevice。代码如下:
package org.client.one.eureka.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import colud.clent.model.User; @Component @FeignClient(name = "client-two-eureka") public interface FeignSevice { @RequestMapping(value = "/client2Test/test2", method = RequestMethod.GET) public String test(@RequestParam("id") String id, @RequestBody User user); }
代码说明:@FeignClient指定了我们所要调用服务的名称,@RequestMapping后面跟的就是我们所要调用服务的具体路径。
2.client-two-eureka
创建的controller代码package org.client.two.eureka.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import colud.clent.model.User; @RestController @RequestMapping("/client2Test") public class Client2Test { @Autowired RestTemplate restTemplate; /**通过client-two-eureka调用client-one-eureka的client1Test/toCall方法(ribbon方式) * * @param id * @return */ @GetMapping("/hi") public String hi(@RequestParam String id) { System.out.println("进入client-two-eureka服务client2Test/hi方法"); return restTemplate.getForObject("http://client-one-eureka/client1Test/toCall?id=" + id, >String.class); } /**等待被client-one-eureka调用 * * @param id * @param user * @return */ @PostMapping("/test2") public String testOne2(@RequestParam(required = false) String id, @RequestBody User user) { System.out.println("进入client2Test/test2"); return "调用client-two-eureka服务test2方法,参数id=" + id + "user=" + user; } }
通过代码可以知道client-one-eureka的client1Test/clint1?id=''可以调用到client-two-eureka的client2Test/test2接口。这个接口采用了返回复杂对象予以说明。由于采用了复杂对象。为了服务间共享模型数据。所以我新建了clent-model模块。并在client-one-eureka服务、client-two-eureka服务中相继加入该模块依赖,完成数据结构共享。调用结果显示如下:
同样通过client-two-eureka的client2Test/hi?id=''可以调用到client-one-eureka的client1Test/toCall方法。结果演示如下:
文章到此就完成了服务注册与通过ribbon、Feign两种方式完成服务间相互调用。