微服務(wù)架構(gòu)中,一般存在2種服務(wù),一種是消費(fèi)服務(wù),一種是提供服務(wù)审胚,消費(fèi)服務(wù)在 SpringCloud-feign 聲明式服務(wù)調(diào)用 這一篇文章中已講過,本章我們主要講解提供服務(wù)
不論是什么服務(wù)礼旅,都需要注冊中心,可以參考 SpringCloud-eureka構(gòu)建簡單注冊中心或SpringCloud-eureka高可用注冊中心洽洁,啟動(dòng)注冊中心后痘系, 消費(fèi)服務(wù)和提供服務(wù)才能注冊。
下面創(chuàng)建一個(gè)Gradle工程饿自,工程依賴如下(這里使用boot1.5.x):
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
compile 'org.slf4j:slf4j-api:1.7.14'
compile('org.springframework.cloud:spring-cloud-starter-eureka')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
配置文件application.properties如下:
spring.application.name=hello-service
# 單機(jī)
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=8081
配置啟動(dòng)類汰翠,將啟動(dòng)服務(wù)配置為可發(fā)現(xiàn)的服務(wù),如下:
@EnableDiscoveryClient
@SpringBootApplication
public class CloudApplication {
public static void main(String[] args) {
SpringApplication.run(CloudApplication.class, args);
}
}
接著昭雌,創(chuàng)建controller复唤,定義相應(yīng)在的服務(wù)接口,如下:
@RestController
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() throws Exception {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
return "Hello World";
}
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
public String hello(@RequestParam String name) {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello1, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
return "Hello " + name;
}
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
public User hello(@RequestHeader String name, @RequestHeader Integer age) {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello2, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
return new User(name, age);
}
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
public String hello(@RequestBody User user) {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello3, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
return "Hello "+ user.getName() + ", " + user.getAge();
}
這樣就可以被消費(fèi)服務(wù)消費(fèi)了烛卧,如restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody()
雖然實(shí)現(xiàn)了service的服務(wù)接口佛纫,但卻有個(gè)問題妓局,提供服務(wù)的接口不容易被消費(fèi)服務(wù)知道,如何解決這個(gè)問題呢呈宇?
我們可以抽取一個(gè)通用的服務(wù)好爬,將url寫在這個(gè)服務(wù)上,如下:
@RequestMapping("/refactor")
public interface HelloService {
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name) ;
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
}
這個(gè)服務(wù)可以放在一個(gè)單獨(dú)的工程內(nèi)甥啄,受maven/gradle管理存炮,這樣就可以在消費(fèi)端和提供端共享,下面看下新的controller如下:
@RestController
public class RefactorHelloController implements HelloService {
@Override
public String hello(@RequestParam("name") String name) {
return "Hello " + name;
}
@Override
public User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age) {
return new User(name, age);
}
@Override
public String hello(@RequestBody User user) {
return "Hello "+ user.getName() + ", " + user.getAge();
}
}
新的controller只需實(shí)現(xiàn)這個(gè)接口服務(wù)就可以了蜈漓。
學(xué)習(xí)交流穆桂,請加群:64691032