接上一篇文章【SpringCloud】第五章 构建服务提供者
服务提供者启动以后,我们要加入我们的对外暴露的接口类,Controller.具体目录如下:
我们的TestController如下,加上@RestController注解,表示这是一个对外提供的接口类
package pers.cc.springCloud.eurekaDiscovery.test.controller;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/dc")
public String dc() {
String services = "Services: " + discoveryClient.getServices() + new Date().getTime();
System.out.println(services);
return services;
}
@GetMapping("/add")
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
System.out.println("我是eureka-client 我的add方法被调用了,可惜:"+(a + b));
return a + b;
}
@GetMapping("/stop")
public String stop() throws InterruptedException {
System.out.println("stop");
Thread.sleep(5000L);
return "stop";
}
}
启动我们的服务,打开我们浏览器,访问
http://localhost:2001/add?a=1&b=100
可以看到浏览器出现了101这个值,说明接口调用成功
到此,第五章已经将一个完整的服务提供者,讲解完毕。