spring cloud 之初步認識

1.實現(xiàn)一個微服務(wù),有訂單服務(wù),有會員服務(wù)。訂單服務(wù)通過rest方式去調(diào)用會員服務(wù),注冊中心使用eureka。

第一步:實現(xiàn)一個eureka服務(wù)注冊中心。

POM的相關(guān)配置:增加依賴如下:

image

application.yml的配置如下:

設(shè)置euraka-server訪問時帶有權(quán)限,不能直接訪問

security:

basic:

enabled:true

user:

name: zzh

password: password123

server:

port:8086

設(shè)置注冊顯示的實例名稱為eureka-server1,如果不配置顯示為UNKNOWN

spring:

application:

name: eureka-server1

eureka:

關(guān)閉Eureka的自我保護功能

server:

enable-self-preservation:false

  #配置Eureka Server清理無效節(jié)點的時間間隔

  eviction-interval-timer-in-ms:4000

instance:

hostname: localhost

配置鼠標點擊到某個服務(wù)上顯示對應的IP地址

  prefer-ip-address:true

client:

register-with-eureka:false

  fetch-registry:false

  healthcheck:

enabled: true

serviceUrl:

defaultZone: http://zzh:password123@{eureka.instance.hostname}:{server.port}/eureka/

新建一個類:

EurakaServerApplication

@SpringBootApplication

@EnableEurekaServer

public class EurakaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurakaServerApplication.class, args);

}

}

啟動服務(wù)。

通過瀏覽器訪問后就可以查看到基本eureka 的整個界面情況。

第二步:把服務(wù)注冊到eureka中。

新建一個會員服務(wù)工程

增加依賴:

image

編寫application.yml

server:

port:8088

spring:

application:

name: service-member

eureka:

instance:

prefer-ip-address:true

client:

register-with-eureka:true

healthcheck:

enabled: true

serviceUrl:

defaultZone: http://zzh:password123@127.0.0.1:8086/eureka/

編寫controller

@RestController

@RequestMapping("/member")

public class MemberController {

@RequestMapping("/getAllMemberInfo")

@ResponseBody

public ListgetAllMemberInfo(){

List list=new ArrayList<>();

    list.add("zhangsan");

    list.add("lisi");

    list.add("wangwu");

    return list;

}

}

編寫啟動服務(wù)類:

@SpringBootApplication

@EnableEurekaClient

@ComponentScan(basePackages ="com.itshirui")

public class ServiceMemberApplication {

public static void main(String[] args) {

SpringApplication.run(ServiceMemberApplication.class, args);

}

}

啟動成功后,可以去注冊中心的頁面中查看該服務(wù)。

第三部,編寫訂單服務(wù),訂單服務(wù)中調(diào)用會員服務(wù),目前通過rest方式進行通訊的發(fā)送。

配置和會員服務(wù)一樣,只是controller中的調(diào)用采用rest方式進行調(diào)用:

@RestController

@RequestMapping("/order")

public class OrderController {

@Autowired

private RestTemplaterestTemplate;

@RequestMapping("/getAllOrderInfo")

@ResponseBody

public List  getAllOrderInfo(){

List result =restTemplate.getForObject("http://service-member/member/getAllMemberInfo", List.class);

    return result;

}

}

經(jīng)過測試通過訂單服務(wù)調(diào)用會員服務(wù),能成功。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容