springcloud+eureka+feign负载均衡

目前,在Spring cloud 中服务之间通过restful方式调用有两种方式

  • restTemplate+Ribbon
  • feign
    现在咱们采用feign进行负载均衡,他的好处就是可以实现接口式的负载均衡,符合接口式的开发规范,而不是像ribbon,要结合restTemplate,通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,所以也支持负载均衡作用。

服务如下:
注意这里没有的dept-80服务,它被dept-feign服务代替。
springcloud+eureka+feign负载均衡_第1张图片

1 microservicecloud-api

它主要为其他服务提供实体类和接口服务,打成,写成后打成jar即可
我们也可以将feign服务接口写在dept-feign服务里面,但是这些接口可能会被其他服务所调用,所以建议写在api中。
springcloud+eureka+feign负载均衡_第2张图片
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>com.wuk.springcloudgroupId>
		<artifactId>microservicecloudartifactId>
		<version>0.0.1-SNAPSHOTversion>
	parent>
	<artifactId>microservicecloud-apiartifactId>
	<dependencies>
		<dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
		dependency>
		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-feignartifactId>
		dependency>
	dependencies>
	
project>

DeptClientService.java

package com.wuk.service;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wuk.entity.Dept;

@FeignClient(value = "producers")
public interface DeptClientService {

	@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
	public Dept get(@PathVariable("id") long id);

	@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
	public List<Dept> list();

	@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
	public boolean add(Dept dept);

}

然后将其打成jar,引入到其他服务中。

2 microservicecloud-consumer-dept-feign

springcloud+eureka+feign负载均衡_第3张图片
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>com.wuk.springcloudgroupId>
		<artifactId>microservicecloudartifactId>
		<version>0.0.1-SNAPSHOTversion>
	parent>
	<artifactId>microservicecloud-consumer-dept-feignartifactId>
	<dependencies>
		<dependency>
			<groupId>com.wuk.springcloudgroupId>
			<artifactId>microservicecloud-apiartifactId>
			<version>${project.version}version>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>springloadedartifactId>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-devtoolsartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-eurekaartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-configartifactId>
		dependency>
		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-feignartifactId>
		dependency>

	dependencies>
project>

application.yml

server:
  port: 80
  
eureka:
  client:
    register-with-eureka: false
    service-url: 
      defaultZone: http://127.0.0.1:7001/eureka/

DeptController_Consumer.java

package com.wuk.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wuk.entity.Dept;
import com.wuk.service.DeptClientService;

@RestController
public class DeptController_Consumer {
	
	@Autowired
	private DeptClientService service = null;


	@RequestMapping(value = "/consumer/dept/add")
	public boolean add(Dept dept) {
		return service.add(dept);
	}
	
	@RequestMapping(value = "/consumer/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id) {
		return service.get(id);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/consumer/dept/list")
	public List<Dept> list() {
		return service.list();
	}
}

DeptConsumer80_Feign_App.java

package com.wuk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.wuk"})
@ComponentScan("com.wuk")
public class DeptConsumer80_Feign_App {

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

剩下的两个服务方和服务注册中心上一篇有,不做赘述。

你可能感兴趣的:(架构)