Netflix 有提供了一個稱為 Eureka 的 open source,可以作為服務註冊之用,spring cloud 將它做了封裝,這裡來看一下怎麼使用。
我總共會寫三支程式,分別為 ...
我總共會寫三支程式,分別為 ...
- myEureka: eureka server,註冊服務的地方。
- myEureka-Client: 提供服務的程式,會到 eureka server 註冊,供其它程式使用。
- myConsumer: 想要使用服務的程式,也會到 eureka server 註冊,註冊後即可使用 server 上的服務。
- myEureka
- build.gradle
buildscript { ext { springBootVersion = '2.0.2.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'idv.steven.eureka' version = '1.0' sourceCompatibility = 1.8 repositories { mavenCentral() maven { url "https://repo.spring.io/milestone" } } ext { springCloudVersion = 'Finchley.RC2' } configurations.all { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } dependencies { def log4j2Version = '2.7' compile('org.springframework.boot:spring-boot-starter') compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') { exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-netflix-eureka-client' } compile('org.springframework.boot:spring-boot-starter-tomcat') compileOnly('org.projectlombok:lombok') testCompile('org.springframework.boot:spring-boot-starter-test') // Log4j2 compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4j2Version}" compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4j2Version}" compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: "${log4j2Version}" compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
- MyEurekaApplication.java
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class MyEurekaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MyEurekaApplication.class).web(true).run(args);
}
}
127.0.0.1 peer1 127.0.0.1 peer2
- application.properties
spring.applicaion.name=eureka-server server.port=1111 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureak.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
- application-peer1.properties
eureka.instance.hostname=peer1 server.port=1111 eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/
- application-peer2.properties
eureka.instance.hostname=peer2 server.port=1112 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
- 啟動 server
java -jar myEureka-1.0.jar --spring.profiles.active=peer1 java -jar myEureka-1.0.jar --spring.profiles.active=peer2
- 進入管理介面觀察系統狀況
再開啟一個新的頁面,輸入 http://localhost:1112/ 進入 peer2 的管理介面,會看到如下,peer1 註冊到 peer2 了。
- myEureka-Client
- build.gradle
buildscript { ext { springBootVersion = '2.0.2.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'idv.steven.eureka' version = '1.0' sourceCompatibility = 1.8 repositories { mavenCentral() maven { url "https://repo.spring.io/milestone" } } ext { springCloudVersion = 'Finchley.RC2' } configurations.all { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } dependencies { def log4j2Version = '2.7' compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') compileOnly('org.projectlombok:lombok') testCompile('org.springframework.boot:spring-boot-starter-test') // Log4j2 compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4j2Version}" compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4j2Version}" compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: "${log4j2Version}" } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
- MyEurekaClientApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class MyEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(MyEurekaClientApplication.class, args);
}
}
- application.properties
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/
- HelloController.java
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
@RestController
@Slf4j
public class HelloController {
@Autowired
private DiscoveryClient client;
@Autowired
private Registration registration;
@RequestMapping(value ="/hello", method = RequestMethod.GET)
public String index() {
List<ServiceInstance> list = client.getInstances(registration.getServiceId());
list.forEach(s -> {
System.out.println("service id: " + s.getServiceId());
});
return "Hello";
}
}
- 進入管理介面觀察結果
- myConsumer
- build.gradle
buildscript {
ext {
springBootVersion = '2.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'idv.steven.consumer'
version = '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
}
ext {
springCloudVersion = 'Finchley.RC2'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
- application.properties
spring.application.name=myconsumer
server.port=9090
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/
- MyConsumerApplication.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class MyConsumerApplication { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MyConsumerApplication.class, args); } }
- ConsumerController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/myconsumer", method = RequestMethod.GET)
public String helloConsumer() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}
- 測試
沒有留言:
張貼留言