新聞中心
這篇文章主要講解了“spring-clound的簡(jiǎn)單使用方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“spring-clound的簡(jiǎn)單使用方法”吧!
普寧網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,普寧網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為普寧數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的普寧做網(wǎng)站的公司定做!
1 第一步

1)創(chuàng)建eureka服務(wù)中心 //相當(dāng)于dubbo使用zookeeper的原理
pom.xml
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
application.properties
eureka.client.service-url.defaultZone= http://localhost:8761/eureka/
server.port=8001
eureka.instance.hostname=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
3)在主應(yīng)用中啟用eureka @EnableEurekaServer
測(cè)試結(jié)果

2 創(chuàng)建服務(wù)提供者的應(yīng)用
1)pom.xml
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
application.properties
server.port=8002
spring.application.name=provider
#使用服務(wù)的ip地址來進(jìn)行注冊(cè)
eureka.instance.prefer-ip-address=true
eureka.client.service-url.defaultZone:http://127.0.0.1:8761/eureka/
創(chuàng)建提供的服務(wù)內(nèi)容 @Service public class BookService { public String getBook(){ return "少年,還好嗎?"; } }
4)創(chuàng)建向外提供服務(wù)的controller @RestController
public class BookController {
@Autowired
BookService bookService;
@GetMapping("/book")public String getBook(){
return bookService.getBook();
} }
3 創(chuàng)建消費(fèi)者
1)pom.xml
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
application.properties
spring.application.name=consumer
server.port=8200
eureka.instance.prefer-ip-address=true
eureka.client.service-url.defaultZone:http://localhost:8761/eureka/
3)編寫調(diào)用服務(wù)的controller
@RestController
public class helloController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/hello")public String UserBook(String name){
String s= restTemplate.getForObject("http://PROVIDER/book",String.class);
return name+"購(gòu)買了"+s;} }
4)啟用從遠(yuǎn)程調(diào)用服務(wù) @EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){return new RestTemplate();
} }
測(cè)試結(jié)果: 
感謝各位的閱讀,以上就是“spring-clound的簡(jiǎn)單使用方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)spring-clound的簡(jiǎn)單使用方法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
分享文章:spring-clound的簡(jiǎn)單使用方法
鏈接地址:http://www.dlmjj.cn/article/jcehps.html


咨詢
建站咨詢
