SpringCloud學習(二)---Eureka

Eureka

重點在使用,概念和源碼基本不涉及java

Eureka是一個基於REST(REST是HTTP協議的)的服務,主要在亞馬遜網絡服務(AWS)雲中使用,定位服務來進行中間層服務器的均衡負載和故障轉移.web

Spring Cloud封裝Eureka來實現服務註冊和發現,Eureka採用了C-S的設計架構,Eureka Server做爲服務註冊功能的服務器,是服務註冊中心,系統中的其餘微服務都是Eureka Client,鏈接到Eureka Server,並維持心跳鏈接.這樣就能夠經過 Eureka Server 來監控系統中各個微服務是否正常運行。spring

Eureka由兩個組件組成: Eureka Server和Eureka Client. Eureka Server用做服務註冊服務器.Eureka Client就是Java客戶端,分爲兩種,一種是Service Provider,一種是Service Consume,可是二者不是嚴格的概念區分,也就是說一個Service Consume也能夠是Service Provider,看實際的場景.服務器

也就是Eureka Server提供服務的註冊和發現網絡

Service Provider將自身服務註冊到Eureka Server,並保持心跳續約等架構

Service Consumer從Eureka Server獲取註冊列表,進行服務消費,也就是經過遠程調用與Service Provider進行通訊.app

搭建Eureka Server

  1. 建立項目,引入依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  1. 啓動類添加註解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerSmileApplication {

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

}
  1. 配置文件
spring:
  application:
    name: spring-cloud-eureka-server
server:
  port: 8761
eureka:
  client:
    # 是否註冊本身
    register-with-eureka: false
    # 是否從Eureka Server獲取註冊信息
    fetch-registry: false
    # 設置與Eureka Server交互的地址,多個地址使用逗號合開,也就是集羣
    service-url:
      default-zone: http://localhost:${server.port}/eureka/
  1. 啓動項目,直接訪問http://localhost:8761/就能夠看到Eureka Server的主界面,這個時候No instances available.

集羣的搭建(3個)

  1. 建立項目添加依賴(同上)
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  1. 啓動類添加註解(同上)
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerSmileJqApplication {

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

}
  1. 配置文件,三個配置文件,第一個個application-master.properties:
server.port=8761
eureka.instance.hostname=master
eureka.client.serviceUrl.defaultZone=http://slave1:8762/eureka/,http://slave2:8763/eureka/
spring.application.name=eureka-server
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

第二個application-slave1.properties負載均衡

server.port=8762
eureka.instance.hostname=slave1
eureka.client.serviceUrl.defaultZone=http://master:8761/eureka/,http://slave2:8763/eureka/
spring.application.name=eureka-server
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

application-slave2.propertieside

server.port=8763
eureka.instance.hostname=slave2
eureka.client.serviceUrl.defaultZone=http://master:8761/eureka/,http://slave1:8762/eureka/
spring.application.name=eureka-server
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

以後找到本地的hosts文件,推薦使用everything軟件,能夠搜索全盤,找到hosts文件添加下面內容:spring-boot

127.0.0.1   master
127.0.0.1   slave1 
127.0.0.1   slave2

進行打包,運行

mvn clean package
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=master
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=slave1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=slave2

啓動完成後進行訪問就能夠看到效果,此次進行了註冊.

服務註冊與調用實戰

這個就要說到上面的Eureka的三個角色,一個註冊中心,一個服務提供者,一個服務消費者,也就是Eureka Server,Service Provider(案例中拼錯了,寫成producter了,諒解),Service Consumer.中間還還涉及到遠程過程調用Feign,這裏只須要看一下就好,後面再說這個.

  1. 建立Eureka Service項目,添加依賴,添加註解,修改配置文件以下展現
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>


@SpringBootApplication
@EnableEurekaServer
public class ServerSmileDemoApplication {

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

}


server.port=8761
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
  1. 建立提供者,依次依賴,註解,配置文件,最後寫個controller
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>


@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProducterDemoApplication {

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

}


server.port=9000
spring.application.name=eureka-service-producter
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/


@RestController
public class HelloController {

    /** 看的博客上出現了Request method ‘POST’ not supported問題,由於沒有添加@RequestParam註解,說是不添加那個註解就是post請求,就算是用GetMapping指定了仍是POST請求,這個本人沒有測試 */
    @GetMapping("hello")
    public String hello(@RequestParam String name){
        return "hello," + name + "1111111";
    }

}
  1. 建立消費者,添加依賴(這個須要添加feign的依賴),後面依次
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceConsumerDemoApplication {

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

}

server.port=9010
spring.application.name=eureka-service-consumer
eureka.client.service-url.defaultZone=http://localhost:8761/eureka


@RestController
public class ConsumerController {

    @Autowired
    private HelloRemote helloRemote;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name){
        return helloRemote.hello(name);
    }
}

上面按照順序啓動,先啓動註冊中心,而後啓動提供者,最後啓動消費者,訪問的時候訪問消費者的hello,就能夠訪問到提供者的hello了.

負載均衡

feign有負載均衡的做用(feign是基於Ribbon實現的,因此自帶客戶端負載均衡功能),在上面的基礎之上,再次建立個提供者,與上面提供者不一樣的是端口號和controller稍微變下,具體代碼以下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

server.port=9001
spring.application.name=eureka-service-producter
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProducterDemo1Application {

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

}


@RestController
public class HelloController {

    @GetMapping("hello")
    public String hello(@RequestParam String name){
        return "hello," + name + "222222";
    }
}

以後同時啓動四個項目,看註冊中心就能看到EUREKA-SERVICE-PRODUCTER後有兩個服務,以後訪問http://localhost:9010/hello/wangzhi就能夠看到hello,wangzhi1111111和hello,wangzhi222222交替出現,這個就是負載均衡了.

到這Eureka基本就結束了我沒有看源碼,這個說明下,還有人說Eureka閉源了,其實並非,只是不繼續開發2.X版本,1.X版本還在更新維護呢.並且如今阿里開源了Nacos,這個也是能夠替代Eureka的,因此說替代方案仍是有的.

相關文章
相關標籤/搜索