In the last article, we introduced Eureka cluster environment construction. In this paper, we use Eureka as the registry to achieve a case, the case structure is as follows

1. Start Eureka service

Each of our deployed Eureka points to the following command to start the Eureka service

./server.sh start

Copy the code

2. Build the Provider service

1. Create a SpringBoot project

2. Modify the POM file

Add dependencies. Note that Eureka dependencies do not need to add “-server”

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> < version > 1.4.5. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - eureka < / artifactId > < version > 1.4.6. RELEASE < / version > < / dependency > < / dependencies > <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope>  </dependency> </dependencies> </dependencyManagement>Copy the code

3. Modify the startup class

Open the primary key of the Eureka client in the header of the startup class

4. Add configuration information

Add the following information to application.properties

Name =eureka-provider server.port=9090 Point to another registry eureka. Client. ServiceUrl. DefaultZone = http://eureka1:8761/eureka/, http://eureka2:8761/eureka/Copy the code

Notice For Eureka1 and Eureka2, you need to configure mappings in the hosts file as follows

5. Add services

Create a pojo

package com.dpb.springcloud_eureka_provider.pojo; /** * @program: Springcloud-eureka-provider * @description: UserBean * @author: 2019-05-28 17:31 */ public class User { private int userid; private String username; private int userage; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getUserage() { return userage; } public void setUserage(int userage) { this.userage = userage; } public User(int userid, String username, int userage) { this.userid = userid; this.username = username; this.userage = userage; } public User() { } }Copy the code

Creating a Controller

/** * @program: Springcloud -eureka-provider * @description: service provider * @author: Bobbo -provider * @create: 2019-05-28 17:30 */ @RestController public class UserController { @RequestMapping("/user") public List<User> getUsers(){  List<User> list = new ArrayList<>(); list.add(new User(1,"zhangsan",20)); list.add(new User(2,"lisi",22)); list.add(new User(3,"wangwu",20)); return list; }}Copy the code

6. Start the service

Start the service and we can see our service provider in the registry

Build Consumer services

Consumers create services, much like providers.

1. Create projects

2. The pom

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> < version > 1.4.5. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - eureka < / artifactId > < version > 1.4.6. RELEASE < / version > < / dependency > < / dependencies > <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope>  </dependency> </dependencies> </dependencyManagement>Copy the code

3. Start the class

4. Configuration file

Name =eureka-consumer server.port=9091 Point to another registry eureka. Client. ServiceUrl. DefaultZone = http://eureka1:8761/eureka/, http://eureka2:8761/eureka/Copy the code

5. Complete the service invocation

The consumer completes the invocation of the server-side service

In the service code

/** * @program: Springcloud-eureka-consumer * @description: User's business processing * @author: bobo ~ ~ * @create: 2019-05-28 17:43 */ @service public class UserService {/** ** Ribbon load Balancing * LoadBalancerClient */ @autoWired Private LoadBalancerClient LoadBalancerClient; Public List<User> getUsers(){ServiceInstance Encapsulates basic service information, such as the IP address and port number. ServiceInstance si = this.loadBalancerClient.choose("eureka-provider"); StringBuilder sb = new StringBuilder(); sb.append("http://") .append(si.getHost()) .append(":") .append(si.getPort()) .append("/user"); // SpringMVC RestTemplate RestTemplate rt = new RestTemplate(); ParameterizedTypeReference<List<User>> type = new ParameterizedTypeReference<List<User>>() {}; ResponseEntity<List<User>> response = rt.exchange(sb.tostring (), httpmethod.get,null,type); List<User> list = response.getBody(); return list; }}Copy the code
interface instructions
LoadBalancerClient Load balancers can obtain the IP address and host information of the corresponding service based on the service name
RestTemplate Rest service templates that can be used to invoke services

Controller code

/** * @program: Springcloud-eureka-consumer * @description: user controller * @author: Bobo * @create: 2019-05-28 17:40 */ @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/consumer") public List<User> getUsers(){ return this.userService.getUsers(); }}Copy the code

6. Start the service

After starting the service, we can discover the service in the Eureka registry.

Request the consumer’s interface to retrieve the data provided by the service provider.

Case success