springcloud+eureka簡單入門案例


springcloud+eureka簡單入門案例

一、服務提供者

直接提供服務,入門案例沒有特別要設置的地方,注意下端口,由於要啟動多個服務,可能會沖突

配置文件(src/main/resources/application.yml)

server:
  port: 8000

二、服務消費者

服務消費者的依賴在這個單獨的demo中其實可有可無,親測不添加,也可以實現demo服務提供能

三、服務消費者啟動類里注入RestTemplate,用於調用遠程服務

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

四、服務消費者Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.xujie.pojo.User;

@RestController
public class UserController {
	
	@Autowired
	private RestTemplate restTemplate;
	
	@GetMapping("/getUser")
	public User getUser() {
		return this.restTemplate.getForObject("http://localhost:8000/getUser", User.class);
	}
}

此時可以通過訪問消費者,間接調用服務提供者的服務,

五、創建服務注冊中心,這里選用Eureka

5.1在springboot基礎環境上添加依賴

<!-- springcloud版本聲明 -->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Brixton.SR5</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>	

<!-- 引入eureka依賴 -->
<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka-server</artifactId>
	</dependency>
</dependencies>

5.2啟動類的編碼

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer  //聲明這是一個Eureka服務器
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
	
}

5.3配置文件(src/main/resources/application.yml)

server:
  port: 8761      #聲明端口號
eureka:
  client:
    register-with-eureka: false    #默認是true,將自己注冊到eureka上
    fetch-registry: false    #是否從eureka上獲取信息,由於本案例是單機,無需從別的eureka上獲取注冊信息
    service-url:
      defaultZone: http://localhost:8761/eureka    #設置與eureka交互的地址,查詢服務和注冊服務都需要依賴這個地址,默認是:http://localhost:8761/eureka

六、將服務提供者注冊到服務注冊中心

6.1改造服務提供者

6.1.1添加依賴,便於把服務注冊到注冊中心Eureka中去:

<!-- springcloud版本聲明 -->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Brixton.SR5</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<!-- eureka的依賴 -->
<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka-server</artifactId>
	</dependency>
</dependencies>

6.1.2修改配置文件,添加下列配置

spring:
  application:
    name: provider   #注冊到Eureka Server上的應用名稱
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka    #注冊的位置
  instance:
    prefer-ip-address: true  #將自己的ip注冊到EuekaServer上

6.1.3修改啟動類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient   //也可以用EnableDiscoveryClient代替,前者兼容性更大,后者僅能兼容Eureka
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

此時可以正常啟動並將服務注冊到了eureka中
啟動Eureka和服務提供者訪問:http://localhost:8761,界面如下:

七、Eureka的高可用

在這里demo之前,修改本地hosts文件,為了區分本地的兩個eureka節點,分別通過:http://peer1:8761和http://peer2:8762訪問

7.1將剛才的eureka項目復制一份,修改兩個的配置文件如下:

eureka1的配置:

server:
  port: 8761      #聲明端口號
eureka:
  instance:
    hostname: peer1
    appname: peer1
  client:
    #register-with-eureka: false    #默認是true,將自己注冊到eureka上,這里設置eureka的高可用,所以需要將自己注冊到eureka上
    #fetch-registry: false    #是否從eureka上獲取信息,由於本案例是單機,無需從別的eureka上獲取注冊信息,這里設置eureka的高可用,所以需要在eureka上獲取服務
    service-url:
      defaultZone: http://peer2:8762/eureka    #設置與eureka交互的地址,查詢服務和注冊服務都需要依賴這個地址,默認是:http://localhost:8761/eureka 

eureka2的配置:

server:
  port: 8762      #聲明端口號
eureka:
  instance:
    hostname: peer2
    appname: peer2
    
  client:
    #register-with-eureka: false    #默認是true,將自己注冊到eureka上,這里設置eureka的高可用,所以需要將自己注冊到eureka上
    #fetch-registry: false    #是否從eureka上獲取信息,由於本案例是單機,無需從別的eureka上獲取注冊信息,這里設置eureka的高可用,所以需要在eureka上獲取服務
    service-url:
      defaultZone: http://peer1:8761/eureka    #設置與eureka交互的地址,查詢服務和注冊服務都需要依賴這個地址,默認是:http://localhost:8761/eureka

此時啟動兩個服務,界面如下
這是peer1:

下面這個是peer2:


免責聲明!

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



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