Spring Cloud's Eureka-based Microservice Cluster Implementation

      Eureka is integrated in Spring Cloud , so Eureka 's distributed services can be used in Spring Boot projects . In this example, we simulate the pseudo-distributed use of Eureka . The ports are 8761 ( default ), 8762, 8763 , and also The host file needs to be modified and specified as:

127.0.0.1 peer1

127.0.0.1 peer2

127.0.0.1 peer3

1. To enable Eureka in Spring Cloud, you need to add the following dependencies to the pom file

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

 

2. Next, you need to modify the configuration file of the project. Since three nodes are simulated, three configuration files are required. Only the port number and hostname are specified differently. The specific configuration of application-s1.yml is as follows:

server:
  port: 8761
spring:
  application:
    name: EurekaServer
eureka:
  instance:
    hostname: peer1 #corresponding hostname
  client:
    service-url:
      defaultZone: #Three service nodes are interconnected
        http://peer1:8761/eureka/,http://peer2:8762/eureka/,http://peer3:8763/eureka/

 You can modify the corresponding port and hostname in application-s2.yml, application-s3.yml

3. Add Eureka service annotations to the mani function

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApp.class, args);
	}
}

 4. Use maven to package, generate a runnable jar file, and execute the following command

spring-cloud>java -jar eureka-server\target\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=s1

spring-cloud>java -jar eureka-server\target\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=s2

spring-cloud>java -jar eureka-server\target\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=s3

An exception such as Connect to peer3:8763 timed out will be reported at startup. After the service is fully started, the console window will appear 2017-04-12 16:46:14.991 INFO 13132 --- [nio-8761-exec-6] cneregistry .AbstractInstanceRegistry : Registered instance EUREKASERVER/*.*.*.144:EurekaServer:8
763 with status UP (replication=true), indicating that the service has been started successfully, open http://localhost:8761/, you can see that the cluster is running normally


Note : If the domain name is not used in the configuration file, but localhost or ip (127.0.0.1/external network ip) is specified, the service can be started normally, but the fragmented service is always displayed in unavailable-replicas, so specify in host corresponding domain name for service distinction

5. Add an external service interface, create a new controller, and print out the information of the current calling service

@RestController
public class AppServiceController {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private DiscoveryClient client;

	@SuppressWarnings("deprecation")
	@RequestMapping("/hello/{user}")
	public String hello(@PathVariable("user") String user) {
		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("/add, host:" + instance.getHost() + ", port: " + instance.getPort() + ", service_id:"
				+ instance.getServiceId() + ", user:" + user);
		return "Hello " + user;
	}
}

 6. Create a new client call project and access the rest service provided by the Eureka cluster in the project. This project is an ordinary Spring Boot project, and the following dependencies are added to the pom

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

 7. Set the project registration property to false in the configuration file and not register in the Eureka cluster

spring:
  application:
    name: eureka-client
eureka:
  client:
    register-with-eureka: false

  server:
    name: eurekaServer

 8. The main class of the client accesses the Eureka cluster through the service name, and the default load balancing strategy is polling

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class EurekaClientApp {
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	@Value("${eureka.server.name}")
	private String eurekaServer;
	
	@Autowired
	RestTemplate restTemplate;

	@RequestMapping(value = "/{user}")
	public String hello(@PathVariable("user") String user) {
		return restTemplate.getForEntity("http://" +eurekaServer +"/hello/"+ user, String.class).getBody();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApp.class, args);
	}
}

 9. Visit http://localhost:8080/tom, the page prints Hello tom, the parameters can be changed, and the log information of the visit is displayed on the server side

2017-04-12 18:12:31.888  INFO 13088 --- [nio-8762-exec-3] org.eureka.server.AppServiceController   : /add, host:peer2, port: 8762, service_id:EurekaServer, user:tom

2017-04-12 18:13:03.310  INFO 6448 --- [nio-8763-exec-2] org.eureka.server.AppServiceController   :

/add, host:peer3, port: 8763, service_id:EurekaServer, user:jack

2017-04-12 18:13:24.342  INFO 12992 --- [nio-8761-exec-6] org.eureka.server.AppServiceController   : /add, host:peer1, port: 8761, service_id:EurekaServer, user:anny

After three visits, you can see that the server is visited once respectively, and the polling strategy is used.

 

Project code address: https://github.com/hjguang/spring-cloud

This project contains other projects, you can use the following command to install only the Eureka project

spring-cloud>mvn install -pl eureka-server,eureka-client -DskipTests=true

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326404312&siteId=291194637