目录
?啥是服务注册/调用?
emmmm,举个栗子:就是小A写博客放在csdn上(服务注册),被小C看到了拿来用一下(服务调用),这里csdn平台相当于注册中心,供小A注册,供小C调用。
可能不太恰当,大概就是这么个意思。
上文已经介绍了服务注册中心的搭建,本文展示使用eureka注册中心,搭建一个服务端注册服务,客户端去调用服务使用的案例。
?服务提供者注册服务
1.创建spring boot项目:
使用官方快速创建➡:https://start.spring.io/
取名为:spring-cloud-producer
2.pom加入依赖:
<dependencies>
<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>
3.配置文件
application.properties配置如下:
spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
4.启动类加入注解@EnableDiscoveryClient
@SpringBootApplication
//开启服务注册
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
5.controller提供hello服务
创建HelloController类:
类具体内容:
//相当于@Controller+@ResponsBody,结果返回json形式
@RestController
public class HelloController {
//url映射
@RequestMapping("/hello")
public String index(@RequestParam String name) {
//返回一个字符串供测试
return "hello ,"+name+",this is producer send first messge";
}
}
启动eureka注册中心,再启动刚刚创建的项目,可以在注册中心看到spring-cloud-producer项目:
ok,此时服务提供者,已经注册完成。
?服务调用者调用服务
和服务提供者项目类似
1.创建spring boot项目:
使用官方快速创建➡:https://start.spring.io/
取名为:spring-cloud-consumer
2.pom加入依赖,要加入feign依赖:
<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>
3.application.properties配置如下:
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
4.启动类添加注解@EnableDiscoveryClient和@EnableFeignClients
@SpringBootApplication
//开启服务注册与发现
@EnableDiscoveryClient
//开启feign进行远程服务调用
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
?ps:安利 feign
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
5.feign远程调用
新建包:remote
创建接口:HelloRemote
接口具体内容:
//使用注解@FeignClient 定义feign客户端
//name:远程服务名,及spring.application.name配置的名称
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
//这里的方法名和远程服务提供者的方法名要一致
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
6.web层远程调用
@RestController
public class ConsumerController {
//将HelloRemote注入到controller层,像普通方法一样去调用即可
@Autowired
HelloRemote HelloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
//去用feign进行远程调用
return HelloRemote.hello(name);
}
}
远程服务调用者也ok了~
?测试服务调用
➡查看注册情况
依次启动,注册中心:eureka/服务提供者:spring-cloud-producer/服务调用者:spring-cloud-consumer
打开浏览器,输入:localhost:8000
可以看到服务提供者:spring-cloud-producer;服务调用者:spring-cloud-consumer正常注册到注册中心:eureka
➡测试服务提供者
打开浏览器,输入:localhost:9000/hello?name=hahaha
说明服务提供者正常。
➡测试服务调用者
打开浏览器,输入:localhost:9001/hello/heiheihei
说明远程调用正常。
➡测试负载均衡
修改服务提供者:spring-cloud-producer的结果返回
controller类:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
//修改一下输出,方便测试查看
return "hello "+name+",this is producer 2 send first messge";
}
}
修改配置文件端口,否则端口占用冲突
spring.application.name=spring-cloud-producer
server.port=9003
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
打包启动,这样不影响刚刚的9000端口的服务提供者
打包:mvn package
启动:java -jar 包名.jar
刷新注册中心eureka:
可以看到两个服务提供者,提供hello服务。
➡进行调用测试:浏览器输入localhost:9001/hello/xixixi
继续访问:localhost:9001/hello/xixixi
不断的访问localhost:9001/hello/xixixi进行调用测试,会发现两种结果交替出现,说明两个服务中心自动提供了服务均衡负载的功能。如果我们将服务提供者的数量在提高为N个,测试结果一样,请求会自动轮询到每个服务端来处理。
?ps: feign本身里面就包含有了ribbon,提供负载均衡的功能,配合Hystrics熔断,完美使服务稳定运行~后续讲解。