SpringCloud(二)SpringCloud項目結構


一.SpringCloud項目結構
1.Eureka注冊發現服務
    springcloud使用服務發現框架來管理微服務。

    使用IDEA創建項目,選擇spring Initializr,然后點擊next(如果不能正確顯示,說明網絡有問題),
再進行項目基本信息填寫之后,選擇cloud discovery中的eureka service。選擇spring boot的版本2.1.4

    

 

創建成功之后查看pom.xml文件,可以查看到spring cloud已經導入

springboot是2.1.4的正式版
springcloud是Greenwich.SR1正式版之后的第一次修正版
根據官網的介紹,Greenwich版本對應的剛好是springboot2.1.x的版本,所以pom文件的版本配置是OK的。

 

 

 

項目創建成功來,現在就開始寫代碼吧。

首先,作為spring的一個發現服務(此處項目起名為yiyuancloud),本身是不需要邏輯代碼的。
在application.properties(很多人都習慣使用application.yml,因為本人習慣了properties,這里就不作更改了)中配置
 

server.port=8761
eureka.instance.hostname=localhost
#是否需要向服務端進行注冊,如果只獲取不注冊,設置為false
eureka.client.register-with-eureka=false
#是否需要從服務端獲取注冊信息,服務端如果是集群需要配置為true
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
然后在啟動類上加上注解
@EnableEurekaServer
啟動項目,訪問http://localhost:8761/即可看到Eureka的服務注冊信息。

 

2.Eureka提供者服務
和第一步一樣創建項目provider。修改application.properties

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=8773
spring.application.name=service-provider
然后在啟動類上加上注解
@EnableEurekaServer
啟動項目,訪問http://localhost:8761/即可看到Eureka上新注冊的provider信息service-provider。

 

修改啟動類,測試代碼
 

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaClient
@RestController
@EnableDiscoveryClient
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}

@Value("${server.port}")
String port;

@RequestMapping("/hi")

public String home(@RequestParam String name)
{
return "hi " + name + ",i am from port:" + port;
}

}
訪問http://localhost:8773/hi?name=武松


3.Eureka消費者服務
和第一步一樣創建項目customer。修改application.properties

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=8764
spring.application.name=feign-consumer
#斷路器
feign.hystrix.enabled=true
在啟動類上加入注解
@EnableEurekaClient啟動項目,訪問http://localhost:8761/即可看到Eureka上新注冊的customer信息feign-customer。

 

修改啟動類,添加HelloController和HelloService

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaClient
@EnableHystrix
public class CustomerApplication {

public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
}

}

@RestController
public class HelloController
{
@Autowired
HelloService helloService;

@RequestMapping(value = "/hi")
public String hi(@RequestParam String name)
{
return helloService.hiService(name);
}

}

@Service
public class HelloService
{
@Autowired
RestTemplate restTemplate;

// 斷路器配置,當無法調用如下方法時,就會調用自定的hiError方法。
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name)
{
String str1 = restTemplate.getForObject("http://service-provider/hi?name=" + name, String.class);
return str1;
}

public String hiError(String name)
{
return "hey " +
name + ", there is some problem with hi page";
}
}
這時候訪問http://localhost:8764/hi?name=fys。可知,該接口調用的是provider服務中的/hi接口

 

如果這時候關閉provider服務,customer將會進入斷路器配置的hiError方法

4.Eureka的高可用性
Eureka的高可用性實現為多個Eureka的相互注冊,這樣哪怕只剩一個eureka服務,系統仍然能夠完好運行。例如現在有三個erueka1:8761,erueka2:8762,erueka3:8763
重點:遇坑筆記,在正式版的springcloud中,同一域名只能配置一個erueka服務。
想要配置多服務,可以修改host文件進行轉發
erueka1的配置為

eureka.client.service-url.default-zone=http://eureka2:8762/eureka/,http://eureka3:8763/eureka/
erueka2的配置為

eureka.client.service-url.default-zone=http://eureka1:8761/eureka/,http://eureka3:8763/eureka/
erueka3的配置為

eureka.client.service-url.default-zone=http://eureka1:8761/eureka/,http://eureka2:8762/eureka/
同時在注冊服務,比如customer中的注冊地址設置多個

eureka.client.service-url.defaultZone= http://eureka1:8761/eureka/,http://eureka2:8762/eureka/,http://eureka3:8763/eureka/
訪問http://eureka1:8761/可以看到eureka1,eureka2,eureka3相互注冊成功

 

至此。最基本的微服務框架就ok了。
yiyuancloud服務用來管理發現
provider和customer作為微服務之間通過restful進行通信。
補充:Eureka還擁有心跳檢測,健康檢查,負載均衡等功能。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM