springcloud-eureka注册中心-ribbon负载均衡

一:Eureka注册中心搭建

1.建springboot注册中心项目,dependence导包

        
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <!--eureka server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
			<version>1.4.3.RELEASE</version>
		</dependency>

2.springcloud包


	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<!--springcloud-->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

3.application.yml配置

server:
  port: 8080
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4.启动项开启注册中心eureka服务(service)

@SpringBootApplication
@EnableEurekaServer //启动eureka服务
public class CloudcenterApplication {

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

5.先启动服务试试

目前没有client连接,所以红框中的client项为空

二:Eureka客户端

1.建springboot审批中心服务,dependence导包

        <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>


		<!--eureka server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
			<version>1.4.3.RELEASE</version>
		</dependency>

2.springcloud包(同上)

3.application.yml配置

eureka:
  client:
    serviceUrl:
      #eureka服务地址
      defaultZone: http://127.0.0.1:8080/eureka/
server:
  #本client的端口
  port: 8091
spring:
  application:
    name: service-audit

4.启动项开启审批服务中心eureka客户端(client)

package com.syf.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@ComponentScan(basePackages = {"com.syf.study.model","com.syf.study.controller"})
public class AuditApplication {

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

5.写一个测试方法,eg:新增一个新增角色审批

package com.syf.study.controller;

import com.syf.study.model.Role;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class RoleController {
    @Value("${server.port}")
    private String port;
    
    @RequestMapping("/queryAllRole")
    private List<Role> queryAllRole(){
        List<Role> list=new ArrayList<Role>();
        for(int i=0;i<10;i++){
            list.add(new Role(i,"role"+i,"count"+i));
        }
        list.add(new Role(11,"port="+port,"countport"+port));
        return list;
    }
    @RequestMapping("/addRoleAudit")
    public Map<String ,Object> addRoleAudit(@RequestBody Role role){
        Map<String ,Object> map =new HashMap<>();
        map.put("code",200);
        map.put("message","success");
        map.put("server.port",port);
        return map;
    }

}

6.查看注册中心是否有客户端注册

三:Eureka客户端

1.建springboot用户中心服务,dependence导包

<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>

		<!--eureka server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
			<version>1.4.3.RELEASE</version>
		</dependency>
		<!--负载均衡-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>

2.springcloud导包(同上)

3.application.yml配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
server:
  port: 8092
spring:
  application:
    name: service-user

4.新增service层

package com.syf.study.service;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private RestTemplate restTemplate;

    public List<Object> queryAllRole(){
        return restTemplate.getForObject("http://service-audit/queryAllRole",List.class);
    }

}

5.controller层

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/queryAllRole")
    public List<Object> queryAllRole(){
        return userService.queryAllRole();
    }
}

6.启动项

package com.syf.study;

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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class UserApplication {

	public static void main(String[] args) {
		SpringApplication.run(UserApplication.class, args);
	}
	@Bean //注册到Bean容器中
	@LoadBalanced //支持负载均衡
	RestTemplate restTemplate(){
		return new RestTemplate();
	}
}

7.打开eureka注册中心查看客户端连接信息

8.用户中心通过负载均衡调用审批中心

四:启动多个服务,具体实现负载均衡演示

1.启动8091和8093端口的同一个审批服务项目,启动用户服务

2.通过用户服务负载均衡访问审批中心

猜你喜欢

转载自blog.csdn.net/yfsread/article/details/81160119