018 Calls between microservices

1 Service Description

    Prepare two maven projects, eureka-sales and eureka-user, and call the eureka-user service by accessing the eureka-sales service. The eureka-user code remains unchanged under the three access methods, just to provide the accessed interface.

    1-1 eureka

        pom dependencies:

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
	</dependencies>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-parent</artifactId>
				<version>Camden.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

        application.yml

server:
  port: 8761
  
spring:
  application:
    name: eureka

eureka: 
  client:
    register-with-eureka: false
    fetch-registry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

        Start the class, annotated with @EnableEurekaServer:

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

    1-2 eureka-sales

        pom dependencies:

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
	</dependencies>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-parent</artifactId>
				<version>Camden.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

        Define the interface:

@RequestMapping("/sales")
public interface SalesRest {
    @RequestMapping(value = "/queryGoodsList/{type}", method = RequestMethod.GET)
    Object queryGoodsList(Integer type);
}

        The interface implementation class decides how to access the eureka-user service according to the incoming type. 1 means ribbon, 2 means feign, and other numbers mean http:

@RestController
public class SalesRestImpl implements SalesRest {

    @Autowired
    private SalesService salesService;

    @Override
    public String queryGoodsList(@PathVariable Integer type) {
    	System.out.println("start to queryGoodsList --->");
    	if(type.intValue() == 1) {
    		return salesService.queryGoodsListByRibbon();
    	}else if(type.intValue() == 2) {
    		return salesService.queryGoodsListByFeign();
    	}else {
            return salesService.queryGoodsListByHttp();
    	}
    }

}

        Method implementation:

@Service
public class SalesService {
	
	@Autowired
	RestTemplate restTemplate;
	
	@Autowired
	UserFeignClient userFeignClient;
	
	private static final String RIBBON_URL = "http://user:8082/user/getUserInfo";
	private static final String HTTP_URL = "http://127.0.0.1:8082/user/getUserInfo";
	private static final String IP = IpUtil.getIp();

	public String queryGoodsListByRibbon() {
		String sales_result = "queryGoodsListByRibbon success : [sales_ip:" + IP + "] ";
		String result = restTemplate.getForObject(RIBBON_URL, String.class);
		return sales_result + result;
	}

	public String queryGoodsListByFeign() {
		String sales_result = "queryGoodsListByFeign success : [sales_ip:" + IP + "] ";
		String result = (String) userFeignClient.getUserInfo();
		return sales_result + result;
	}

	public String queryGoodsListByHttp() {
		String sales_result = "queryGoodsListByHttp success : [sales_ip:" + IP + "] ";
		String result = HttpClientUtil.doGet(HTTP_URL);
		return sales_result + result;
	}
}

        client

@FeignClient(name = "USER")
public interface UserFeignClient {
	@RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)
	String getUserInfo();
}

    1-3 eureka-user

        pom dependencies:

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
	</dependencies>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-parent</artifactId>
				<version>Camden.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

        interface:

@RequestMapping("/user")
public interface UserRest {
	@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
	String getUserInfo();
}

        Interface implementation class:

@RestController
public class UserRestImpl implements UserRest{
	
	@Autowired
	private UserService userService;

	@Override
	public String getUserInfo() {
		System.out.println("start to getUserInfo --->");
		return userService.getUserInfo(); 
	}

}

        The method is implemented. In order to verify the load balancing of the ribbon, the port number of the service is returned here:

@Service("userService")
public class UserService {
	
	@Value("${server.port}")
	private Integer port;
	
	public String getUserInfo() {
		// 当前项目所在IP
		String ip = IpUtil.getIp();
		return "getUserInfo success : [user_ip:" + ip + "user_port:" + port + "] ";
	}

}

2 ribbon method

        Ribbon is a load balancer released by Netflix. When used with Eureka, Ribbon can automatically obtain the address list of service providers from Eureka Server, and request an instance of one of the service providers based on the load balancing algorithm. Ribbon's dependencies are:

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

But spring-cloud-starter-eureka already contains ribbon dependencies, so just add eureka directly.

    2-1 eureka-sales

        pom dependency, just have eureka directly here, eureka already includes ribbon dependency:

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
	</dependencies>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-parent</artifactId>
				<version>Camden.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

        application.yml

server:
  port: 8081

spring:
  application:
    name: sales
    
eureka:
  client:
    serviceUrl: 
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

        Start the class, register RestTemplate here, and add @LoadBalanced to enable load balancing:

@SpringBootApplication
@EnableDiscoveryClient
public class SalesApplication {
    
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
    	return new RestTemplate();
    }

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

}

       Call the eureka-user service, where the user in the RIBBON_URL is the name of the eureka-user registered to the eureka center, that is, application.name:

@Service
public class SalesService {
	
	@Autowired
	RestTemplate restTemplate;
	
	private static final String RIBBON_URL = "http://user:8082/user/getUserInfo";
	private static final String IP = IpUtil.getIp();

	public String queryGoodsListByRibbon() {
		String sales_result = "queryGoodsListByRibbon success : [sales_ip:" + IP + "] ";
		String result = restTemplate.getForObject(RIBBON_URL, String.class);
		return sales_result + result;
	}
}

    2-2 Test

        1. Start eureka 2. Start eureka-sales 3. Start eureka-user 4. Modify the application.yml file of eureka-user, change the port to 8083, and start the second eureka-user service.

        Access interface: http://127.0.0.1:8081/sales/queryGoodsList/1 

        At this time, the ribbon is accessed 10 times, and the result shows that there is load balancing.

3 feign ways

    3-1 eureka-sales

        pom dependency, add feign dependency:

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
	</dependencies>
	</build>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-parent</artifactId>
				<version>Camden.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

        application.yml

server:
  port: 8081

spring:
  application:
    name: sales
    
eureka:
  client:
    serviceUrl: 
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

        Start the class and add the @EnableFeignClients annotation: 

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SalesApplication {
    
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
    	return new RestTemplate();
    }

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

}

        Create the feign client interface, where USER is the registered name of eureka-user in the eureka center, and the interface inside is defined as the interface in eureka-user (called service):

@FeignClient(name = "USER")
public interface UserFeignClient {
	@RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)
	String getUserInfo();
}

         Call the eureka-user service, where the user in the RIBBON_URL is the name of the eureka-user registered to the eureka center, that is, application.name:

@Service
public class SalesService {
	
	@Autowired
	UserFeignClient userFeignClient;
    private static final String IP = IpUtil.getIp();

	public String queryGoodsListByFeign() {
		String sales_result = "queryGoodsListByFeign success : [sales_ip:" + IP + "] ";
		String result = (String) userFeignClient.getUserInfo();
		return sales_result + result;
	}
}

    3-2 Test

        1. Start eureka 2. Start eureka-sales 3. Start eureka-user 4. Modify the application.yml file of eureka-user, change the port to 8083, and start the second eureka-user service.

        Access interface: http://127.0.0.1:8081/sales/queryGoodsList/2

        At this time, the ribbon is accessed 10 times, and the result shows that there is load balancing.

4 http method

        It is to encapsulate a get method of httpclient by yourself, and then access:

http://127.0.0.1:8081/sales/queryGoodsList/3, and then observe the results and find that there is no automatic load balancing:

5 Appendix

    eureka project: https://code.aliyun.com/995586041/eureka.git

    eureka-sales project: https://code.aliyun.com/995586041/ribbon_feign_http_sales.git

    eureka-user project: https://code.aliyun.com/995586041/ribbon_feign_http_user.git

    core-simple project: https://code.aliyun.com/995586041/core-simple.git

    The related tools and methods above are in core-simple 

Guess you like

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