SpringCloud-service 服務(wù)提供

微服務(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市融虽,隨后出現(xiàn)的幾起案子享完,更是在濱河造成了極大的恐慌,老刑警劉巖衣形,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驼侠,死亡現(xiàn)場離奇詭異,居然都是意外死亡谆吴,警方通過查閱死者的電腦和手機(jī)倒源,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來句狼,“玉大人笋熬,你說我怎么就攤上這事∧骞剑” “怎么了胳螟?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長筹吐。 經(jīng)常有香客問我糖耸,道長,這世上最難降的妖魔是什么丘薛? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任嘉竟,我火速辦了婚禮,結(jié)果婚禮上洋侨,老公的妹妹穿的比我還像新娘舍扰。我一直安慰自己,他們只是感情好希坚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布边苹。 她就那樣靜靜地躺著,像睡著了一般裁僧。 火紅的嫁衣襯著肌膚如雪个束。 梳的紋絲不亂的頭發(fā)上慕购,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天,我揣著相機(jī)與錄音播急,去河邊找鬼脓钾。 笑死,一個(gè)胖子當(dāng)著我的面吹牛桩警,可吹牛的內(nèi)容都是我干的可训。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼捶枢,長吁一口氣:“原來是場噩夢啊……” “哼握截!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起烂叔,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤谨胞,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后蒜鸡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胯努,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年逢防,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了叶沛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡忘朝,死狀恐怖灰署,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情局嘁,我是刑警寧澤溉箕,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站悦昵,受9級特大地震影響肴茄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜但指,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一寡痰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧枚赡,春花似錦、人聲如沸谓谦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽反粥。三九已至卢肃,卻和暖如春疲迂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背莫湘。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工尤蒿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人幅垮。 一個(gè)月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓腰池,卻偏偏與公主長得像,于是被迫代替她去往敵國和親忙芒。 傳聞我的和親對象是個(gè)殘疾皇子示弓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354